Discord.js-通过awaitReactions方法获得多个反应来执行一个命令



我目前正在制作一个TicTacToe的小游戏,我的想法是制作它,而不是让频道不断植入要求用户下一步行动的内容,而是简单地基于反应,你可以从9个反应中选择1个(当然,如果其他玩家已经选择了,你就无法再选择它(。

我从来没有真正处理过需要多个反应的问题,因此我想请您帮助了解如何做到这一点,这样消息命令的执行就不是一次性的,而是会一直持续到最终获胜。

到目前为止,在我编写的代码中,这确实工作了2次,但后来它随机停止,不再工作。

此外,当我试图将一个点声明为x或圆时,该点将完全变为空白。

请帮忙!

到目前为止,我拥有的代码:https://sourceb.in/S7cayfoYjp

编辑:我现在还发现,机器人最初有点跳过整个awaitReactions代码。我使用了"console.log(I(",所以每次它循环时都会打印出"I",而且它似乎会立即打印出数字0-8,这意味着它没有正确地执行代码。

我认为最好使用的是reactionCollector。这是一个临时的反应监听器,附加在一条消息上。一个示例代码是:


const msg = await message.channel.send('tic tac toe test');
const acceptedEmojis = ['↖️', '⬆️', '↗️', '⬅️', '⏺️', '➡️', '↙️', '⬇️', '↘️']
const filter = (reaction, user) => {
return acceptedEmojis.includes(reaction.emoji.name) && user.id === turnId;
}
//here you create the collector. It has following attributes: it stops after 10 minutes or after 2 minutes of not collecting anything. 
const collector = msg.createReactionCollector(filter, { time: 600000, idle: 120000});
//here you start the listener
collector.on('collect', (reaction, user) => {
if (reaction.emoji.name === '↖️') {
//remove the reaction
await msg.reactions.resolve('↖️')
acceptedEmojis.splice(acceptedEmojis.indexOf('↖️'), 1);
//rest of your code...
} else if (reaction.emoji.name === '⬆️') {
...
}
});

最新更新