簡體   English   中英

用戶反應后機器人確認未在 Discord.js 中顯示

[英]Bot confirmation after user reacts not displaying in 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.')
        })
    }
})

對於您的問題,我有一個更簡單的解決方案:

    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 ,因為它使用起來非常簡單。 為此,我們需要一個過濾器。 在這種情況下,我也將變量命名為filter filter檢查反應是yesEmoji還是noEmoji以及反應的用戶是否被mention (我們提到的用戶)。 然后在awaitReactions中,我們只需要1 個反應(是或否),我將時間設置為 10 秒,但您可以根據需要更改它。 awaitReactions設置之后,我們想要收集我們的反應。 這是在.then()中完成的。 collected給了我們反應,我們只想要第一個,所以我們將collected.first()存儲在reaction中。 現在我們有了一個非常簡單的 if-else 語句。 如果反應的 emoji 是yesEmojinegotiating將被設置為 true 並且一條消息被發送到通道中,否則它只會發送一條消息並返回。

如果用戶對yesEmoji做出反應,將negotiating設置為true很重要。 在您的代碼中,即使什么也沒發生也是true ,因為當您運行命令時,該命令代碼中的所有內容都將被執行。 最后一行是negotiating = true; . 我認為這不是你想做的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM