首页 > 解决方案 > 如何从自定义帮助命令中隐藏 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)

标签: pythondiscord.py

解决方案


如果您在命令顶部使用@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)

参考 :


推荐阅读