Hey everyone! I’m new to JavaScript and I’m trying to build a Discord bot using DiscordJS. I’ve got a problem that’s driving me crazy.
I made a system to load commands from a folder and sort them by category. But when I try to use a command, the bot says it doesn’t exist. Here’s what I’ve done:
global.commandTypes = {
user: {},
mod: {},
admin: {},
owner: {}
};
function loadCommands() {
const cmdFiles = fs.readdirSync('./cmds/');
cmdFiles.forEach(file => {
const cmd = require(`./cmds/${file}`);
const cmdName = file.split('.')[0];
const category = cmd.info.category.toLowerCase();
if (commandTypes[category]) {
commandTypes[category][cmdName] = cmd;
} else {
console.warn(`Can't add ${cmdName} to any category`);
}
});
console.log(`Loaded ${cmdFiles.length} commands`);
}
function handleMessage(msg) {
if (msg.author.bot || !msg.content.startsWith('!')) return;
const args = msg.content.slice(1).split(' ');
const cmd = args.shift().toLowerCase();
for (const type in commandTypes) {
if (cmd in commandTypes[type]) {
return msg.reply(`Command type: ${type}`);
}
}
msg.reply('Command not found. Try !help for a list of commands.');
}
What am I doing wrong? Any help would be awesome!
I’ve encountered similar issues when building Discord bots. From what I can see, your command loading logic looks solid, but the problem might be in how you’re executing the commands.
One thing to check is if the loadCommands()
function is actually being called before you start handling messages. Make sure it’s invoked when your bot initializes.
Also, your handleMessage
function is only replying with the command type, not actually executing the command. You might want to modify it like this:
if (cmd in commandTypes[type]) {
commandTypes[type][cmd].execute(msg, args);
return;
}
This assumes each command has an execute
method. If not, you’ll need to adjust accordingly.
Lastly, double-check your file structure. Ensure your command files are indeed in the ‘./cmds/’ directory and follow the expected format.
Hope this helps! Let me know if you need any further clarification.
hey tom, u might wanna check if ur command files are exported correctly. make sure each file has something like module.exports = { execute: (msg, args) => { /* command logic */ }, info: { category: 'user' } };
at the end. also, double-check if the categories in ur files match the ones in commandTypes
. goodluck!
I’ve dealt with similar issues in my Discord bot projects. One thing that stands out is your command execution. You’re only replying with the command type, not actually running the command.
Try modifying your handleMessage function like this:
function handleMessage(msg) {
if (msg.author.bot || !msg.content.startsWith('!')) return;
const args = msg.content.slice(1).split(' ');
const cmd = args.shift().toLowerCase();
for (const type in commandTypes) {
if (cmd in commandTypes[type]) {
return commandTypes[type][cmd].execute(msg, args);
}
}
msg.reply('Command not found. Try !help for a list of commands.');
}
This assumes your command files export an execute function. If they don’t, you’ll need to adjust your command structure. Also, make sure loadCommands() is called before you start handling messages. Let me know if this helps!