我如何使用异步函数在不和谐js使问题游戏?



我想做一个不和谐的机器人,它会问我一个问题,如果我在一个特定的时间内输入done,它应该说很好你赢了,如果它的答案,但如果它达到7秒,它应该说时间上升,但我不知道有时它垃圾邮件的频道时间上升,我怎么能修复,请我被困在那里8个小时?


/* === countries image === */
var   kurdistan = "https://upload.wikimedia.org/wikipedia/commons/thumb/3/35/Flag_of_Kurdistan.svg/800px-Flag_of_Kurdistan.svg.png";
var   catalonia = "https://upload.wikimedia.org/wikipedia/commons/thumb/c/ce/Flag_of_Catalonia.svg/810px-Flag_of_Catalonia.svg.png";
var   palastine = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Flag_of_Palestine.svg/800px-Flag_of_Palestine.svg.png";

/* === countries names === */
const obj = [
kurdistan,
catalonia,
palastine
];
/* === code === */

client.on('message', async message => {
if(message.author.bot) return;

const ranImg = Math.floor(Math.random() * obj.length); 
if(message.content === 'flag') {
const embed = new Discord.MessageEmbed();
embed.setTitle('Flag guesser');
embed.setDescription('which countries flag does this belong?');
embed.setImage(obj[ranImg]);
embed.setColor(0x3aeb34);
embed.setFooter('this game provided by by me');
embed.setTimestamp();
message.channel.send(embed).then(() => {
let filter = m => m.author.id === message.author.id;

message.channel.awaitMessages(filter, { max: 1, time: '7000', errors: ['time'] })
.then(collected => {  
//when it gives me the image and I write the correct answer of the obj[ranImg] it doesn't says you win all the time it says try again 👇🏽👇🏽👇🏽👇🏽 
if (collected.first().content === obj[ranImg]) {
return message.channel.send(':white_check_mark: You win!') //they won 
} else {
return message.channel.send(`:x: Oooh close! Try again!`); // they lost 
}
})
.catch(collected => {
return message.channel.send('times up! no answers were collected!');
});
});
}
}
);

消息收集器是困难的,而您是真正接近的。您不需要定义awaitMessages,而且await已经是异步的,因此您不需要在#awaitMessages之前键入async。此外,您需要一个#then来连接#awaitMessages

此外,如果抛出一个由errors: ['time']对象定义的错误,收集器也有#catch系统的概念。

maxMatches已过时,在v12中更改为max

最后,我将结合上述所有内容来修复您的代码:

client.on('message', async message => {
if(message.author.bot) return; //have this here so the command does as little as possible before returning
const ranImg = Math.floor(Math.random() * obj.length); 
if(message.content === 'flag') {
const embed = new Discord.MessageEmbed();
embed.setTitle('Flag guesser');
embed.setDescription('which countries flag does this belong?');
embed.setImage(obj[ranImg]);
embed.setColor(0x3aeb34);
embed.setFooter('this game provided by by me');
embed.setTimestamp();
message.channel.send(embed).then(() => {
let filter = m => m.author.id === message.author.id;
message.channel.awaitMessages(filter, { max: 1, time: '7000', errors: ['time'] })
.then(collected => {
if (collected.first().content === obj[ranImg]) {
return message.channel.send(':white_check_mark: You win!') //they won 
} else {
return message.channel.send(`:x: Oooh close! Try again!`); // they lost 
}
})
.catch(collected => {
return message.channel.send('times up! no answers were collected!'); // if the timer of 7000ms elapses, catch here
});
});
}
}
);

如果你还需要更多的帮助,这里有一个指南

最新更新