1# If ctx is not defined and you are handling a message, use this code
2ctx = await bot.get_context(message)
3
4# This is the code needed to give a user a role
5member = ctx.message.author # Member object that you want to add the role to
6role = discord.utils.get(lambda role: role.name == "Role Name", ctx.guild.roles) # The role object
7await member.add_roles(role) # Adds the role to the member
1member = message.author
2var = discord.utils.get(message.guild.roles, name = "role name")
3member.add_role(var)
1# If you need this in a bot command
2@bot.command()
3async def online(ctx):
4 role = discord.utils.get(bot.get_guild(ctx.guild.id).roles, id ="Role ID")
5 await member.add_roles(role)
1@client.command(aliases=['make_role'])
2@commands.has_permissions(manage_roles=True) # Check if the user executing the command can manage roles
3async def create_role(ctx, *, name):
4 guild = ctx.guild
5 await guild.create_role(name=name)
6 await ctx.send(f'Role `{name}` has been created')
1# If you need this in a bot command
2@discord.ext.commands.command()
3async def online(ctx):
4 role = discord.utils.get(bot.get_guild(ctx.guild.id).roles, id ="Role ID")
5 await member.add_roles(role)