I've been working on a Discord bot that triggers a response when a specific word is mentioned. Originally, I had it set up to say only one thing (you can check out the original post here), but now I want the bot to have multiple responses. To achieve this, I implemented a switch statement with a random number generator function that generates a number used in the switch statement for selecting the message output. However, my code doesn't seem to be working, and I'm unsure why.
let Discord;
let Database;
if(typeof window !== "undefined"){
Discord = DiscordJS;
Database = EasyDatabase;
} else {
Discord = require("discord.js");
Database = require("easy-json-database");
}
const delay = (ms) => new Promise((resolve) => setTimeout(() => resolve(), ms));
const s4d = {
Discord,
client: null,
tokenInvalid: false,
reply: null,
joiningMember: null,
database: new Database("./db.json"),
checkMessageExists() {
if (!s4d.client) throw new Error('You cannot perform message operations without a Discord.js client')
if (!s4d.client.readyTimestamp) throw new Error('You cannot perform message operations while the bot is not connected to the Discord API')
}
};
s4d.client = new s4d.Discord.Client({
fetchAllMembers: true
});
s4d.client.on('raw', async (packet) => {
if(['MESSAGE_REACTION_ADD', 'MESSAGE_REACTION_REMOVE'].includes(packet.t)){
const guild = s4d.client.guilds.cache.get(packet.d.guild_id);
if(!guild) return;
const member = guild.members.cache.get(packet.d.user_id) || guild.members.fetch(d.user_id).catch(() => {});
if(!member) return;
const channel = s4d.client.channels.cache.get(packet.d.channel_id);
if(!channel) return;
const message = channel.messages.cache.get(packet.d.message_id) || await channel.messages.fetch(packet.d.message_id).catch(() => {});
if(!message) return;
s4d.client.emit(packet.t, guild, channel, message, member, packet.d.emoji.name);
}
});
s4d.client.login('TOKEN').catch((e) => { s4d.tokenInvalid = true; s4d.tokenError = e; });
function GetRandomInteger(a, b){
if (a > b){
small = b;
large = a;
}
else{
small = a;
large = b;
}
s4d.client.on('message', async (s4dmessage) => {
if (s4dmessage.author.bot) return;
if (s4dmessage.content.includes('apples')) {
switch(GetRandomInteger(1,5)){
case 1: s4dmessage.channel.send(String('yum I love apples!')); break;
case 2: s4dmessage.channel.send(String('apples are the best')); break;
case 3: s4dmessage.channel.send(String('did someone say apples?')); break;
case 4: s4dmessage.channel.send(String('apples are great!')); break;
case 5: s4dmessage.channel.send(String('yum yum!')); break;
}
}
});
s4d;