discord.js-commando开发的Discord机器人Purge命令异常求助
Hey there, let's figure out why your !purge command is successfully deleting messages but still throwing that annoying "Unknown command" error! The root issue here is a conflict between the discord.js-commando framework's built-in command handling and the custom message event listener you've added.
What's Causing the Conflict?
Commando is designed to handle command registration and execution automatically—you don't need to write a raw message event to parse commands. When you send !purge, two things happen in sequence:
- Commando recognizes the command and runs your purge logic (hence the messages get deleted successfully).
- Your custom
messageevent fires next, checks if the command exists in your manual logic (which it doesn't, since you're relying on Commando for this), and spits out the "Unknown command" error.
How to Fix It
You have two straightforward options depending on how you want to structure your bot:
Option 1: Use Commando's Official Command Structure (Recommended)
This is the proper way to build commands with Commando, and it eliminates the conflict entirely. Replace your custom message event command handling with a dedicated Purge command class:
const commando = require('discord.js-commando'); const bot = new commando.Client({ commandPrefix: '!' // Set prefix directly in the Client constructor (Commando's standard approach) }); // Register command groups and default commands bot.registry.registerGroup('moderation', 'Moderation'); bot.registry.registerDefaults(); // Register commands from a dedicated folder (organizes your code better) bot.registry.registerCommandsIn(__dirname + '/commands'); // Example Purge command file (save as commands/moderation/purge.js) class PurgeCommand extends commando.Command { constructor(client) { super(client, { name: 'purge', group: 'moderation', memberName: 'purge', description: 'Deletes a specified number of messages', args: [ { key: 'amount', prompt: 'How many messages do you want to delete?', type: 'integer', min: 1, max: 100 } ], userPermissions: ['MANAGE_MESSAGES'], clientPermissions: ['MANAGE_MESSAGES'] }); } async run(message, { amount }) { // +1 to delete the original command message itself await message.channel.bulkDelete(amount + 1); const successMsg = await message.channel.send(`Successfully deleted ${amount} messages!`); // Auto-delete confirmation after 3 seconds setTimeout(() => successMsg.delete(), 3000); } } module.exports = PurgeCommand;
Option 2: Adjust Your Custom Message Event to Play Nice with Commando
If you need to keep the custom message event for other non-command logic, modify it to check if Commando recognizes the command before throwing an error:
const commando = require('discord.js-commando'); const bot = new commando.Client({ commandPrefix: '!' }); bot.on('message', async message => { // Ignore bots and messages without your prefix if (message.author.bot || !message.content.startsWith(bot.commandPrefix)) return; // Extract the command name (without the prefix) const commandName = message.content.slice(bot.commandPrefix.length).split(' ')[0].toLowerCase(); // Check if Commando has this command registered const registeredCommand = bot.registry.findCommands(commandName, false)[0]; if (registeredCommand) { // Let Commando handle the command—skip your custom error logic return; } // Your existing non-command logic goes here let msg = message.content.toUpperCase(); let sender = message.author; // ... rest of your custom code // Only send the unknown command error if Commando doesn't recognize it message.channel.send('Unknown command, do !help to view a list of commands'); });
Key Takeaway
Commando is an opinionated framework—let it do the heavy lifting for command handling instead of mixing raw Discord.js event logic with it. This will prevent weird conflicts like the one you're seeing and make your bot easier to maintain long-term.
内容的提问来源于stack exchange,提问作者Awesome Stickz

