You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

discord.js-commando开发的Discord机器人Purge命令异常求助

Fixing the "Unknown Command" Error for Working !purge Command in Discord.js-Commando

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:

  1. Commando recognizes the command and runs your purge logic (hence the messages get deleted successfully).
  2. Your custom message event 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:

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:52:54