我需要将某些Cogs/Commands从嵌入到自定义帮助命令中排除,尽管我无法找到将它们从循环中排除的方法。
感谢您的帮助,谢谢
@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)
如果在命令顶部使用@has_permissions()
或@has_role()
装饰器,则只有通过检查后,它才会显示在默认帮助菜单中。
如果你想制作自己的自定义命令,你可以通过self.get_commands()
迭代所有的bot或cog命令。然后,对于每个命令,您都可以找到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)
参考文献:
- https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#checks
- https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.has_permissions
- https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.has_role
这应该会有所帮助:can_run(discord.py-docs(
go = False
try:
await command.can_run(self.context)
go = True
except:
go = False
在你的代码中实现这一点,应该很好(它对我有效(