我需要从嵌入自定义帮助命令中排除某些 Cogs/命令,但我无法找到将它们从循环中排除的方法。
感谢任何帮助,谢谢!
@commands.command(name="help")
async def help(self, context):
"""
List all commands from every Cog the bot has loaded.
"""
prefix = config.BOT_PREFIX
if not isinstance(prefix, str):
prefix = prefix[0]
embed = discord.Embed(title="Help", description="List of available commands:", color=config.success)
for i in self.bot.cogs:
cog = self.bot.get_cog(i.lower())
if cog != "owner":
commands = cog.get_commands()
command_list = [command.name for command in commands]
command_description = [command.help for command in commands]
help_text = '\n'.join(f'{prefix}{n} - {h}' for n, h in zip(command_list, command_description))
embed.add_field(name=i.capitalize(), value=f'```{help_text}```', inline=False)
await context.send(embed=embed)
答案 0 :(得分:0)
如果您在命令顶部使用 @has_permissions()
或 @has_role()
装饰器,则只有通过该检查后,它才会显示在默认帮助菜单中。
如果您想创建自己的自定义命令,可以通过 self.get_commands()
遍历所有机器人或齿轮命令。然后对于每个命令,您可以找到 command.checks
,它返回添加到您的命令的所有检查的列表(has_permissions、has_role 或您的自定义检查)。然后你可以使用这些检查(它们是一个函数)来检查消息的作者是否全部通过了
此代码发送一个嵌入,其中包含作者可以使用的所有命令
@commands.command()
def help(self, ctx: Context):
embed = Embed()
embed.title = f"Admin commands of {self.qualified_name}"
for command in self.get_commands():
name = command.name
description = command.description
passes_check = True
for check in command.checks:
if not check(ctx.author):
passes_check = False
break
if passes_check:
embed.add_field(name=name, value=description, inline=False)
await ctx.send(embed=embed)
参考资料: