tic-tac-toe循环中的Discord.js消息收集器
所以我开始使用discord.js制作一个discord机器人,它有一个tic-tac-toe命令。 我试着使用一个消息收集器,就像在while循环中这样。在它进入循环后,什么也没有发生。我添加了一个tic-tac-toe循环中的Discord.js消息收集器,discord.js,Discord.js,所以我开始使用discord.js制作一个discord机器人,它有一个tic-tac-toe命令。 我试着使用一个消息收集器,就像在while循环中这样。在它进入循环后,什么也没有发生。我添加了一个console.log,以查看totalRouds变量是否正在修改,它是否正在修改,因此循环以某种方式传递消息收集器代码,并仅在最后一行执行 while(numberOfrounds < 9){ const filter = m => m.author.id === mess
console.log
,以查看totalRouds变量是否正在修改,它是否正在修改,因此循环以某种方式传递消息收集器代码,并仅在最后一行执行
while(numberOfrounds < 9){
const filter = m => m.author.id === message.author.id;
message.reply('Enter a position:');
message.channel.awaitMessages(filter, {max: 1, time: 3000, errors: ['time']}).then(collected => {
// code for drawing a canvas
}).catch(err => {
console.log(err);
});
console.log(totalRouds);
numberOfrounds ++;
}
while(轮数<9){
const filter=m=>m.author.id==message.author.id;
message.reply('输入位置:');
message.channel.awaitMessages(过滤器,{max:1,时间:3000,错误:['time']})。然后(收集=>{
//绘制画布的代码
}).catch(错误=>{
控制台日志(err);
});
控制台日志(totalRouds);
numberOfrounds++;
}
.awaitMessages()
返回异步解析为收集的邮件数据的。这意味着,虽然在您开始收集消息后,您的最后一个log语句立即运行,但收集的消息仅可用,因此将在以后进行处理
为了说明这一点,让我稍微整理一下您的代码,并按照代码正常运行的顺序添加一些日志:
while (numberOfrounds < 9) {
console.log("1");
const filter = m => m.author.id === message.author.id;
message.reply('Enter a position:');
console.log("2");
message.channel.awaitMessages(
filter,
{ max: 1, time: 3000, errors: ['time'] }
).then(collected => {
console.log("5, after resolving, the collected data is only now avaiable");
}).catch(err => {
console.log(err);
});
console.log("3");
console.log(totalRouds);
numberOfrounds++;
console.log("4, immediately starts the next loop, back to #1 again");
}
但这可能仍然不是你想要的行为。为什么?您的bot将尝试一次发送所有9个回复(假设您的numberOfRounds
在0之前开始),此时Discord.js将自动批量发送这些回复,以避免对API进行垃圾邮件处理,并且所有消息收集器将同时等待。您可能打算“暂停”或暂停处理,直到.awaitMessages()
返回的Promise
解析,并且您已经完成了对返回数据的处理,从而在使用异步方法调用时导致同步行为(因此您说“循环以某种方式通过消息收集器代码并仅在最后一行执行”)。为此,我们可以使用:
/*
您尚未提供完整的代码,
因此,您需要做的是标记消息事件的
回调函数是异步的。
请参阅有关MDN的链接文章。
*/
while(轮数<9){
const filter=m=>m.author.id==message.author.id;
message.reply('输入位置:');
/*
然后,等待对方的承诺
这个承诺链需要解决
在恢复操作之前
然后进入下一个迭代。
*/
等待消息。频道。等待消息(
过滤器,
{max:1,时间:3000,错误:['time']}
)。然后(收集=>{
设totalRounds=1
控制台日志(totalRounds);
}).catch(错误=>{
控制台日志(err);
});
numberOfrounds++;
}
我的术语可能不是100%正确,但这是我的理解。如果可以改进,请随意评论
while (numberOfrounds < 9) {
const filter = m => m.author.id === message.author.id;
message.reply('Enter a position:');
message.channel.awaitMessages(
filter,
{ max: 1, time: 3000, errors: ['time'] }
).then(collected => {
let totalRounds = 1 // Do your processing here
console.log(totalRounds); // This should log as expected
}).catch(err => {
console.log(err);
});
numberOfrounds++;
}
/*
You have not provided your full code,
so what you need to do is mark your message event's
callback function as async.
Refer to the linked article on MDN.
*/
while (numberOfrounds < 9) {
const filter = m => m.author.id === message.author.id;
message.reply('Enter a position:');
/*
Then, wait for the Promise returned by
this promise chain to resolve
before resuming operation
and moving on to the next iteration.
*/
await message.channel.awaitMessages(
filter,
{ max: 1, time: 3000, errors: ['time'] }
).then(collected => {
let totalRounds = 1
console.log(totalRounds);
}).catch(err => {
console.log(err);
});
numberOfrounds++;
}