Search code examples
pythonpycharmdiscord.pypython-3.9

How to limit Administration menu to Admins, Mods and Owners (PyCharm 2020.3.3 Python 3.9.1


I created an Administration cog a long time ago but I didn't limit it for administrators. I tried to limit it for admins but I was only able to limit the cog a single role. How do I make it that the cog is limited to people who have Admin powers?

Current Code:

    async def cog_check(self, ctx):
        admin = get(ctx.guild.roles, name="Admin")
        return admin in ctx.author.roles

Solution

  • You can use discord.ext.commands.has_permissions() if you need that the person executing the command has a certain permission, otherwise you an also use permissions_for() that return a Permissions object and use the permission administrator

    It would look like this

    async def cog_check(self, ctx):
        return ctx.channel.permissions_for(ctx.author).administrator
    

    this coroutine will return True if and only if the member has the permission administrator (in one of his roles or if he was specified the permission in a channel)