How to make this code work ?? ( discord.py )

Question:

Ok, so I want to remove a role from a user but it don’t work ( the command is : $rmv_role @user )

CODE :
( it’s not all of the code just the part with the command )

@bot.command(name='rmv_role', description="description for help command")
@commands.has_permissions(manage_roles=True)

async def rmv_role(ctx,arg):
    user_id = ''.join(c for c in arg if c.isdigit())
    guild = ctx.guild
    user = discord.utils.get(bot.get_all_members(), id=user_id)
    role_id = 999988854696194048
    role = discord.utils.get(guild.roles, id=role_id)
    await ctx.reply(f"th user :{arg} no longer has the role!")
    await bot.remove_roles(user,role)
Asked By: univers

||

Answers:

I Made a Bot Couple Months Ago, I had This Problem But I solved it My Self,
I had a Data Frame Like This

main.py
Commands (Folder)
  -> utility.py 

I just Linked The Main File to My Command Files so Instead Of @bot.command(), I had @commands.command()

You Don’t Worry About The self (It Refers To your Prefix in My Code), If you have any other Doubts Regarding My Code Just Comment Down :

Code:

   @commands.command(name='rmv_role', description="description for help command")
    @commands.has_permissions(manage_roles=True)
    async def rmv_role(self, ctx, member: discord.Member):
      print("open")
      guild = ctx.guild
      role_id =  999988854696194048
      role = discord.utils.get(guild.roles, id=role_id)
      await ctx.reply(f"th user :{member.name} no longer has the role!")
      await member.remove_roles(role)

Note That I Changed Your args to member: discord.Member Cause You dont Have to get the User with discord.Member Just Ping The Person!

I removed the user and user_id because of That As Well!

I tested and Checked The Code in My Bot Thats Why I took 20 Mins!

Hope This Helps, Comment If You have Any Doubt About this Or wanna learn about these self or @commands.command() more! I am Happy To Help!

Answered By: Ganime Dewer

So, finaly I changed my code and now it looks like this :

@bot.command(name='rmv_role', description="description for help command")
@commands.has_permissions(manage_roles=True)

async def rmv_role(ctx,user: discord.Member):
    guild = ctx.guild
    role_id = 999988854696194048
    role = discord.utils.get(guild.roles, id=role_id)
    await user.remove_roles (role)
Answered By: univers
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.