Discord.py |为某人添加角色

时间:2018-03-02 20:24:26

标签: python discord.py

我在discord.py中制作'添加角色'命令时遇到问题。我不知道出了什么问题 - 它只是不起作用。

@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)

5 个答案:

答案 0 :(得分: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 role
async def addrole(ctx):
    member = ctx.message.author
    role = get(member.server.roles, name="Test")
    await bot.add_roles(member, role)

我认为代码中唯一真正的错误是pass_context=True装饰器中缺少@bot.command。您可能已经看过没有这个的一些代码,但这可能属于的实验性“重写”分支

答案 1 :(得分:0)

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

答案 2 :(得分:0)

@bot.command(pass_context=True)
async def giverole(ctx, user: discord.Member, role: discord.Role):
    await user.add_roles(role)
    await await ctx.send(f"hey {ctx.author.name}, {user.name} has been giving a role called: {role.name}")

让我知道它是否有效!

答案 3 :(得分:0)

@client.command()
async def addrole(ctx, member : discord.Member, role : discord.Role):
    await member.add_roles(role)

用法:!addrole [成员] [角色]

注意:机器人只能给比他低的角色!

答案 4 :(得分:0)

这是给你的角色命令!

@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}") 

以及错误:

@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!**')

现在它完成了,你有一个角色命令!