1
votes

I am coding a Discord bot in Discord.js. I want to make it so when a member joins the server they have to react to a message in a certain channel to get a role. But for some reason, if a message was sent before I turned the bot on, it would not detect it if that message was reacted to and only messages after the bot was on were detected.

Here is the code I tried first:

bot.on("messageReactionAdd", (reaction, users) => {
    
    console.log("reaction");

    if (reaction.message.channel.name == "rules") {
        console.log("rules")
    } else {
        console.log("not rules");
    }

});

It told the the channel the reaction was in and if a message was reacted to but only if the message was sent after the bot started.

In desperation, I tried this code next:

bot.on("messageReactionAdd", () => {
    
    console.log("reaction");

});

It did the same thing as the other code.

I want it to detect a reaction even if the message was send before the bot was started.

2

2 Answers

0
votes

messageReactionAdd works with cached messages, this means when the bot was on, few solutions to this.

1st option: <message>.createReactionCollector()

2nd option: manually set the cache with that message

3rd option. make a new message on bot connection

4th option. use commands instead of reactions like .agree

Just found a link that might be useful: https://github.com/AnIdiotsGuide/discordjs-bot-guide/blob/master/coding-guides/raw-events.md

Also this thread might be useful: Why messageReactionAdd do nothing discord.js

0
votes

If you want to use an easier way than the above, you can use Partials. I'm not the best at explaining things, but Partials are given when there's not enough provided data for the event, even though everything's correct (correct me if I'm wrong).

Anyways, if you want to add them to your code, you'd need to replace your Client instance. You'd want yours to look like this:

const Discord = require('discord.js');
const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION"] });

Have fun!