通过 ID 添加角色 discord.py

Posted

技术标签:

【中文标题】通过 ID 添加角色 discord.py【英文标题】:Add role by ID discord.py 【发布时间】:2021-06-05 05:19:15 【问题描述】:

我已经坚持不懈地尝试了大约 1-2 小时来让这段代码正常工作。我需要将角色添加到用户,够简单吧?

此代码搜索角色但找不到它,这是因为我从该角色无权访问的频道发送它吗?我需要帮助。

编辑 1:删除了 ID 周围的引号

@bot.command()
async def addrole(ctx, user: discord.User):
    #Add the customer role to the user
    role_id = 810264985258164255

    guild = ctx.guild
    role = discord.utils.get(guild.roles, id=810264985258164255)
    await user.add_roles(role)
2021-03-06T21:12:51.811235+00:00 app[worker.1]: Your bot is ready.
2021-03-06T21:14:09.010057+00:00 app[worker.1]: Ignoring exception in command addrole:
2021-03-06T21:14:09.012477+00:00 app[worker.1]: Traceback (most recent call last):
2021-03-06T21:14:09.012553+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 85, in wrapped
2021-03-06T21:14:09.012554+00:00 app[worker.1]: ret = await coro(*args, **kwargs)
2021-03-06T21:14:09.012592+00:00 app[worker.1]: File "bot.py", line 92, in addrole
2021-03-06T21:14:09.012593+00:00 app[worker.1]: await user.add_roles(user, role)
2021-03-06T21:14:09.012621+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/member.py", line 673, in add_roles
2021-03-06T21:14:09.012621+00:00 app[worker.1]: await req(guild_id, user_id, role.id, reason=reason)
2021-03-06T21:14:09.012651+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/http.py", line 243, in request
2021-03-06T21:14:09.012652+00:00 app[worker.1]: raise NotFound(r, data)
2021-03-06T21:14:09.012699+00:00 app[worker.1]: discord.errors.NotFound: 404 Not Found (error code: 10011): Unknown Role
2021-03-06T21:14:09.012735+00:00 app[worker.1]:
2021-03-06T21:14:09.012735+00:00 app[worker.1]: The above exception was the direct cause of the following exception:
2021-03-06T21:14:09.012736+00:00 app[worker.1]:
2021-03-06T21:14:09.012772+00:00 app[worker.1]: Traceback (most recent call last):
2021-03-06T21:14:09.012832+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 935, in invoke
2021-03-06T21:14:09.012833+00:00 app[worker.1]: await ctx.command.invoke(ctx)
2021-03-06T21:14:09.012863+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 863, in invoke
2021-03-06T21:14:09.012863+00:00 app[worker.1]: await injected(*ctx.args, **ctx.kwargs)
2021-03-06T21:14:09.012890+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 94, in wrapped
2021-03-06T21:14:09.012891+00:00 app[worker.1]: raise CommandInvokeError(exc) from exc
2021-03-06T21:14:09.012932+00:00 app[worker.1]: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NotFound: 404 Not Found (error code: 10011): Unknown Role

编辑 2:从 add_role 中删除用户

2021-03-06T22:22:45.062172+00:00 app[worker.1]: Your bot is ready.
2021-03-06T22:22:52.026199+00:00 app[worker.1]: Ignoring exception in command addrole:
2021-03-06T22:22:52.028082+00:00 app[worker.1]: Traceback (most recent call last):
2021-03-06T22:22:52.028089+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 85, in wrapped
2021-03-06T22:22:52.028089+00:00 app[worker.1]: ret = await coro(*args, **kwargs)
2021-03-06T22:22:52.028092+00:00 app[worker.1]: File "bot.py", line 94, in addrole
2021-03-06T22:22:52.028100+00:00 app[worker.1]: await user.add_roles(role)
2021-03-06T22:22:52.028104+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/member.py", line 673, in add_roles
2021-03-06T22:22:52.028108+00:00 app[worker.1]: await req(guild_id, user_id, role.id, reason=reason)
2021-03-06T22:22:52.028111+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/http.py", line 241, in request
2021-03-06T22:22:52.028111+00:00 app[worker.1]: raise Forbidden(r, data)
2021-03-06T22:22:52.028152+00:00 app[worker.1]: discord.errors.Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
2021-03-06T22:22:52.028160+00:00 app[worker.1]:
2021-03-06T22:22:52.028161+00:00 app[worker.1]: The above exception was the direct cause of the following exception:
2021-03-06T22:22:52.028161+00:00 app[worker.1]:
2021-03-06T22:22:52.028164+00:00 app[worker.1]: Traceback (most recent call last):
2021-03-06T22:22:52.028223+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 935, in invoke
2021-03-06T22:22:52.028224+00:00 app[worker.1]: await ctx.command.invoke(ctx)
2021-03-06T22:22:52.028225+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 863, in invoke
2021-03-06T22:22:52.028225+00:00 app[worker.1]: await injected(*ctx.args, **ctx.kwargs)
2021-03-06T22:22:52.028225+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 94, in wrapped
2021-03-06T22:22:52.028226+00:00 app[worker.1]: raise CommandInvokeError(exc) from exc
2021-03-06T22:22:52.028228+00:00 app[worker.1]: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Forbidden: 403 Forbidden (error code: 50013): Missing Permissions

【问题讨论】:

角色 ID 是整数 iirc,您是否尝试过使用 ID 作为数字而不是像现在这样的字符串? 是的,刚刚测试过,遗憾的是仍然没有工作 关于第二个错误:bot 无法为用户添加角色,要么是层次太低,要么就是没有必要的权限 【参考方案1】:
@bot.command()
async def addrole(ctx, user: discord.Member):
    # Add the customer role to the user

    guild = ctx.guild # You can remove this if you don't need it for something other
    role = ctx.guild.get_role(810264985258164255)
    await user.add_roles(role)

希望现在应该可以工作了:)

【讨论】:

【参考方案2】:

您无需再次将user 添加到user.add_roles()

所以如果你尝试user.add_roles(role),它应该可以工作。

编辑:您现在应该将 discord.User 替换为 discord.Member

所以它看起来像这样;

async def addrole(ctx, user: discord.Member):

【讨论】:

【参考方案3】:

你把它当作一个 str,所以删除你的 ""。

【讨论】:

天哪,你知道你什么时候盯着某样东西看了这么久......谢谢你 遗憾的是仍然没有用,我真的认为这是一个黄金解决方案 您的机器人角色(您无法编辑的角色)是否高于人(可能是您)角色 并将 discord.User 替换为 discord.Member 缺少权限错误意味着您的机器人缺少权限,例如,添加角色。您(或机器人)需要“管理角色”

以上是关于通过 ID 添加角色 discord.py的主要内容,如果未能解决你的问题,请参考以下文章

如何通过 REST API 将 Keycloak 领域角色添加到组

mysql添加用户时设定角色,能添加进数据库,但是用户ID和角色ID不是一个

Unity3d - RPG项目学习笔记(二十八)

如果用户在某个角色中,则将术语添加到用户的帖子中

如何添加 ID 保存在 JSON 文件 discord.js v12 中的角色?

成员加入公会时如何添加角色