Raging Ultimate
Raging Ultimate

Reputation: 17

How to make a Discord Bot (Javascript) react to reactions

I've been trying to make a text-based game using a Javascript Discord Bot, and I can't figure out how to make the bot send a message upon the user sending a reaction (i.e. player reacting to bot message with a certain emoji).

client.on("message", message => { 
  if (message.content === "!start") {
    message.channel.send("Hello")
  }

  if (message.content === "Hello") {
    message.react("😄")
  }

  if (message.react === "😄") {
    message.channel.send("You walk into the labyrinth")
  }
)}

Is it possible for someone to help correct whatever's wrong?

Upvotes: 1

Views: 157

Answers (1)

NullDev
NullDev

Reputation: 7303

I'm not sure if there is a dedicated event for it but you can catch the reaction event by using raw:

const events = {
    MESSAGE_REACTION_ADD: "messageReactionAdd",
    MESSAGE_REACTION_REMOVE: "messageReactionRemove"
};

client.on("raw", async (event, client) => {
    if (!events.hasOwnProperty(event.t)) return;

    const { d: data } = event;

    const message = await client.channels.cache.get(data.channel_id).messages.fetch(data.message_id);

    if (event.d.emoji.name !== "😄"){
        message.channel.send("You walk into the labyrinth");
    }

    // ...

    if (event.t === "MESSAGE_REACTION_ADD"){
        // Reaction was added
    }
    else if (event.t === "MESSAGE_REACTION_REMOVE"){
        // Reaction was removed
    }
});

Edit: Seems like there is a dedicated event.

Upvotes: 1

Related Questions