如何在discord.py中为命令参数添加描述?
为discord.py命令参数添加描述的方法
要给命令的参数添加描述,让帮助命令能显示参数说明,有两种常用方法,对应不同版本的discord.py:
方法一:使用@commands.param装饰器(v2.0+版本推荐)
直接在参数定义时通过commands.param指定描述,默认值也可以在这里设置,示例代码如下:
@bot.command(description='command do some stuff') async def somecommand( ctx: commands.Context, arg1: str = commands.param(description="需要拼接的第一个字符串"), arg2: str = commands.param(default="hello", description="需要拼接的第二个字符串,默认值为hello") ): await ctx.channel.send(arg1 + arg2)
方法二:手动设置params属性(兼容旧版本)
如果使用的是discord.py旧版本,可以在命令定义完成后,通过修改命令的params属性来添加参数描述:
@bot.command(description='command do some stuff') async def somecommand(ctx: commands.Context, arg1:str, arg2:str = "hello"): await ctx.channel.send(arg1+arg2) # 为参数添加描述 somecommand.params["arg1"].description = "需要拼接的第一个字符串" somecommand.params["arg2"].description = "需要拼接的第二个字符串,默认值为hello"
设置完成后,调用!help somecommand或对应前缀的帮助命令时,就能看到每个参数的详细描述了。
内容的提问来源于stack exchange,提问作者Glaurung
相关产品推荐
相关产品推荐

