I’m working on a Discord bot using discord.js-commando and I need help with setting up a custom prefix. The bot works fine when I don’t use command groups, but I’m having trouble getting the prefix to work properly when I organize commands into groups.
Main bot file:
const { CommandoClient } = require('discord.js-commando');
const client = new CommandoClient();
const botPrefix = "$";
client.registry.registerGroup('games', 'Gaming Commands');
client.registry.registerCommandsIn(__dirname + "/cmd");
client.login('YOUR_BOT_TOKEN');
Command in group:
const { Command } = require('discord.js-commando');
class CoinFlipCommand extends Command {
constructor(client) {
super(client, {
name: 'flip',
group: 'games',
memberName: 'flip',
description: 'Flip a coin'
});
}
async run(message, args) {
const result = Math.random() < 0.5 ? 'Heads' : 'Tails';
message.reply(`The coin landed on ${result}!`);
}
}
module.exports = CoinFlipCommand;
What am I missing to make the custom prefix work with grouped commands?
classic mistake! you set up the botPrefix variable but didn’t tell commando to use it. just add commandPrefix: botPrefix to your CommandoClient options and you’re set. happens all the time when you’re starting out with commando.
You’re defining the prefix variable but not actually using it. The CommandoClient needs the prefix passed in when you create it. Fix your main bot file like this:
const { CommandoClient } = require('discord.js-commando');
const client = new CommandoClient({
commandPrefix: '$'
});
client.registry.registerGroup('games', 'Gaming Commands');
client.registry.registerCommandsIn(__dirname + "/cmd");
client.login('YOUR_BOT_TOKEN');
The commandPrefix property is what sets the prefix for all commands. Your command structure looks fine, so once you add this it should work with $flip.
You didn’t pass the botPrefix to the CommandoClient constructor. You declared it but never actually used it in the client config. Here’s the fix:
const { CommandoClient } = require('discord.js-commando');
const botPrefix = "$";
const client = new CommandoClient({
commandPrefix: botPrefix,
owner: 'YOUR_USER_ID' // optional but recommended
});
I ran into this same thing when I started with commando. The framework needs the prefix explicitly set in the client options or it just uses the default behavior and ignores your custom prefix completely. Your command group and individual command setup look fine, so this should fix it right away.