I already have a variable containing a specific channel, but how can I obtain the last message sent to the channel? I want to make my bot only perform an action if the last message to the channel wasn't by it.
If you already have the specific channel stored in a variable, it's quite easy. You can call the MessageManager#fetch()
method on that specific channel and get the latest message.
Example:
let channel // <-- your pre-filled channel variable
channel.messages.fetch({ limit: 1 }).then(messages => {
let lastMessage = messages.first();
if (!lastMessage.author.bot) {
// The author of the last message wasn't a bot
}
})
.catch(console.error);
However if you don't have the complete channel object saved in a variable but only the channel ID, you'll need to fetch the correct channel first by doing:
let channel = bot.channels.get("ID of the channel here");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With