Search code examples
python-3.xpycord

Pycord mute command doesn't throw error but doesn't add role


I am using pycord for this so I can use slash commands.

Here is my code:

import discord, os
from dotenv import load_dotenv
from discord.ext import commands

load_dotenv(f'{os.getcwd()}/token.env')

bot = commands.Bot()

token = str(os.getenv('TOKEN'))
@bot.slash_command(description = 'mutes member of choice')
@commands.has_permissions(administrator = True)
async def mute(ctx, member: discord.Member):
    guild = ctx.guild
    mutedRole = discord.utils.get(guild.roles, name="Muted")

    if not mutedRole:
        mutedRole = await guild.create_role(name="Muted")

        for channel in guild.channels:
            await channel.set_permissions(mutedRole, speak=False, send_messages=False, read_message_history=True, read_messages=False)

    await member.add_roles(mutedRole)
    await ctx.send(f"Muted {member.mention}")

@mute.error
async def permserror(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        ctx.respond('Sorry! You dont have permission.')

@bot.event
async def on_ready():
    print(f'Client ready as {bot.user}')

bot.run(token)

This isn't my full code, but the other commands don't seem necessary for my problem.

Anyway, when I try to use this command, it doesn't throw an error in the console, but the application doesn't respond and it doesn't add the role.

I put a print statement between each line of code, and it seems to stop just before it adds the role.

How can I fix this?

Update: My previous code did not run so I have updated it.


Solution

  • This doesn't actually have to do with the code of the program. All you have to do is move the bot role to the top and it'll work. The error is 403 Forbidden (error code: 50013): Missing Permissions. Also, you have to reinvite the bot and add applications.commands if you haven't already.