如何使用 discord.py/my discord bot 为用户添加角色?

How can I add a role to a user with discord.py/my discord bot?

我在 python 到 运行 中在我的服务器上制作了一个小的不和谐机器人,但我不想在我的服务器中有随机数,所以我创建了这个命令。
当执行“代码”(也可以看作密码)时,用户获得用户角色(来自访客角色)

我的代码:

@client.command()                                                                   
async def test(ctx):                                                               
    user = ctx.message.author                                                       
    await user.add_roles(discord.utils.get(user.guild.roles, id=...))

我得到的错误:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception:
Forbidden: 403 Forbidden (error code: 50013): Missing Permissions

到目前为止我尝试了什么:

那么,有人能帮帮我吗?

我发现了一个与这个问题类似的问题,它有一些答案,所以我调整了代码以找出答案,试试这个,它对我有用:

@client.command()
async def test(ctx):
    user= ctx.message.author
    role = (discord.utils.get(user.guild.roles, id=insert id here))
    await ctx.author.add_roles(role)
    await ctx.send("Successfully assigned {}!".format(role.mention))

client.run('token for obvious reasons here')

您可以在 The discord developer portal, Error Code 50013 Is You lack permissions to perform that action or the bot can't do the action you wanted it to do. More Info here 查看错误代码。

Note: There's probably something wrong with your code.