1
votes

first time poster here. Sorry if this is an obvious fix, but I'm very new to the world of nodejs and programming in general.

I'm currently trying to create a Discord bot that allows any user to initiate a "love it or hate it" vote with the !vote command. Once the vote is initiated, the bot sends out a message announcing the vote, then reacts to its own message with a heart and skull emoji to denote the love and hate options, respectively. This part is working as intended.

After a set amount of time passes (a very short period), the bot should tally the emoji reactions and figure out if there are more hearts, more skulls, or an equal number of both. Depending on the outcome, it will send another message announcing the outcome of the vote. This part is not working as intended.

As it stands, I can get the bot to respond to my !vote command by sending a new message in the chat and reacting to that message with the proper emojis. The bot will also wait for the set amount of time and announce the outcome of the vote. However, it always announces that the vote was neutral, regardless of which emoji I clicked on before the timer expired (making sure I didn't click both, of course).

My code to compare the number of votes clearly is not functioning as intended. However, after spending hours trying out different fixes, I can't figure out the solution for the life of me and it's driving me crazy. Is there a part of this that's incorrect? And if so, how do I fix it?

Many thanks to anyone who can chime in. After lurking for a while and finding countless fixes in other people's questions in the past, I thought I'd finally turn to the lovely people at Stack Overflow for help. You guys rock!

const Discord = require('discord.js');
const bot = new Discord.Client();

bot.on('message', function(message){
    if(message.content.toLowerCase().startsWith('!vote'))
    {
    var heartCount = 0;
    var skullCount = 0;
        message.channel.send(
            "The vote begins! Do we love it or hate it?")
                .then(async function (message){
                    try {
                    await message.react("❤️")
                    await message.react("????")
                    }
                catch (error) {
                    console.error('One of the emojis failed to react.');
                    }
                })
        const filter = (reaction, user) => {
        return ["❤️","????"].includes(reaction.emoji.name) && user.id === message.author.id };
                
                message.awaitReactions(filter, {time: 10000})
                .then(collected => {
                    for (var i = 0; i < collected.length; i++){
                        if (collected[i].emoji.name === "❤️")
                        {heartCount++;}
                        else if (collected[i].emoji.name === "????")
                        {skullCount++;}
                    };
                
                    if (heartCount > skullCount){
                        message.channel.send("We love it!");
                    }
                    else if (heartCount < skullCount){
                        message.channel.send("We hate it.");
                    }
                    else {
                        message.channel.send("We're neutral about it.");
                    }
                    
                })
    }
});

bot.login(process.env.BOT_TOKEN);
1

1 Answers

0
votes

The fist problem its user.id === message.author.id so only message author can react. message.channel.send return a promise of new message, so you can use then => for message react. Better use action collector on collect for get count and then when collector ends send a message.

const Discord = require('discord.js');
const bot = new Discord.Client();

bot.on('message', function(message){
    var heartCount = 0;
    var skullCount = 0;

    if(message.content.toLowerCase().startsWith('!vote')) {
        message.channel.send('The vote begins! Do we love it or hate it?').then(msg => {
            msg.react(`❤️`).then(() => msg.react('💀'));
            const filter = (reaction, user) => {
                return [`❤️`, '💀'].includes(reaction.emoji.name);
            };

            const collector = msg.createReactionCollector(filter, {time: 10000});
            collector.on('collect', (reaction, reactionCollector) => {
                if (reaction.emoji.name === `❤️`) {
                    heartCount+=1
                } else if (reaction.emoji.name === `💀`) {
                    skullCount+=1
                }
            });
            collector.on('end', (reaction, reactionCollector) => {
                   if (heartCount > skullCount){
                        message.channel.send("We love it!");
                    }
                    else if (heartCount < skullCount){
                        message.channel.send("We hate it.");
                    }
                    else {
                        message.channel.send("We're neutral about it.");
                    }
            });

        })
    }
})