Issue with my Discord bot responding to multiple prefixes
I have configured my bot to use $ as the primary prefix, but it responds to other prefixes as well, such as !, >, @, #, &, and so on. This issue seems to be isolated to the command handler, as commands defined directly in the main file with if-else statements function without this problem.
Working example in the main.js file:
if (message.content.startsWith('$kick')) {
// this functions properly
}
Problematic command handler implementation:
if(command === 'joke') {
client.commands.get('joke').execute(message, args);
}
if(command === 'lock') {
client.commands.get('lock').execute(message, args);
}
if(command === 'roast') {
client.commands.get('roast').execute(message, args);
}
if(command === 'unlock') {
client.commands.get('unlock').execute(message, args);
}
if(command === 'mute') {
client.commands.get('mute').execute(message, args);
}
if(command === 'unmute') {
client.commands.get('unmute').execute(message, args);
}
Here’s a snippet from my main.js file:
const https = require('https');
const Discord = require('discord.js');
const client = new Discord.Client();
const fs = require('fs');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
client.on('message', async message => {
if (message.channel.type == 'dm') return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'joke') {
client.commands.get('joke').execute(message, args);
}
// other commands...
});
commandFiles.forEach(file => {
const command = file.split(/.js$/)[0];
client.commands.set(command, require(`./commands/${file}`));
});
Sample command file (joke.js):
const Discord = require('discord.js');
module.exports = {
name: 'joke',
description: 'tells a joke',
async execute(message, args) {
// joke execution logic here
}
};
I’m relatively new to JavaScript, and I would appreciate any assistance. Thank you!