简体   繁体   English

遇到正确反应时停止无限循环 discord.js

[英]Stop an infinite loop when correct reaction is met discord.js

I'm creating a discord bot command that gives a question and two emojis on the question's message for user to react.我正在创建一个 discord 机器人命令,该命令在问题的消息上给出一个问题和两个表情符号,供用户做出反应。 If you select the wrong emoji, the bot will tell you "Wrong", react again on the message, and wait for the user to react.如果您 select 错误的表情符号,机器人会告诉您“错误”,再次对消息做出反应,并等待用户做出反应。 I want to put this into an infinite loop that only stops when the user chooses the correct emoji.我想把它放到一个无限循环中,只有当用户选择正确的表情符号时才会停止。 This is what I have worked so far:这是我迄今为止所做的工作:

            message.channel.send(`Question?`).then(sentMessage => {
                sentMessage.react('✅');
                sentMessage.react('❌');

                const filter = (reaction, user) => {
                    return ['✅', '❌'].includes(reaction.emoji.name) && user.id != `${bid}`; // bid = bot's id
                };

                sentMessage.awaitReactions({ filter, max: 1, time: 60000, errors: ['time'] }).then(collected => {
                    var reaction = collected.first();
                    if (reaction.emoji.name === '❌') {
                        var check = true;
                        while (check) {
                            console.log('?');
                            message.channel.send('Wrong').then(sentMessage => {
                                sentMessage.react('✅');
                                sentMessage.react('❌');

                                const filter = (reaction, user) => {
                                    return ['✅', '❌'].includes(reaction.emoji.name) && user.id != `${bid}`;
                                };

                                sentMessage.awaitReactions({ filter, max: 1, time: 60000, errors: ['time'] }).then(collected => {
                                    var reaction = collected.first();

                                    if (reaction.emoji.name == '✅') check = false;
                                });
                            });
                        }
                    }
                });
            });

The idea is when the user finally reacts ✅ after first reacting some ❌, check will become false and the loop will stop.这个想法是当用户在第一次反应一些❌后最终反应✅时, check将变为false并且循环将停止。 However, after first choosing ❌, the while loop is not running any code inside it other than looping console.log('?') .然而,在第一次选择 ❌ 之后,while 循环除了循环console.log('?')之外没有运行任何代码。 Can someone point out where I did wrong?有人能指出我哪里做错了吗?

In your code, you send Wrong and put the reaction check logic inside then and wrap with while .在您的代码中,您发送Wrong并将反应检查逻辑放入then并用while包装。 However, since Channel.send returns a Promise, it will end almost instantly, continue the loop while doing Channel.send and reaction logic simultaneously.然而,由于Channel.send返回一个 Promise,它几乎会立即结束,继续循环同时执行Channel.send和反应逻辑。 You can use an async function and call it recursively.您可以使用异步 function 并递归调用它。 So the code would be this:所以代码是这样的:

message.channel.send(`Question?`).then(sentMessage => {
                sentMessage.react('✅');
                sentMessage.react('❌');

                const filter = (reaction, user) => {
                    return ['✅', '❌'].includes(reaction.emoji.name) && user.id != `${bid}`; // bid = bot's id
                };

                sentMessage.awaitReactions({ filter, max: 1, time: 60000, errors: ['time'] }).then(collected => {
                    var reaction = collected.first();
                    if (reaction.emoji.name === '❌') {
                        async function reactionLoop() {
                            console.log('?');
 
                            let sentMessage = await message.channel.send('Wrong');
                            await sentMessage.react('✅');
                            await sentMessage.react('❌');

                            const filter = (reaction, user) => ['✅', '❌'].includes(reaction.emoji.name) && user.id != `${bid}`;
                            let collected = await sentMessage.awaitReactions({ filter, max: 1, time: 60000, errors: ['time'] })
                            var reaction = collected.first();

                            if (reaction.emoji.name == '✅') await reactionLoop();                
                        }
                        reactionLoop();
                    }
                });
            });

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

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