Discord bot responds to various prefixes with discord.js

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!

Yeah, lucasg nailed it - you’re processing every message instead of checking the prefix first. But there’s another problem that’ll bite you later. You’re loading command files after setting up the message listener, which creates timing issues. Move that command loading loop before the message event handler so everything’s registered properly. Also, wrap your command execution in try-catch blocks since you’re using async operations. Trust me, I learned this the hard way when my bot kept crashing from unhandled promise rejections. Something like try { client.commands.get('joke').execute(message, args) } catch(err) { console.log(err) } will save you headaches.

You’re missing the prefix check in your message handler. You’re slicing with prefix.length and grabbing the command, but you never actually check if the message starts with your prefix. Just add if (!message.content.startsWith(prefix)) return; right after your DM check and before processing args. Without this, your bot tries to parse every single message as a command no matter what it starts with. I made the exact same mistake when I started - wasted hours debugging before I realized I was processing messages that didn’t even have the right prefix.

also check how ur defining the prefix. if it’s defined elsewhere, use the same var when checking. otherwise it’ll skip everything else. make sure each command handler only runs when the prefix matches.