pythondiscorddiscord.pydisnake

How to make a ckeck of user permissions in disnake or discord.py


I've got a code with permissions. I need to check a member permissions and if member don`t have such permissions send a message

Main code:

@commands.slash_command(name = "addrole", description="Додати користувачу роль")
@commands.has_permissions(view_audit_log=True)
async def addrole(self, ctx, member: disnake.Member, role: disnake.Role):
    #try:
        await member.add_roles(role)
        emb = disnake.Embed(title=f"Видача ролі", description=f"Користувачу {member.mention} було видано роль {role.mention} на сервері {ctx.guild.name}\n Видав модератор - **{ctx.author.mention}**", colour=disnake.Color.blue(), timestamp=ctx.created_at)
        await ctx.send(embed=emb)

What i want to have:

    @commands.slash_command(name = "addrole", description="Додати користувачу роль")
    @commands.has_permissions(view_audit_log=True)
    async def addrole(self, ctx, member: disnake.Member, role: disnake.Role):
        if ctx.author.guild_permissions.view_audit_log:
            await member.add_roles(role)
            emb = disnake.Embed(title=f"Видача ролі", description=f"Користувачу {member.mention} було видано роль {role.mention} на сервері {ctx.guild.name}\n Видав модератор - **{ctx.author.mention}**", colour=disnake.Color.blue(), timestamp=ctx.created_at)
            await ctx.send(embed=emb)
        else:
            await ctx.send("You don`t have such permissions!")

Please, help me. I tried diferrent variants and no one working


Solution

  • I think you are not getting what you want because you are trying to integrate both at once. From what I can see from your description and code, the easiest solution I could provide you is removing @commands.has_permissions(view_audit_log=True) from your "what I want to have" section of code. However, if you don't want to have to add an extra if and else statement sending "you dont have permissions" to the user under every command, I would suggest creating an error handler. Using this method, you would be able to use the first block of code you have listed in your question. Here is a very basic sample of the one I use which deals with a user not having permissions to use a certain command. You'll probably need to change it to fit your specific bot but it's a start:

    @client.event
    async def on_command_error(ctx, error)
       await ctx.send(error) # this sends the error where the user is attempting to use a command. This means if the user is missing the right permissions to use a command, it will send something like "missing permissions" to the user. Which I think is what you are looking for
    
    

    The "what I want to have" code is not working because the code you have under it is unreachable unless the user has the audit_log permissions. And if they already have those permissions, the if and else statements checking permissions are not very useful. An error handler is in my opinion good to have and has helped me a ton with my bots. Hope this helps