Reputation: 89
I have trouble with making an "add role" command in discord.py. I don't know what is wrong; it just doesn't work.
@client.command()
@commands.has_role("Admin")
async def addrole(ctx):
user = ctx.message.author
role = discord.utils.get(user.server.roles, name="Test")
await client.add_roles(user, role)
Upvotes: 7
Views: 121179
Reputation: 55
For anyone looking for a current solution I was stuck on this for a while but you can use ctx.message.author.add_roles(get(guild.roles, name="Test"))
to add a role to a user. (Assuming the role exists)
You can grab the guild information with await bot.get_guild(id)
and you can basically call anything from guild.roles
to guild.bans
lol
DEMO
import discord
from discord.ext import commands
from discord.utils import get
TOKEN = ""
intents=discord.Intents.all()
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.command(pass_context=True) # pass_context=True is important here.
async def go(ctx):
guild = bot.get_guild(GUILD_ID) # Get guild information
role = get(guild.roles, name="Test") # Get specific role from guild
await ctx.message.author.add_roles(role) # Add role here.
await ctx.send('Role added!') # Confirm roles added
bot.run(TOKEN)
Make sure your bot has permission to add the role your looking for and that it exists.
Upvotes: 3
Reputation: 11
Use this code:
role = discord.utils.get(ROLE_ID)
member.add_roles(role)
Important: bot can't manage roles that above him
Upvotes: 1
Reputation: 63
Here is a role command for you!
@client.command('role')
@commands.has_permissions(administrator=True) #permissions
async def role(ctx, user : discord.Member, *, role : discord.Role):
if role.position > ctx.author.top_role.position: #if the role is above users top role it sends error
return await ctx.send('**:x: | That role is above your top role!**')
if role in user.roles:
await user.remove_roles(role) #removes the role if user already has
await ctx.send(f"Removed {role} from {user.mention}")
else:
await user.add_roles(role) #adds role if not already has it
await ctx.send(f"Added {role} to {user.mention}")
Aswell as errors:
@role.error
async def role_error(ctx, error):
if isinstance(error, MissingPermissions):
await ctx.send('**:x: | You do not have permission to use this command!**')
Now that its finished, you have a role command!
Upvotes: 3
Reputation: 121
@bot.command(pass_context=True)
async def giverole(ctx, user: discord.Member, role: discord.Role):
await user.add_roles(role)
await ctx.send(f"hey {ctx.author.name}, {user.name} has been giving a role called: {role.name}")
Upvotes: 12
Reputation: 198
@client.command()
async def addrole(ctx, member : discord.Member, role : discord.Role):
await member.add_roles(role)
usage: !addrole [member] [role]
NOTE : the bot can only give roles lower than him !
Upvotes: 4
Reputation: 91
roleVer = 'BOT' #role to add
user = ctx.message.author #user
role = roleVer # change the name from roleVer to role
await ctx.send("""Attempting to Verify {}""".format(user))
try:
await user.add_roles(discord.utils.get(user.guild.roles, name=role)) #add the role
except Exception as e:
await ctx.send('There was an error running this command ' + str(e)) #if error
else:
await ctx.send("""Verified: {}""".format(user)) # no errors, say verified
Upvotes: 7
Reputation: 61052
from discord.ext import commands
from discord.utils import get
bot = commands.Bot(command_prefix='!')
@bot.command(pass_context=True)
@commands.has_role("Admin") # This must be exactly the name of the appropriate role
async def addrole(ctx):
member = ctx.message.author
role = get(member.server.roles, name="Test")
await bot.add_roles(member, role)
I think the only real mistake in your code is the lack of pass_context=True
in the @bot.command
decorator. You may have seen some code without this, but that likely belongs to the experimental "rewrite" branch of discord.py
Upvotes: 13