简体   繁体   English

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

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

I've made a little discord bot in python to run it on my server, but i don't want to have randoms in my server so i created this command.我在 python 中制作了一个小小的 discord bot 来在我的服务器上运行它,但我不想在我的服务器中有随机数,所以我创建了这个命令。
When executed the "code" (can also seen as password), the user get the user role (from the visitor role)当执行“代码”(也可以看作是密码)时,用户获得用户角色(来自访问者角色)

My code:我的代码:

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

The error I get:我得到的错误:

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

What I tried so far:到目前为止我尝试过的:

  • Give the bot all permissions给机器人所有权限
  • Elevate the bot at highest hierarchy.将机器人提升到最高层次。

So, could anyone please help me?那么,有人可以帮我吗?

I found a similar question to this one, that had some answers, so I tweaked the code to figure something out, try this, it worked for me:我发现了一个类似的问题,有一些答案,所以我调整了代码以找出一些东西,试试这个,它对我有用:

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

You can see the error code at 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.您可以在Discord 开发人员门户看到错误代码,错误代码 50013 是You lack permissions to perform that action还是机器人无法执行您希望它执行的操作。 More Info here . 更多信息在这里

Note: There's probably something wrong with your code.注意:您的代码可能有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM