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_
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
@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 !
@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}")
Let me know if it works!
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