discord.py中!help命令异常:扩展帮助出现重复参数描述及冗余区块
解决discord.py中!help命令重复显示参数的问题
问题原因
discord.py的默认HelpCommand会同时执行两个操作:
- 展示你在函数docstring中编写的全部内容
- 自动提取函数参数信息,生成一个独立的参数列表区块
你在docstring里手动编写了参数说明,同时默认命令又自动生成了参数列表,因此出现重复的“参数”区块;而自动生成的部分因为没识别到规范的参数描述格式,所以显示“无描述”。
解决方法
方法1:自定义HelpCommand,移除自动生成的参数区块
通过继承默认的HelpCommand,重写负责添加参数区块的方法,使其不再生成自动参数列表:
from discord.ext import commands class CustomHelpCommand(commands.HelpCommand): def add_arguments_section(self, command, embed=None): # 重写此方法,不添加自动生成的参数区块 pass # 将自定义帮助命令绑定到客户端 client.help_command = CustomHelpCommand()
修改后运行!help box_custom,只会显示你在docstring中编写的内容,不会出现重复的参数区块。
方法2:使用规范的docstring格式,让自动参数列表显示正确描述
discord.py支持解析Google风格或reStructuredText风格的docstring,可自动提取参数描述,既避免重复,也不会显示“无描述”。
Google风格示例:
@client.command() async def box_custom(ctx, command, *args): """允许为指定内置命令定义自定义命令。 示例:"!custom clear box_clear" 将添加名为!clear的新命令,该命令会调用box_clear函数! Args: command (str): 要添加的新命令名称。 args (List[str]): 原命令的额外参数。 """ # 执行相关逻辑 box_custom.brief = "为指定内置命令定义自定义命令。"
reStructuredText风格示例:
@client.command() async def box_custom(ctx, command, *args): """允许为指定内置命令定义自定义命令。 示例:"!custom clear box_clear" 将添加名为!clear的新命令,该命令会调用box_clear函数! :param command: 要添加的新命令名称。 :type command: str :param args: 原命令的额外参数。 :type args: List[str] """ # 执行相关逻辑 box_custom.brief = "为指定内置命令定义自定义命令。"
使用这类格式后,默认HelpCommand会自动解析参数描述,替换“无描述”占位符,同时你无需在docstring中手动编写重复的参数区块,彻底避免重复显示问题。
内容的提问来源于stack exchange,提问作者Mike - SMT
相关产品推荐
相关产品推荐

