Search code examples
javascriptnode.jsdiscorddiscord.js

How to detect if a user has reacted to a embedded reaction of a message in discord js and sends him a message?


I am new to Discord and Discord js and what I am trying to do here is that I wish my discord bot to detect if a user has just reacted to a emoji with that message. The emoji here is [:toolbox:]. I am using a third party bot for sending the message in the channel where the user will react with emoji and he will be assigned the role by third party bot. Once that is done I need to send a personal message to the user. Here is my code

if(reaction.msg.id == "910907568459759617"){
        console.log("Reaction detected.Sending a message to the user in dm");
        reaction.author.send("Holla Can you send me your email id ?");
    }

I am not even able to detect any changes or any click done on reaction emoji on that message. Please help.


Solution

  • What you are looking for is the Message.createReactionCollector method.

    You first need to declare the <Message> object that represents the message on which you'd like to create the reaction collector. One way of doing this would be through a channel ID where the message is sent, and a message ID. Like so:

    const message = await (await <Client>.channels.fetch(CHANNEL_ID)).messages.fetch(MESSAGE_ID);
    

    I'm obviously not handling any Promise rejections in these examples for simplicity's sake. You can then use the previously mentioned createReactionCollector method to expect and handle reactions on this message:

    const collector = message.createReactionCollector(<ReactionCollectorOptions>);
    
    collector.on("collect", <Reaction> => {
        //TODO
    });
    
    collector.on("end", <Collection> => {
        //TODO
    });