首页 > 解决方案 > 用户反应后的机器人确认未在 Discord.js 中显示

问题描述

我希望用户使用反应来回答“是或否”的问题。但是,当被标记的用户对问题做出反应时,机器人不会发送关于被标记的用户是否想要协商的消息。下面是我的代码。

     const yesEmoji = '✅';
        const noEmoji = '❌';
        client.on('message', (negotiate) => {
            const listen = negotiate.content; 
            const userID = negotiate.author.id;
            var prefix = '!';
            var negotiating = false; 
            let mention = negotiate.mentions.users.first();
        
            if(listen.toUpperCase().startsWith(prefix + 'negotiate with '.toUpperCase()) && (mention)) {
                negotiate.channel.send(`<@${mention.id}>, do you want to negotiate with ` + `<@${userID}>`)
                .then(async (m) => {
                    await m.react(yesEmoji);
                    await m.react(noEmoji);

                    //get an answer from the mentioned user
                    const filter = (reaction, user) => user.id === mention.id;
                    const collector = negotiate.createReactionCollector(filter);
                    collector.on('collect', (reaction) => {
                         if (reaction.emoji.name === yesEmoji) {
                              negotiate.channel.send('The mentioned user is okay to negotiate with you!');
                             
                         } 
                         else {
                              negotiate.channel.send('The mentioned user is not okay to negotiate with you...')
                         }
                    })
                })
                negotiating = true;
            }
        })

So far, the code displays the reaction but it does not make the bot send a message whether the tagged user is ok or not ok to negotiate with the user that tagged them. 

更新:我设法让机器人发送一条消息,无论标记的用户是否可以与标记他们的用户协商。现在有一个错误,在 10 秒(指定时间)后显示。以下是更新后的代码:

const yesEmoji = '✅';
const noEmoji = '❌';

client.on("message", async negotiate => {
    const listen = negotiate.content; 
    let mention = negotiate.mentions.users.first();
    if(listen.toUpperCase().startsWith(prefix + 'negotiate with '.toUpperCase()) && (mention)) {
        let mention = negotiate.mentions.users.first();
        let msg = await negotiate.channel.send(`${mention} do you want to negotiate with ${negotiate.author}`);
        var negotiating = false;

        await msg.react(yesEmoji);
        await msg.react(noEmoji);

        const filter = (reaction, member) => {
        return reaction.emoji.name === yesEmoji || reaction.emoji.name === noEmoji && member.id === mention.id;
        };

        msg.awaitReactions(filter, { max: 1, time: 10000, errors: ['time'] })
        .then(collected => {
            const reaction = collected.first();
            if (reaction.emoji.name === yesEmoji) {
            negotiating = true;
            negotiate.reply('The mentioned user agreed to negotiate with you!');
            }
            else return negotiate.reply('The mentioned user did not agree to negotiate with you.')
        })
    }
})

标签: javascriptnode.jsdiscorddiscord.js

解决方案


对于您的问题,我有一个更简单的解决方案:

    const yesEmoji = '✅';
    const noEmoji = '❌';

    let mention = negotiate.mentions.users.first();
    if(mention.id === negotiate.author.id) return message.channel.send("You cannot tag yourself!");
    let msg = await negotiate.channel.send(`${mention} do you want to negotiate with ${negotiate.author}`);
    var negotiating = false;

    await msg.react(yesEmoji);
    await msg.react(noEmoji);

    const filter = (reaction, member) => {
      return (member.id === mention.id && reaction.emoji.name === yesEmoji) || (member.id === mention.id && reaction.emoji.name === noEmoji);
    };

    msg.awaitReactions(filter, { max: 1, time: 10000, errors: ['time'] })
      .then(collected => {
        const reaction = collected.first();
        if (reaction.emoji.name === yesEmoji) {
          negotiating = true;
          negotiate.channel.send('The mentioned user is okay to negotiate with you!');
        }
        else if (reaction.emoji.name === noEmoji) return negotiate.channel.send('The mentioned user is not okay to negotiate with you...')
      }).catch(err => {
        if(err) return message.channel.send(`${mention} did not react within the 10 seconds!`);
      })

所以首先我们得到了两个表情符号,我们希望用户做出反应。mention是我们提到的用户,msg是“是或否”问题,negotiating默认设置为假。起初,我们用表情符号对问题做出反应。在这个例子中我使用awaitReactions的是 ,因为它使用起来非常简单。为此,我们需要一个过滤器。在这种情况下,我也将变量命名为filterfilter检查反应是否是yesEmoji,或者反应noEmoji的用户是否是mention(我们提到的用户)。然后awaitReactions我们只需要1 个反应(是或否),我将时间设置为 10 秒,但您可以根据需要更改它。设置完成后awaitReactions,我们要收集我们的反应。这是在.then().collected给我们反应,我们只想要第一个,所以我们存储collected.first()reaction. 现在我们有了一个非常简单的 if-else 语句。如果反应的 emoji 是yesEmoji,negotiating将被设置为 true 并且一条消息被发送到通道中,否则它只会发送一条消息并返回。

negotiatingtrue当用户对 做出反应时才设置为很重要yesEmoji。在您的代码中,true即使什么也没发生,因为当您运行命令时,该命令代码中的所有内容都将被执行。你的最后一行是negotiating = true;. 我认为这不是你想做的。


推荐阅读