[英]How can I show the next questions in my quiz bot and fix the error?
我正在尝试通过我的 discord 机器人进行测验,当我对第一个问题做出反应时,它会在其各自的数组中推送数值,然后显示下一个问题。 我尝试使用“for”,但它显示所有问题而没有反应。
我制作了这段代码,但是当我做出反应时,它显示了这个错误:
UnhandledPromiseRejectionWarning: # (node:73631) UnhandledPromiseRejectionWarning: 未处理的 promise 拒绝。 此错误源于在没有 catch 块的情况下抛出异步 function 内部,或拒绝未使用.catch() 处理的 promise。 (拒绝 ID:1)(节点:73631)[DEP0018] DeprecationWarning:不推荐使用未处理的 promise 拒绝。 将来,未处理的 promise 拒绝将使用非零退出代码终止 Node.js 进程。
我如何解决错误并在做出反应时显示下一个问题?
const Discord = require('discord.js');
const Client = require('../src/structures/client');
module.exports = {
name: `quiz`,
aliases: 'q',
/**
* @param {Client} client
* @param {Message} message
* @param {String[]} args
*/
run: async (client, message, args) => {
const pregunta = [
'Do you smoke?',
'Do you drink alcohol?',
'Do you read books?',
];
const respuestas = `1 ) Nunca
2 ) Casi nunca
3 ) A veces
4 ) Usualmente
5 ) Siempre`;
const time = 10;
var autoconciencia = []; //0.4.8.11.14.
var autogestion = []; //2.5.9.12.17.
var concienciasocial = []; //3.6.13.16.18.
var relaciones = []; //1.7.10.15.19.
var i = 0;
function pusharrays(reaction, array) {
switch (reaction) {
case '1️⃣':
array.push(1);
break;
case '2️⃣':
array.push(2);
break;
case '3️⃣':
array.push(3);
break;
case '4️⃣':
array.push(4);
break;
case '5️⃣':
array.push(5);
break;
}
}
message.channel
.send(
new Discord.MessageEmbed()
.setAuthor(`Question: ${pregunta[i]}`, message.client.user.avatarURL)
.setDescription(`${respuestas}`)
.setColor('#ffdb26')
)
.then(async (msg) => {
await msg.react('1️⃣');
await msg.react('2️⃣');
await msg.react('3️⃣');
await msg.react('4️⃣');
await msg.react('5️⃣');
const filter = (reaction, user) => {
return (
['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣'].includes(reaction.emoji.name) && !user.bot
);
};
msg
.awaitReactions(filter, {
max: 1,
time: time * 1000,
errors: ['time'],
})
.then((collected) => {
var reaction = collected.first().emoji.name;
var challanger = collected.first().users.cache.last();
console.log(reaction);
if (reaction) {
if (i == 0 || i == 4 || i == 8 || i == 11 || i == 14) {
pusharrays(reaction, autoconciencia);
console.log('array_1: ' + autoconciencia);
i++;
} else if (i == 2 || i == 5 || i == 9 || i == 12 || i == 17) {
pusharrays(reaction, autogestion);
console.log('array_2: ' + autogestion);
i++;
} else if (i == 3 || i == 6 || i == 13 || i == 16 || i == 18) {
pusharrays(reaction, concienciasocial);
console.log('array_3: ' + concienciasocial);
i++;
} else if (i == 1 || i == 7 || i == 10 || i == 15 || i == 19) {
pusharrays(reaction, relaciones);
console.log('array_4: ' + relaciones);
i++;
}
}
});
});
},
};
这是解决方案:
const Discord = require('discord.js');
const Client = require('../src/structures/client');
module.exports = {
name: `quiz`,
aliases: 'q',
/**
* @param {Client} client
* @param {Message} message
* @param {String[]} args
*/
run: async (client, message, args) => {
const preguntas = [
'Do you smoke?',
'Do you drink alcohol?',
'Do you read books?',
];
const respuestas = `1 ) Nunca
2 ) Casi nunca
3 ) A veces
4 ) Usualmente
5 ) Siempre`;
const time = 10;
var autoconciencia = []; //0.4.8.11.14.
var autogestion = []; //2.5.9.12.17.
var concienciasocial = []; //3.6.13.16.18.
var relaciones = []; //1.7.10.15.19.
var i = 0;
function pusharrays(reaction, array) {
switch (reaction) {
case '1️⃣':
array.push(1);
break;
case '2️⃣':
array.push(2);
break;
case '3️⃣':
array.push(3);
break;
case '4️⃣':
array.push(4);
break;
case '5️⃣':
array.push(5);
break;
}
}
nextquestion(i);
function nextquestion(i) {
if (i < preguntas.length) {
message.channel
.send(
new Discord.MessageEmbed()
.setAuthor(`Question: ${preguntas[i]}`, message.client.user.avatarURL)
.setDescription(`${respuestas}`)
.setColor('#ffdb26')
)
.then(async (msg) => {
await msg.react('1️⃣');
await msg.react('2️⃣');
await msg.react('3️⃣');
await msg.react('4️⃣');
await msg.react('5️⃣');
const filter = (reaction, user) => {
return (
['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣'].includes(reaction.emoji.name) &&
!user.bot
);
};
msg
.awaitReactions(filter, {
max: 1,
time: time * 1000,
errors: ['time'],
})
.then((collected) => {
var reaction = collected.first().emoji.name;
var challanger = collected.first().users.cache.last();
console.log(reaction);
if (reaction) {
if (i == 0 || i == 4 || i == 8 || i == 11 || i == 14) {
pusharrays(reaction, autoconciencia);
console.log('array_1: ' + autoconciencia);
i++;
nextquestion(i);
} else if (i == 2 || i == 5 || i == 9 || i == 12 || i == 17) {
pusharrays(reaction, autogestion);
console.log('array_2: ' + autogestion);
i++;
nextquestion(i);
} else if (i == 3 || i == 6 || i == 13 || i == 16 || i == 18) {
pusharrays(reaction, concienciasocial);
console.log('array_3: ' + concienciasocial);
i++;
nextquestion(i);
} else if (i == 1 || i == 7 || i == 10 || i == 15 || i == 19) {
pusharrays(reaction, relaciones);
console.log('array_4: ' + relaciones);
i++;
nextquestion(i);
}
}
});
});
} else console.log('FIN CUESTIONARIO');
}
},
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.