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

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-20 04:35:56

问题


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.

回答1:


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
});


来源:https://stackoverflow.com/questions/54838447/how-to-fix-discord-js-commando-bot-responding-to-unknown-commands

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!