I'm very new to using Discord.JS, so this didn't come naturally to me. Sorry if it feels like I'm just being stupid, but I have no idea how I am meant to get the last post in a channel using a Discord bot.
Any helps is appreciated, thanks!
TextChannels have a lastMessage property which lets you get the last last message sent in a channel. So, if you have a channel stored in the channel variable, you can get the last message sent via channel.lastMessage.
Related
Im getting a message that got reacted with a emoji, and want to send it in another channel, but I cant get the embed information from that message reaction.message.embeds is empty.
Do I need to set some intents to be able to read that data? Like GatewayIntentsBits.MessageContent for messages
Want to re-send a message with embed to another channel
[EDIT]
When I do some weird shit: reaction.message.channel.messages.fetch(reaction.message.id) the embeds tag seems to work, is there any way or reason for that? Or should I use this weird way, can someone explain to me why the first example doesnt work?
In regards of the Intents:
This property requires the GatewayIntentBits.MessageContent privileged intent in a guild for messages that do not mention the client. (https://discord.js.org/#/docs/discord.js/main/class/Message?scrollTo=embeds)
The return is an Array of embeds. Therefore you would need to access it with reaction.message.embeds[0] for example. Of course if your reaction.message.embedsis completly empty this would not help.
A good way to check for the information is to console.log(reaction.message)
You can also fetch() the reaction directly to get the full MessageReaction information.
reaction.fetch().then((fetchedReaction) => {
fetchedReaction.message.embeds[0]; //or
fetchedReaction.message.embeds;
});
Your solution
reaction.message.channel.messages.fetch(reaction.message.id)
is indeed the best and correct way if the message may not be in the cache. This way it is still being retrived.
Backstory
So, basically what I'm trying to do is using YT, I saw a lockdown command that locks down every channel in the guild once we type "!lockdown" by overwriting the channel permissions for certain roles but if we unlock the channels, everyone is able to type in other channels as permission gets overwritten to SEND message.
Trying to achieve
What I'm trying to achieve is that once we type "!lockdown", it stores the current permissions and then reapply them after I type "!unlock", I'm pretty sure it's possible but I can't figure out how. Any sorts of help will be appreciated!
Thanks in advance!
You may store the permissions of each channel in an object or an array or a database (more efficiency since the bot can be offline between both !lockdown and !unlock).
Then get the datas by a key as the id of each channel to get their permissions and do waht you want.
i'm coding a discord bot with JavaScript. It's running pretty good, but the only thing i can't do is sending a message in the server when a new member joins the server. (Not a private message, directly a message in the server). I don't find any way to do that, any ideas ?
bot.on('guildMemberAdd', function(member){
member.send("Hello there !");
});
This one sends a private message, it does exactly what i don't want it to do ^^'
The member parameter that gets passed into the callback function is exactly that--an object representing a discord member. If you want to send a message to the guild the member just joined, you will need to get the guild property of the member, member.guild and then get the channel you want from channels and send the message you wish. All together it should look something like this:
bot.on('guildMemberAdd', function(member){
member.guild.channels.get("put-channel-id-here").send("Hello there!")
});
I think you may know by now but in Discord.js v12 (current stable version), you would use
member.guild.channels.cache.get("put-channel-id here").send("Hello there!")
I want to make my discord bot make an announcement to a certain channel, based on the Channel ID.
I know how to make the bot respond in whatever channel it's talked to, using message.channel.send(), but I was wondering if there was a way to do message.channelID.send() to have the bot talk in a specific channel to make an announcement.
Thanks!
Nathan
client.channels.find("id", "what ever").send(/*...*/)
All channels are stored in a Collection under the Client.channels prpperty which you can use to get it.
Using client.channels.find("id", "what ever").send(/*...*/); will work to send a message to a certain channel, but in the console, an alert is thrown. Using client.channels.get("what ever").send("/*...*/"); does the same job, but without using a discord.js deprecated function.
Thanks to Jonas Wilms!
How do I get the main channel from a server? Almost all major bots do this. For example, Rythm. The bot sends a message in the main channel that tells you how to use Rythm. That's kind of what I intend to do also, to let people know how to use my bot. If you could help me with this, that would be much appreciated. Thanks!
Guilds have a defaultChannel value you can access.
guild.defaultChannel.send(`Hello`);
Documentation:
https://discord.js.org/#/docs/main/stable/class/Guild?scrollTo=defaultChannel
Though this is Deprecated you can still use it unless the #general channel is removed.
Otherwise:
For the channel the invite refers to:
https://discord.js.org/#/docs/main/stable/class/Invite
invite.channel is the channel the invite refers to