【问题标题】:How can I add a role to a user with discord.py/my discord bot?如何使用 discord.py/my discord bot 向用户添加角色?
【发布时间】:2020-10-13 19:12:44
【问题描述】:

我在 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

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

  • 授予机器人所有权限
  • 将机器人提升到最高层级。

那么,谁能帮帮我?

【问题讨论】:

  • 您可以尝试将user = ctx.message.author 更改为user = ctx.author 吗?
  • 我做了这个改变,但它不影响结果
  • get 角色和add_role 分拆怎么样
  • 我尝试了多种选择,但都没有帮助

标签: python python-3.x discord discord.py discord.py-rewrite


【解决方案1】:

您可以在 The discord developer portal 看到错误代码,错误代码 50013 是 You lack permissions to perform that action,否则机器人无法执行您希望它执行的操作。 More Info here.

注意:您的代码可能有问题。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 不幸的是,这也没有帮助。我收到相同的错误消息
    • 那么这部分代码可能不是问题,而是其他一些字符串?
    猜你喜欢
    • 2020-09-02
    • 1970-01-01
    • 2023-03-19
    • 2018-11-26
    • 2021-05-15
    • 2021-04-29
    • 2019-08-11
    • 1970-01-01
    • 2019-11-01
    相关资源
    最近更新 更多