I'm trying to make a quiz through my discord bot where when I react to the first question it pushes in its respective array the number value and then shows the next question. I tried with a "for" but it shows all the questions without the reaction.
I made this code but when I react it shows this error:
UnhandledPromiseRejectionWarning: # (node:73631) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with.catch(). (rejection id: 1) (node:73631) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
How could I solve the error and show the next question when I react?
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++;
}
}
});
});
},
};
This is the solution:
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');
}
},
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.