简体   繁体   English

用户反应后机器人确认未在 Discord.js 中显示

[英]Bot confirmation after user reacts not displaying in Discord.js

I want the user to answer a "yes or no" question using reactions.我希望用户使用反应来回答“是或否”的问题。 However, there is a bug in which when the tagged user reacts to the question, the bot is not sending a message on whether or not the tagged user wants to negotiate.但是,当被标记的用户对问题做出反应时,机器人不会发送关于被标记的用户是否想要协商的消息。 Here is my code below.下面是我的代码。

     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. 

UPDATE: I managed to get the bot to send a message whether the tagged user is ok or not ok to negotiate with the user that tagged them.更新:我设法让机器人发送一条消息,无论标记的用户是否可以与标记他们的用户协商。 Now there is an error in which is shown after 10 seconds (specified time).现在有一个错误,在 10 秒(指定时间)后显示。 Here is the updated code below:以下是更新后的代码:

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.')
        })
    }
})

I have a much easier solution to your problem:对于您的问题,我有一个更简单的解决方案:

    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!`);
      })

So first we got the two emojis, we want the user to react with.所以首先我们得到了两个表情符号,我们希望用户做出反应。 mention is our mentioned user, msg is the "yes or no" question and negotiating is set to false by default. mention是我们提到的用户, msg是“是或否”的问题, negotiating默认设置为假。 At first we react to the question with our emojis.起初,我们用表情符号对问题做出反应。 In this example I am using awaitReactions , because it is very simple to use.在此示例中,我使用awaitReactions ,因为它使用起来非常简单。 For this we need a filter.为此,我们需要一个过滤器。 In this case I named the variable also filter .在这种情况下,我也将变量命名为filter filter checks if the reaction wether is yesEmoji or noEmoji and if the user who reacted is mention (our mentioned user). filter检查反应是yesEmoji还是noEmoji以及反应的用户是否被mention (我们提到的用户)。 Then in awaitReactions we want only 1 reaction (yes or no), and I set the time to 10 seconds, but you can change it if you want.然后在awaitReactions中,我们只需要1 个反应(是或否),我将时间设置为 10 秒,但您可以根据需要更改它。 After awaitReactions is set up we want to collect our reaction.awaitReactions设置之后,我们想要收集我们的反应。 This is done in .then() .这是在.then()中完成的。 collected gives us the reactions, and we only want the first one, so we store collected.first() in reaction . collected给了我们反应,我们只想要第一个,所以我们将collected.first()存储在reaction中。 Now we have a really simple if-else statement.现在我们有了一个非常简单的 if-else 语句。 If the reacted emoji is yesEmoji , negotiating will be set to true and a message gets sent into the channel, otherwise it will only sent a message and return.如果反应的 emoji 是yesEmojinegotiating将被设置为 true 并且一条消息被发送到通道中,否则它只会发送一条消息并返回。

It is important to set negotiating only to true if the user reacted with yesEmoji .如果用户对yesEmoji做出反应,将negotiating设置为true很重要。 In your code it is true even if nothing happens, because as you run the command everything in that command code will be executed.在您的代码中,即使什么也没发生也是true ,因为当您运行命令时,该命令代码中的所有内容都将被执行。 and your last line there was negotiating = true;最后一行是negotiating = true; . . And I think that is not what you wanted to do.我认为这不是你想做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM