1
votes

I'm trying to get my first discord bot to send a message after clicking a reaction,

One for yes reaction, one for no

I have my code already send an embed with the reactions

I created a Reaction collector but the only thing now its that it instantly reacts with (reacted no) twice, even before I click the reaction

Help is greatly appreciated!

My Code so far:

const {Client, RichEmbed } = require('discord.js');
const client = new Client();
const a =
client.once('ready', () => {
    console.log('boogie time!');
});
client.on('message', message => {
    if(message.author.bot)
    {
        if(message.embeds)
        {
            const embedMsg = message.embeds.find(msg=> msg.title ==='Boogie Time!?');
            if(embedMsg)
            {
                embedMsg.message.react('✅')
                .then(reaction => reaction.message.react('❌'))

                // This is filter, this specified which reactions it should capture, you can use filter to make sure you're only catching specific reactions by specific user
const filter = (reaction, user) => (reaction.emoji.name === '✅' || reaction.emoji.name === '❌') && user.id === message.author.id;

// Here, we're defining a collector that will be active for 30 seconds and collect reactions that pass the above filter
const collector = embedMsg.message.createReactionCollector(filter, {time: 10000});

// This event is emitted when a reaction passes through the filter
collector.on('collect', r => r.name === '✅' ? 
console.log('Reacted Yes') : console.log('Reacted No'));



            }
        }
        return;
    }


if(message.content.toLowerCase() === 'boogie')
{
    const embed = new RichEmbed();
    embed.setTitle("Boogie Time!?")
    embed.setColor("GREEN")
    embed.setDescription("Are you sure?")
    message.channel.send(embed);



};
});
1

1 Answers

1
votes

You have a several problems here. The first issue is you are only looking at messages sent by a bot if(message.author.bot) and then later trying to filter by that message author which will always be a bot, not you or anyone else user.id === message.author.id. I think your intention may have been to not collect bot reactions.

The second issue you have is that the asynchronous execution is causing the collector to get created before the bot adds the initial reaction.

embedMsg.message.react('✅')
   .then(reaction => reaction.message.react('❌'))

After this call to .react, the code below it starts immediate async execution, before the reactions complete. If you aren't listening to the bots reaction this shouldn't be an issue, but if you enclose the collector creating in a second .then statement it will ensure it doesn't create it until the second reaction is complete and you won't need to filter the user.id because the bot shouldn't react after that, thus eliminating both problems.

So the cause of the problem is the bot is collecting it's own two reactions. Why is it always saying 'React No' then? This is the third issue:

collector.on('collect', r => r.name === '✅' ? 
console.log('Reacted Yes') : console.log('Reacted No'));

Here you have forgotten to call out the reactions emoji. This line should be:

collector.on('collect', r => r.emoji.name === '✅' ? 
console.log('Reacted Yes') : console.log('Reacted No'));

In conclusion, this should be the changes described above:

if(embedMsg)
{
    embedMsg.message.react('✅')
    .then(reaction => reaction.message.react('❌')
        .then(() => {
            // This is filter, this specified which reactions it should capture, you can use filter to make sure you're only catching specific reactions by specific user
            const filter = (reaction, user) => (reaction.emoji.name === '✅' || reaction.emoji.name === '❌');

            // Here, we're defining a collector that will be active for 30 seconds and collect reactions that pass the above filter
            const collector = embedMsg.message.createReactionCollector(filter, {time: 10000});

            // This event is emitted when a reaction passes through the filter
            collector.on('collect', r => r.emoji.name === '✅' ? 
            console.log('Reacted Yes') : console.log('Reacted No'));
    }));
}