Discord.py | Add Role To Someone
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(ct
Solution 1:
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 roleasyncdefaddrole(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
Solution 2:
@bot.command(pass_context=True)asyncdefgiverole(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}")
Solution 3:
roleVer = 'BOT'#role to add
user = ctx.message.author #user
role = roleVer # change the name from roleVer to roleawait ctx.send("""Attempting to Verify {}""".format(user))
try:
await user.add_roles(discord.utils.get(user.guild.roles, name=role)) #add the roleexcept Exception as e:
await ctx.send('There was an error running this command ' + str(e)) #if errorelse:
await ctx.send("""Verified: {}""".format(user)) # no errors, say verified
Solution 4:
@client.command()asyncdefaddrole(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 !
Solution 5:
Here is a role command for you!
@client.command('role')@commands.has_permissions(administrator=True) #permissionsasyncdefrole(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 errorreturnawait 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 hasawait ctx.send(f"Removed {role} from {user.mention}")
else:
await user.add_roles(role) #adds role if not already has itawait ctx.send(f"Added {role} to {user.mention}")
Aswell as errors:
@role.errorasyncdefrole_error(ctx, error):
ifisinstance(error, MissingPermissions):
await ctx.send('**:x: | You do not have permission to use this command!**')
Now that its finished, you have a role command!
Post a Comment for "Discord.py | Add Role To Someone"