'Help' 命令使用此函数自动为 commands.group 子命令生成帮助
an 'Help' commands that auto generate a help for a commands.group subcommand using this function
我试着做一个帮助命令,但我不知道如何为命令组创建帮助命令,这是主命令与组分开的,我试过help modmail channel
modmail 是组,channel 是子命令,它只是说命令不存在
这是函数:
Please note that, I'm not asking how to use the commands.group, I'm asking about how can I use my function to create a help for Subcommands on commands.group()
async def cmdhelp(self, ctx, command):
params = []
for key, value in command.params.items():
if key not in ("self", "ctx"):
params.append(f"[{key}]" if "NoneType" in str(value) else f"<{key}>")
params = " ".join(params)
alias = ", ".join(command.aliases)
commandhelp = command.help
commanddesc = command.description
if not command.help:
commandhelp = "`None`"
if not command.description:
commanddesc = "`None`"
if not command.aliases:
alias = "`None`"
embed = discord.Embed(title=f"Help for {command}",
colour=0x59FFE7,
description=f"**Description:**\n{commandhelp}\n**Usage:**\n`{command} {params}`\n**Aliases:**\n`{alias}`\n**Permission:**\n`{commanddesc}`")
await ctx.send(embed=embed)
@commands.group(invoke_without_command=True)
async def help(self, ctx, *,cmd=None):
if cmd is None:
# lets say this is a embed with a help category list
else:
if (command := get(self.bot.commands, name=cmd)):
await self.cmdhelp(ctx, command)
else:
await ctx.send("That command does not exist.")
例子:
如果你看到了,它可以与 help modmail
的正常命令一起使用,但是我如何为 modmail 组的 子命令创建?这是 help modmail channel
首先,这不是使用子命令的正确方法。这是如何使用它们:
client.remove_command("help")
@client.group()
async def main_cmd(ctx):
print("main command")
@main_cmd.command()
async def sub_cmd(ctx):
print("subcommand")
在 discord 中说 main_cmd
只会打印 "main command"
,但在 discord 中说 main_cmd sub_cmd
会打印 "main command"
然后是 "subcommand"
.
如果调用子命令时不希望原始命令 运行,请使用 ctx.invoked_subcommand
:
client.remove_command("help")
@client.group()
async def main_cmd(ctx):
if ctx.invoked_subcommand != None:
return
print("main command")
@main_cmd.command()
async def sub_cmd(ctx):
print("subcommand")
编辑(编辑问题后):
要为机器人中的每个命令创建“命令”(它不会真的是命令),请使用 bot.commands
:
client.remove_command("help")
@client.command(description = "THIS IS THE MESSAGE THAT WILL APPEAR IN THE SPECIFIC HELP COMMAND")
async def a_command(ctx):
pass #look at the decorator
@client.command()
async def help(ctx, cmd = None):
if cmd == None:
await ctx.send("DEFAULT HELP")
elif cmd in (cmds := {command.name: command for command in client.commands}):
await ctx.send("cmd: " + cmds[cmd].description)
else:
await ctx.send("command not found")
这会为机器人中的每个命令创建“子命令”。
我试着做一个帮助命令,但我不知道如何为命令组创建帮助命令,这是主命令与组分开的,我试过help modmail channel
modmail 是组,channel 是子命令,它只是说命令不存在
这是函数:
Please note that, I'm not asking how to use the commands.group, I'm asking about how can I use my function to create a help for Subcommands on commands.group()
async def cmdhelp(self, ctx, command):
params = []
for key, value in command.params.items():
if key not in ("self", "ctx"):
params.append(f"[{key}]" if "NoneType" in str(value) else f"<{key}>")
params = " ".join(params)
alias = ", ".join(command.aliases)
commandhelp = command.help
commanddesc = command.description
if not command.help:
commandhelp = "`None`"
if not command.description:
commanddesc = "`None`"
if not command.aliases:
alias = "`None`"
embed = discord.Embed(title=f"Help for {command}",
colour=0x59FFE7,
description=f"**Description:**\n{commandhelp}\n**Usage:**\n`{command} {params}`\n**Aliases:**\n`{alias}`\n**Permission:**\n`{commanddesc}`")
await ctx.send(embed=embed)
@commands.group(invoke_without_command=True)
async def help(self, ctx, *,cmd=None):
if cmd is None:
# lets say this is a embed with a help category list
else:
if (command := get(self.bot.commands, name=cmd)):
await self.cmdhelp(ctx, command)
else:
await ctx.send("That command does not exist.")
例子:
help modmail
的正常命令一起使用,但是我如何为 modmail 组的 子命令创建?这是 help modmail channel
首先,这不是使用子命令的正确方法。这是如何使用它们:
client.remove_command("help")
@client.group()
async def main_cmd(ctx):
print("main command")
@main_cmd.command()
async def sub_cmd(ctx):
print("subcommand")
在 discord 中说 main_cmd
只会打印 "main command"
,但在 discord 中说 main_cmd sub_cmd
会打印 "main command"
然后是 "subcommand"
.
如果调用子命令时不希望原始命令 运行,请使用 ctx.invoked_subcommand
:
client.remove_command("help")
@client.group()
async def main_cmd(ctx):
if ctx.invoked_subcommand != None:
return
print("main command")
@main_cmd.command()
async def sub_cmd(ctx):
print("subcommand")
编辑(编辑问题后):
要为机器人中的每个命令创建“命令”(它不会真的是命令),请使用 bot.commands
:
client.remove_command("help")
@client.command(description = "THIS IS THE MESSAGE THAT WILL APPEAR IN THE SPECIFIC HELP COMMAND")
async def a_command(ctx):
pass #look at the decorator
@client.command()
async def help(ctx, cmd = None):
if cmd == None:
await ctx.send("DEFAULT HELP")
elif cmd in (cmds := {command.name: command for command in client.commands}):
await ctx.send("cmd: " + cmds[cmd].description)
else:
await ctx.send("command not found")
这会为机器人中的每个命令创建“子命令”。