Check if a command has the check "guild_only"

Viewed 1021

I'm trying to make a DM help command for my bot which is automatically generated depending on how many commands I add. I want it to omit commands with the guild_only check.

Here's my code:

import discord
from discord.ext import commands
...
...

# Imagine that in this case, `command` is a command with the `guild_only` check.
# This is in a much larger for loop
if commands.guild_only not in command.checks:
    # add command to string of commands
    help_c = ''.join([help_c, f"`{ctx.prefix}{command.name}{brief}`"
                              f"{separate_into_aliases(command)}\n{desc}\n\n"])

I assumed commands.guild_only not in command.checks would return False since the guild_only check is in the list of checks. But it didn't.

Here's the list the check attribute of a command with guild_only returns:

[<function guild_only.<locals>.predicate at 0x04A60150>]

And here's commands.guild_only:

<function guild_only at 0x0481F150>

The two are similar but not the same.

My question is how can I check if a command has the guild_only check in its checks attribute?

1 Answers

You can do something like

@commands.guild_only()
@commands.is_owner()
@commands.command()
async def show_checks(self, ctx):
    checks = list(map(lambda s: s.split('.')[0], map(lambda f: f.__qualname__, ctx.command.checks)))
    # printing checks gives ['is_owner', 'guild_only']
    checks = [s.split('.')[0] for s in [f.__qualname__ for f in ctx.command.checks]]
    # same result but shorter code

Related