Discord.py | add role to someone

后端 未结 4 1523
别跟我提以往
别跟我提以往 2021-01-07 01:44

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_         


        
相关标签:
4条回答
  • 2021-01-07 02:18
    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
    
    0 讨论(0)
  • 2021-01-07 02:20
    @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 !

    0 讨论(0)
  • 2021-01-07 02:25
    @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!

    0 讨论(0)
  • 2021-01-07 02:28
    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

    0 讨论(0)
提交回复
热议问题