python - 如何从自定义帮助命令中隐藏 Cogs/Commands
问题描述
我需要从嵌入自定义帮助命令中排除某些 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()
. 然后,对于每个命令,您可以找到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)
参考 :
推荐阅读
- ffmpeg - 通过 UTP 上的 USB 使用 FFmpeg 进行网络摄像头录制
- php - PHP 7.4:如何检查类型属性是否已初始化(包括使用 null 进行初始化)?
- mysql - SQL分组更多表
- node.js - 如何在 react.js 中获取这些注释数组的值?
- c++ - libssh 文件操作下载和上传速度慢
- javascript - 在不重新渲染子组件的情况下反应父组件的 setState
- python - 计算组内独特组合的权重
- javascript - 不必要的条件渲染不会在脚本标签版本 React 中完全重新渲染相同级别的元素
- autodesk-forge - onComplete 回调可以与 localhost 一起使用吗?
- git - 当我从特定提交(合并提交)创建分支时,一些较早的提交被删除