简体   繁体   中英

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). 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. At first we react to the question with our emojis. In this example I am using awaitReactions , because it is very simple to use. For this we need a filter. In this case I named the variable also filter . filter checks if the reaction wether is yesEmoji or noEmoji and if the user who reacted is mention (our mentioned user). 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. After awaitReactions is set up we want to collect our reaction. This is done in .then() . collected gives us the reactions, and we only want the first one, so we store collected.first() in reaction . Now we have a really simple if-else statement. 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.

It is important to set negotiating only to true if the user reacted with yesEmoji . In your code it is true even if nothing happens, because as you run the command everything in that command code will be executed. and your last line there was negotiating = true; . And I think that is not what you wanted to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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