Discord 机器人:努力计算表情符号对投票的反应

Discord bot: struggling to tally emoji reacts for voting

第一次张贴在这里。很抱歉,如果这是一个明显的修复,但我对 nodejs 和一般编程的世界还很陌生。

我目前正在尝试创建一个 Discord 机器人,它允许任何用户使用 !vote 命令发起 "love it or hate it" 投票。投票开始后,机器人会发出一条消息宣布投票结果,然后用心形和骷髅表情符号对自己的消息做出反应,分别表示爱和恨的选择。 这部分工作正常。

经过一定时间(很短的时间)后,机器人会计算表情符号的反应,并计算出是否有更多的心、更多的头骨,或者两者的数量相等。根据结果​​,它将发送另一条消息宣布投票结果。 这部分没有按预期工作。

就目前而言,我可以通过在聊天中发送一条新消息并使用适当的表情符号对该消息作出反应,让机器人响应我的 !vote 命令。机器人还将等待设定的时间并宣布投票结果。然而,它总是宣布投票是中立的,不管我在计时器到期前点击了哪个表情符号(当然要确保我没有点击两个)。

我用来比较票数的代码显然没有按预期运行。然而,在花了几个小时尝试不同的修复之后,我无法找到适合我生活的解决方案,这让我发疯。这其中有一部分是不正确的吗?如果是这样,我该如何解决?

非常感谢任何可以插话的人。在潜伏了一段时间并在过去的其他人的问题中找到了无数的解决方案之后,我想我终于可以向 Stack Overflow 上可爱的人们寻求帮助了。你们摇滚!

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);

第一个问题是 user.id === message.author.id 所以只有消息作者才能做出反应。 message.channel.send return 新消息的承诺,因此您可以使用 then => 进行消息反应。最好使用操作 collector on collect 获取计数,然后在收集器结束时发送消息。

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.");
                    }
            });

        })
    }
})