Search code examples
pythonpycord

How exactly does discord.CommandPermission work?


I try to gray out some slash commands that only someone with the right permissions can use. I found out that you can gray them out for everyone if you set default_permission = False. Then I found out that theres a permissions attribute that you can provide. But I just can't get it working. I tried using it like this permissions = [discord.CommandPermission(id=8589934592, type=3)] But with this the command is disabled for everyone. My complete command looks like this:

@slash_command(name='testcommand', description='its just a test', guild_ids = [831161440705839124], permissions = [discord.CommandPermission(id=8589934592, type=3)])
async def testcommand(self, ctx, channel : Option(discord.TextChannel, "a normal test", required = True)):
    print("Test passed!")

If anyone knows how I am supposed to use the permissions attribute or another way to disable slash commands that require specific permissions, please let me know!

I don't know if it changes anything but I'm using Pycord 2.0.0b5


Solution

  • Looking at the source code for the permission decorator, it becomes apparent that your type integer is invalid (not sure why they didn't write any validation or documentation for this, I guess they anticipated everyone would use the @permissions decorators)

    def decorator(func: Callable):
        if not role_id is None:
            app_cmd_perm = CommandPermission(role_id, 1, permission, guild_id)
        elif not user_id is None:
            app_cmd_perm = CommandPermission(user_id, 2, permission, guild_id)
        else:
            raise ValueError("role_id or user_id must be specified!")
    

    If you are trying to whitelist a role, you should provide type 1, if you want to whitelist a user, provide type 2


    On a side note, Discord have just released their improved slash command permission system allowing you much, much more flexibility on who can use which command and where. It will probably be a bit until this is supported by Pycord, but you might want to keep an eye on this in the next weeks