Home > Software engineering >  How do you assign a role to a user in pycord?
How do you assign a role to a user in pycord?

Time:11-28

I have a discord bot that needs to add someone to the ban watch list, which is a role. But so far, all I can find are discord.py solutions, and since discord.py is discontinued, I use pycord instead. The bot has full admin privileges, so those are no worry.

dmchannel = await user.create_dm()
dmchannel.send(f"<@{user.id}> You have been put on the ban watch list! Be careful, you could be banned soon.")
# Add the role here

How would I manage to do this?

CodePudding user response:

you can get the role using discord utils, then using member.add_roles()

member = #member
role = discord.utils.get(ctx.guild.roles, name=#role name)
member.add_roles(role)

CodePudding user response:

Pycord is now not much different from discord.py.
So, you can use something like this:

member = something  # specify member here
role = something  # specify role here
await member.add_roles(role)
  • Related