This code basically just checks that if the command raiser is the owner of the server or not and then assigns the specified role to him.
@bot.command()
@commands.is_owner()
async def role(ctx, role:discord.Role):
"""Add a role to someone"""
user = ctx.message.mentions[0]
await user.add_roles(role)
await ctx.send(f"{user.name} has been assigned the role:{role.name}")
Let me break the code down:
@bot.command()
@commands.is_owner()
These are just plain function decorators. They are pretty much self-explanatory. But still let me tell.
@bot.command()
just defines that it is a command.
@commands.is_owner()
checks that the person who has raised that command is the owner.
async def role(ctx, role:discord.Role):
> This line defines the function.
user = ctx.message.mentions[0] #I don't know about this line.
await user.add_roles(role) #This line adds the roles to the user.
await ctx.send(f"{user.name} has been assigned the role:{role.name}")
#It just sends a kind of notification that the role has been assigned
if discord.utils.get(ctx.message.author.roles, name=rolename): removerole; else: addrole
- Patrick Haugh