Skip to content

Instantly share code, notes, and snippets.

@Painezor
Last active April 18, 2024 05:22
Show Gist options
  • Save Painezor/eb2519022cd2c907b56624105f94b190 to your computer and use it in GitHub Desktop.
Save Painezor/eb2519022cd2c907b56624105f94b190 to your computer and use it in GitHub Desktop.
Built-in Checks for the commands extension of discord py
@commands.guild_only()
# Command cannot be used in private messages.
@commands.dm_only()
# Command can only be used in private messages.
@commands.is_owner()
# Command can only be used by the bot owner.
@commands.is_nsfw()
# Command can only be used in NSFW channels
@commands.has_role("name")
# Check if member has a role with the name "name"
@commands.bot_has_role(11132312313213)
# As above, but for the bot itself. (name can be replaced with id)
@commands.has_any_role("role1","foo",11132312313213)
# Check if user has any of the roles with the names "role1", "foo", or the role with id 11132312313213
@commands.bot_has_any_role(*roles)
# As above, but for the bot itself
@commands.has_permissions(ban_members=True, kick_members=True)
# Check if user has all of the passed permissions
# e.g. this command will require both kick and ban permissions
@commands.bot_has_permissions(**perms)
# As above, but for the bot itself.
@commands.has_guild_permissions(**perms)
@commands.bot_has_guild_permissions(**perms)
# As for the two above, but for guild permissions rather than channel permissions.
@commands.check(myfunction)
# Check against your own function that returns those able to use your command
@commands.check_any(*myfunctions)
# Command will be ran if the conditions of any of your own check functions are met
from discord.ext.commands.cooldowns import BucketType
# BucketType can be BucketType.default, member, user, guild, role, or channel
@commands.cooldown(rate,per,BucketType)
# Limit how often a command can be used, (num per, seconds, BucketType)
@commands.max_concurrency(number, per=BucketType.default, *, wait=False)
# Limit how many instances of the command can be running at the same time.
# Setting wait=True will queue up additional commands. False will raise MaxConcurrencyReached
# Checks can be stacked, and will Raise a CheckFailure if any check fails.
@ShorelineMafia
Copy link

Is there a way to make it for user IDs instead of role IDs?

@AwSkies
Copy link

AwSkies commented Aug 7, 2020

Make what for user IDs instead of role IDs? Your question wasn't very specific.

@ShorelineMafia
Copy link

Make what for user IDs instead of role IDs? Your question wasn't very specific.

Yeah I mean like @commands.has_role making it so you need to have the role name/role ID. Is there a way to make it so the commands only work on specific user IDs?

@AwSkies
Copy link

AwSkies commented Aug 8, 2020

From what I can tell, no, however, within a command you can put in some code that checks the user IDs:

@commands.command
async def my_command(ctx):
    allowed_IDs = [123456789, 987654321, ...]
    if ctx.author.id in allowed_IDs:
        #put your command content here
        ...
    else:
        #stuff to do if user is not authorized
        ...

@ShorelineMafia
Copy link

ShorelineMafia commented Aug 9, 2020

Nevermind I fixed my issue. Thank you for telling me how to check the user IDs

@choppa1337
Copy link

Hey cap, I have a question regarding what you and shoreline were talking about. Could you drop your discord? I’d prefer to discuss it over discord and not having to conversate it over a github forum

@Nanofaux
Copy link

They're talking about a check but only certain users can run it. I'd personally make it its own predicate, so it can be used anywhere and allows you to pass in your own IDs, of course.

@choppa1337
Copy link

I know but my question was going to be whether if it’s possible to code a command that sends the role ID to a whitelist.json or txt so you don’t have to add each individual role ID in the code itself. So instead of opening up your editor and adding onto “ allowed_IDs = [123456789, 987654321, ...]” you could add a command where the usage would be “(prefix)whitelist add (roleID). Sorry if this doesn’t make much sense I did the best I could to explain it.

@AwSkies
Copy link

AwSkies commented Aug 15, 2020

@choppa1337 my discord is CaptainClumsy#3018. I'm pretty much always active other than at night.
You can accomplish something like this using the json python module, and you can contact me on discord for help with.

@AwSkies
Copy link

AwSkies commented Aug 15, 2020

Also @Nanofaux that's a good idea lol, I kind of forgot about defining your own predicate.

@Nanofaux
Copy link

@choppa1337 Yeah something like that would be possible

@george2781
Copy link

george2781 commented Aug 21, 2020

Is it possible to make a message send if the @commands.is_owner() check fails? and if so how

@AwSkies
Copy link

AwSkies commented Aug 21, 2020

@george2781 Yes, that's possible. After defining your command, define an error handler that checks if the error raised is commands.NotOwner:

@my_command_name.on_error
async def my_command_name_error_handler(ctx, error):
    if isinstance(error, commands.NotOwner:
        ctx.send('You are not the owner')

Copy link

ghost commented Aug 23, 2020

Does the cool down apply to rewrite?

yes

@Parzivel-1
Copy link

Parzivel-1 commented Nov 17, 2020

How to check if the user who raised the command is a specific user, not about roles or permissions?
Same about guild, how to check if the command is raised in a specific guild?

@AwSkies
Copy link

AwSkies commented Nov 17, 2020

@Parzivel-1
Copy link

For that, you can write your own custom check: https://discordpy.readthedocs.io/en/latest/ext/commands/api.html?highlight=checks#checks

Thanks so much! Have been looking for hours for an answer!

@DuckMasterAl
Copy link

DuckMasterAl commented Dec 8, 2020

@Painezor, you should add @commands.dm_only() to the list.

@Painezor
Copy link
Author

@Painezor, you should add @commands.dm_only() to the list.

Done.

@MPolygon
Copy link

image

I have an issue with the @commands.has_any_role() method...
The first line is the error returned by the method, you can see the role id required in order to execute the command.
The second line is the list of the roles from the member... The id is in the list, but the method won't see it... It doesn't matter wether it looks for names or for ids, it won't work...

@MPolygon
Copy link

image

I have an issue with the @commands.has_any_role() method...
The first line is the error returned by the method, you can see the role id required in order to execute the command.
The second line is the list of the roles from the member... The id is in the list, but the method won't see it... It doesn't matter wether it looks for names or for ids, it won't work...

Ok nvm i made a mistake in the code. I was inputing a list in the has_any_role() method.

@xjunko
Copy link

xjunko commented Mar 17, 2021

i still looked at this, very useful gist

@wavyyy1337
Copy link

thank u so much <3

@parthshingari28
Copy link

You could do it by putting a * before the variable (list) containing the roles in the parameters:

roles = ['role_a', 'role_b', 'role_c']

@commands.has_any_role(*roles)
@commands.command
async def my_command(ctx):
    ...

Not working @CapClumpsy

@AwSkies
Copy link

AwSkies commented Jun 29, 2021

@parthshingari28 Could you show your code, or contact me on discord at CaptainClumsy#3018, since just saying "Not working" isn't very helpful and I can't really tell what's the problem.

@Mr-ROI
Copy link

Mr-ROI commented Jul 7, 2021

Can You give a example how to do it for Cogs , that will be very helpful for me .

@AwSkies
Copy link

AwSkies commented Jul 7, 2021

@Mr-ROI what is "it", are you talking about the checks, or something else?
If you're talking about the checks, simply put the check decorator before a method that is being used for a command.

@writeblankspace
Copy link

Is there an example as to how I could use my own function? What should the function return? Should it be true/false or a list of ID's?

@xjunko
Copy link

xjunko commented Oct 17, 2021

@jeanravenclaw true/false yea

@writeblankspace
Copy link

@FireRedz thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment