I've been working on a discord js bot. For some reason when my bot uses random responses like this, the application does not and does respond randomly.
I even tried having an array for responses but discord js gave me a hassle about that as well.
On top of this, for whatever reason, when I tried removing the catches next to the replies (the promise version) it would not work, and when it does the catch always fires.
8ball file:
const { SlashCommandBuilder } = require('discord.js');
const { logging } = require('../index')
module.exports = {
data: new SlashCommandBuilder()
.setName('8ball')
.setDescription('Ask me a question')
.addStringOption(opt => opt.setName('question').setDescription('question you have for meh').setRequired(true)),
async execute(interaction) {
logging.info(`Log:8ball command was called printing a random message`);
let randomnumber = Math.floor(Math.random() * 4);
if(interaction.options.getString('question').includes('?') === true) {
await interaction.reply({ content:'Sorry, but this command does not support question marks', ephemeral:true })
.catch(logging.error(`Log:A error occurred while trying to send error message in 8ball`))
return;
}
if (randomnumber === 1) {
log.info(`Log:A response 0 was selected, printing response 0`);
try {
await interaction.reply('Q: ' + interaction.options.getString('question') + '\nA: yes, i suppose');
}
catch {
await interaction.followUp('An error occurred while attempting to send the reply, please try again')
.catch(logging.error(`Log:A error occurred while trying to send error message `))
.then(logging.error( 'A error occurred while sending response 1, returning'))
return;
}
}
else if (randomnumber === 2) {
log.info(`Log:A response 2 was selected, printing response 2`);
try {
await interaction.reply('Q: ' + interaction.options.getString('question') + '\nA: maybe, unsure how should I know.');
}
catch {
await interaction.followUp('An error occurred while attempting to send the reply, please try again')
.catch(logging.error(`Log:A error occurred while trying to send error message `))
.then(logging.error( 'A error occurred while sending response 2, returning'))
return;
}
}
else if (randomnumber === 3) {
log.info(`Log:A response 3 was selected, printing response 3`);
try {
await interaction.reply('Q: ' + interaction.options.getString('question') + '\nA: definitely not, I am not sure why you would even ask');
}
catch {
await interaction.followUp('An error occurred while attempting to send the reply, please try again')
.catch(logging.error(`Log:A error occurred while trying to send error message `))
.then(logging.error( 'A error occurred while sending response 3, returning'))
return;
}
}
},
}
The other part is a function that prints responses if production mode is off:
const logging = {
info: function(msg) {
if(process.env.NODE_ENV === "production") {
return 0;
}
else {
log.info(msg);
}
return 0;
},
error: function(msg) {
if(process.env.NODE_ENV === "production") {
return 0;
}
else {
log.error(msg);
}
return 0;
}
}
I'm using nodeJS v16.17.0 discord.js v14 and Winston for logging
Edit:
I apologize for being very vague and for the large volume of code, the expected output of this function is supposed to do the following
- loads the question from the getString() function
- logs to a file (debug.log) that the command /8ball was called
- generates a random number
- replies with a response that changes based of the value of
randomnumber - logs if an error occurred with replying to the user
- logs that ccb replied successfully
- returns
but what the files do is this (in worse cases)
- logs that 8ball has been called
- sends
deferReply() - halts, does not write anything to debug.log, no error message, no success message, nothing, and stays on "CCB is thinking"
also: I am aware that there is no handler for randomnumber being 0, the reason for that is when I first created /8ball, the randomnumber variable never seemed to respond with 0 so I emitted it for performance
also keep in mind that each time production mode has not been on
thanks again in advance