What command should I use to give a user a role in discord?
That sort of question is best suited for the Discord server however, to answer your question, this method should do it (note that this is in a class - remove self if you're not using it in a cog) and bot is the name of the Discord client object:
@commands.command(pass_context=True)
async def role(self, ctx, *, role: discord.Role = None):
"""
Toggle whether or not you have a role. Usage: `!role DivinityPing`. Can take roles with spaces.
:param role: Anything after "role"; should be the role name.
"""
if role is None:
return await self.bot.say("You haven't specified a role! ")
if role not in ctx.message.server.roles:
return await self.bot.say("That role doesn't exist.")
if role not in ctx.message.author.roles:
await self.bot.add_roles(ctx.message.author, role)
return await self.bot.say("{} role has been added to {}.".format(role, ctx.message.author.mention))
if role in ctx.message.author.roles:
await self.bot.remove_roles(ctx.message.author, role)
return await self.bot.say("{} role has been removed from {}."
.format(role, ctx.message.author.mention))
I wrote a small improvement to this answer that checks a user's permissions and adds the ability to add/remove roles from more than just yourself. Keep in mind this is the function route rather than the class route and is not a copy paste solution for most bots.
def user_is_mod(user):
author_roles = user.roles
has_right_role = False
for role in author_roles:
if role.name == os.environ['mod_role'] or user.id == "X":
has_right_role = True
return has_right_role
def user_is_admin(user):
author_roles = user.roles
has_right_role = False
for role in author_roles:
if role.name == os.environ['admin_role'] or user.id == "X":
has_right_role = True
return has_right_role
def user_is_custom_role(user):
author_roles = user.roles
has_right_role = False
for role in author_roles:
if role.name == os.environ['custom_role'] or user.id == "X":
has_right_role = True
return has_right_role
@client.command(pass_context=True)
async def role(ctx, role: discord.Role = None, user: discord.Member = None):
"""
Toggle whether or not you have a role. Usage: `!role Admin`. Can take roles with spaces.
:param role: Anything after "role"; should be the role name.
:param user: Any user
"""
if user_is_mod(ctx.message.author) or user_is_admin(ctx.message.author) or user_is_custom_role(
ctx.message.author):
if role is None and user is None:
return await client.say("You haven't specified a role or a user! ")
if role not in ctx.message.server.roles or user not in ctx.message.server.members:
return await client.say("That role or user doesn't exist.")
if role not in ctx.message.author.roles and user == None:
await client.add_roles(ctx.message.author, role)
return await client.say("{} role has been added to {}."
.format(role, ctx.message.author.mention))
if role in ctx.message.author.roles and user == None:
await client.remove_roles(ctx.message.author, role)
return await client.say("{} role has been removed from {}."
.format(role, ctx.message.author.mention))
if user != None and role not in user.roles:
await client.add_roles(user, role)
return await client.say("{} role has been added to {}.".format(role, user.mention))
if user != None and role in user.roles:
await client.remove_roles(user, role)
return await client.say("{} role has been removed from {}."
.format(role, user.mention))
else:
return await client.say("Silly human, you do not have permission to use this command!")
bad asfffff
This issue has been inactive since late 2018???? The problem with the answer @jcsumlin gave is that it's a rough copy of what it uses. The intent being that you read the code and redesign it to fit your bot.
You are being extremely rude.
@Rapptz I think it's time to lock this issue...
Most helpful comment
I wrote a small improvement to this answer that checks a user's permissions and adds the ability to add/remove roles from more than just yourself. Keep in mind this is the function route rather than the class route and is not a copy paste solution for most bots.