Motofo20
Motofo20

Reputation: 1

How do I make my discord bot react with ✅ to the last message

I am in the process of programming a counting bot and want the bot to respond to the message with the emoji ✅ for every correct number. The bot was written in js.

I want the bot to respond to the right message with the emoji ✅. This is the code snippet I have so far:

let count = 1
let timeout

client.on('messageCreate', ({channel, content, member}) => {
  // Only do this for the counting channel of course
  // If you want to simply make this work for all channels called 'counting', you
  // could use this line:
  // if (client.channels.cache.filter(c => c.name === 'counting').has(channel.id))

  if (channel.id === channelid) {
    if (member.user.bot) return

    if (Number(content) === count + 1) {
      // ...increase the count
      count++     
      if (timeout) clearTimeout(timeout)

      // Add a new timeout
      timeout = setTimeout(
        // This will make the bot count and log all errors
        () => channel.send(++count).catch(console.error),
        // after 30 seconds
        30000
      )
    // If the message wasn't sent by the bot...
    } else if (member.id !== client.user.id) {
      // ...send a message because the person stuffed up the counting (and log all errors)
      channel.send(`${member} messed up!`).catch(console.error)
      // Reset the count
      count = 0
      // Reset any existing timeout because the bot has counted so it doesn't need to
      // count again
      if (timeout) clearTimeout(timeout)
    } else if (lastMsgAuthor && message.author.id === lastMsgAuthor) {
        message.delete();
    }
  }
});

Upvotes: 0

Views: 377

Answers (1)

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23161

You should use message.react('✅'). The problem is that there is no message object as you've already destructured it. Instead, you could name the argument message and destructure it as the first thing in your callback, like this:

client.on('messageCreate', (message) => {
  const { channel, content, member } = message;

  if (channel.id === channelid) {
    // ...

And use the message object to send a reaction:

if (Number(content) === count + 1) {
  message.react('✅').catch(console.error);
  count++;

  if (timeout)
    clearTimeout(timeout);
  // ...

You could also grab the react() method though, like this:

client.on('messageCreate', ({ channel, content, member, react }) => {
  if (channel.id === channelid) {
    // ...

And then just call react():

if (Number(content) === count + 1) {
  react('✅').catch(console.error);
  count++;

  if (timeout)
    clearTimeout(timeout);
  // ...

Upvotes: 1

Related Questions