【问题标题】:discord.py rewrite/delete standard help commanddiscord.py 重写/删除标准帮助命令
【发布时间】:2021-04-17 09:05:39
【问题描述】:

我想创建一个自定义的 !help 命令,但我必须先禁用标准命令。当我这样做时:

client = commands.Bot(command_prefix='!')
client.remove_command('help')

或者

client = commands.Bot(command_prefix='!', help_command=None)

它仍然显示此消息: [1]:https://i.stack.imgur.com/Ut9dO.png 有大佬怎么删除吗?

【问题讨论】:

    标签: discord.py discord.py-rewrite


    【解决方案1】:

    随着 discord.py-rewrite 的最新变化,您无需删除标准帮助命令即可创建自己的帮助命令。要实现你想要的,你需要继承 HelpCommandMinimalHelpCommand,然后将其传递给 bot.help_command。

    以下代码显示了子类化MinimalHelpCommand的标准方式:

    class MyHelpCommand(commands.MinimalHelpCommand):
        def get_command_signature(self, command):
            return '{0.clean_prefix}{1.qualified_name} {1.signature}'.format(self, command)
    
    class MyCog(commands.Cog):
        def __init__(self, bot):
            self._original_help_command = bot.help_command
            bot.help_command = MyHelpCommand()
            bot.help_command.cog = self
    
        def cog_unload(self):
            self.bot.help_command = self._original_help_command
    

    按照上面的代码,您将获得您想要实现的行为,但如果您出于任何原因仍想禁用默认帮助命令,现在您可以将None 传递给help_command kwarg。

    更多信息,discord.py 文档: https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#help-commands

    【讨论】:

      【解决方案2】:

      创建帮助命令的更好方法是使用组。
      首先删除默认命令;

      client.remove_command("help")
      

      然后使用此方法发出帮助命令:

      @client.group(invoke_without_command= True)
      
      async def help(ctx):
          # insert your help command embed or message here
          embed= discord.Embed(title="Help", description="List of commands")
          embed.add_field(name="Moderation", value="!help moderation")
          embed.add_field(name="Misc", value="!help misc")
          await ctx.send(embed=embed)
      

      您已经完成了帮助命令,现在您必须为某个类别或命令提供帮助。你可以这样做:

      @help.command()
      
      async def moderation(ctx):
          embed= discord.Embed(title="Moderation", description="List of Moderation commands")
          embed.add_field(name="Commands", value="ban, kick, warn, mute, unban")
          await ctx.send(embed=embed)
      

      现在如果用户输入,!help moderation 上面的嵌入/消息将被发送。

      【讨论】:

      猜你喜欢
      • 2020-11-06
      • 2021-09-16
      • 2021-01-01
      • 2019-03-17
      • 2020-10-19
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      • 2020-03-31
      相关资源
      最近更新 更多