Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last message sent to channel

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.

like image 487
clubby789 Avatar asked Dec 09 '18 21:12

clubby789


1 Answers

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");
like image 137
T. Dirks Avatar answered Nov 16 '22 00:11

T. Dirks