pythondiscord.pypython-3.11pycord

Mute command does not check for member permissions


I have been programming a Discord bot, and I added a mute command to it that works perfectly fine. However, anyone that has access to the command can mute anyone they please, even people with the manage_roles permissions. Here is the MRE:

# mute command
@bot.slash_command(name="mute", description="Mute a member in your server (REQUIRES MANAGE ROLES)")
@default_permissions(manage_roles = True)
@commands.guild_only()
async def mute(ctx, member:discord.Member, *, reason=None):
    coolbotvar = ctx.guild.me
    role = discord.utils.get(ctx.guild.roles, name="Muted")
    guild = ctx.guild
    if role in member.roles:
        embed=discord.Embed(title="",
                            description="This user is already muted!",
                            color=discord.Color.red())
        await ctx.respond(embed=embed, ephemeral=True)
    else:
        if reason == None:
            if member == coolbotvar:
                embed=discord.Embed(title="",
                                    description="I cannot mute myself!",
                                    color=discord.Color.red())
                await ctx.respond(embed=embed, ephemeral=True)
            else:
                if member.guild_permissions(manage_roles = True) == True:
                    embed=discord.Embed(title="",
                                        description="I cannot mute this member because they have `Manage Roles` permissions!",
                                        color=discord.Color.red())
                    await ctx.respond(embed=embed, ephemeral=True)
                else:
                    await member.add_roles(role)
                    embed=discord.Embed(title="Successfully muted!",
                                        description=f"Successfully muted {member.mention}, no reason provided.",
                                        color=discord.Color.green())
                    await ctx.respond(embed=embed)
                    for channel in guild.channels:
                        await channel.set_permissions(role, speak=False, send_messages=False)
                    print(f"{ctx.author} has successfully muted {member} in {guild}, no reason provided.")
# rest of code down here...

I tried this code and I expected the bot to return to me an ephemeral message saying that it cannot mute this person. However, it returns back the "The application did not respond" ephemeral message, with no error in the terminal.


Solution

  • Turns out, you can actually do this:

    if member.guild_permissions.manage_roles:
    

    This is usually done in order to see if the person DOING the command has that permission:

    if ctx.author.guild_permissions.manage_roles: