Help With Discord.py mute and unmute add roles and take away roles

when running this code on a discord bot using my prefix and command i get the error

"Ignoring exception in command mute: Traceback (most recent call last): File “C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py”, line 85, in wrapped ret = await coro(*args, **kwargs) File “C:\Users\User\Desktop\yhyh\bot (1).py”, line 145, in mute await member.remove_roles(“Members”) File “C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\member.py”, line 685, in remove_roles await req(guild_id, user_id, role.id, reason=reason) AttributeError: ‘str’ object has no attribute ‘id’

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File “C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py”, line 903, in invoke await ctx.command.invoke(ctx) File “C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py”, line 855, in invoke await injected(*ctx.args, **ctx.kwargs) File “C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py”, line 94, in wrapped raise CommandInvokeError(exc) from exc discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: ‘str’ object has no attribute ‘id’"

in console. I am new to discord.py please could someone more developed fix this code. its not to do with my other code everything works normal without the mute function. (code is how i need it just it dont work i need it to remove a role then add muted)

@bot.command()
async def mute(ctx, member: discord.Member):
    role = discord.utils.get(ctx.guild.roles, name='Muted')
    await member.remove_roles("Members")
    await member.add_roles("Muted")
    await context.send("User Was Muted")

@bot.command()
async def unmute(ctx, member: discord.Member):
    role = discord.utils.get(ctx.guild.roles, name='Members')
    await member.remove_roles("Muted")
    await member.add_roles("Members")
    
    await context.send("User Was Un Muted")

AttributeError: ‘str’ object has no attribute ‘id’

I see you are passing a str to the remove_roles and add_roles methods. You should check the discord.py reference to see if that’s correct.

I also see you get a role object above as if you were about to do something different, but the code goes on not to use it. Could you comment on that?

2 Likes