我正在尝试制作一种琐事机器人,但问题是我无法让它工作。我有它,所以当你输入“-quiz”时,它会发送一个带有随机问题的嵌入。现在你可能会说我需要制作一个单独的 JSON 文件并将问题和答案放在那里,问题是,我需要这些字符串中的变量,当我尝试它时,它不起作用,因为顺序或类似的东西那。我试图解决这个问题,但无论如何这似乎是一个糟糕的解决方案。我设置了它,所以它在初始命令之后寻找一条消息,问题是它读取它自己的嵌入,老实说我不知道如何让它跳过机器人消息
client.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'quiz'){
var TypeID = (Math.floor(Math.random() * 11))
var toLog = (Math.floor(Math.random() * 2))
{ //there is something here that is used for the variables above but it is super long
}
var question = [`Question one`, `Question two`]
var answers = [[Answer1_1, Answer1_2],[Answer1_1]]
if(toLog === 0){
const quizEmbed1 = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('quiz')
.setDescription(`${question[0]}`)
message.channel.send(quizEmbed1)
if(!message.content || message.author.bot) return;
if(message.content === [answers[0], answers[1], answers[2], answers[3], answers[4], answers[5]]){
message.channel.send('Good Job! That is right!')
}else{
message.channel.send('Oops! that is wrong.')
}
}else if(toLog == 1){
const quizEmbed2 = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('quiz')
.setDescription(`${question[1]}`)
message.channel.send(quizEmbed2)
if(!message.content || message.author.bot) return;
if(message.content === [answers[6]]){
message.channel.send('Good Job! That is right!')
}else{
message.channel.send('Oops! That is wrong.')
}
}
}
});
如果有什么问题,那很可能是因为我改变了它以使其更小,我对 JavaScript 编码相当新
答案 0 :(得分:0)
尝试使用 MessageCollector。 discord.js guide
上有很好的指南或者使用 awaitMessages。还有一个关于 discord.js guide
的指南这是使用 awaitMessages 的示例
const filter = response => {
return item.answers.some(answer => answer.toLowerCase() === response.content.toLowerCase());
};
message.channel.send(item.question).then(() => {
message.channel.awaitMessages(filter, { max: 1, time: 30000, errors: ['time'] })
.then(collected => {
message.channel.send(`${collected.first().author} got the correct answer!`);
})
.catch(collected => {
message.channel.send('Looks like nobody got the answer this time.');
});
});