Search code examples
javascriptdiscord.jscommando

How to fix discord.js-commando bot responding to unknown commands


I'm creating the client with the unknownCommandResponse property set to false:

const client = new CommandoClient({
  commandPrefix: '$',
  unknownCommandResponse: false,
  owner: '291048060845424640',
  disableEveryone: true
});

Yet when I try $kasopdkoakwdokapowkdo, it responds with:

Unknown command. Use $help or @Mysticonomy#2670 help to view the command list.

Solution

  • That was the right way to do it until the 18th of January: they decided to make the bot "unknown command" and "error" replies overridable, by allowing custom commands that will run instead.
    This change may not be well-documented yet, but has been pushed to the master branch with this commit by Gawdl3y. This topic comes from this issue, and is also listed in the "Done" column of the "Important stuff" project [link].

    If you want to make it work like in the past, you'll need to use a previous version; you won't be able to update the library to add new functionalities without updating this part of the code too.

    With this update, you can create a new command by extending the Command class (as usually) and then adding two properties set to true: unknown and hidden.
    If you want an example, you can look directly at the default unknown-command by the author of the change:

    module.exports = class UnknownCommandCommand extends Command {
      constructor(client) {
        super(client, {
          name: 'unknown-command',
          group: 'util',
          memberName: 'unknown-command',
          description: 'Displays help information for when an unknown command is used.',
          examples: ['unknown-command kickeverybodyever'],
          unknown: true,
          hidden: true
        });
      }
    
      run(msg) {
        return msg.reply(
          `Unknown command. Use ${msg.anyUsage(
                    'help',
                    msg.guild ? undefined : null,
                    msg.guild ? undefined : null
                )} to view the command list.`
        );
      }
    };
    

    Remember to avoid loading the default unknown-command: it'll be loaded by default by CommandoRegistry.registerDefaultCommands() unless you explicitly tell it not to do it.
    To avoid that, add unknownCommand: false to the options when you load those commands.

    client.registry.registerDefaultCommands({
      unknownCommand: false
    });