通过 ID 添加角色 discord.py

时间:2021-03-06 21:49:39

标签: python discord discord.py bots roles

我坚持不懈地尝试了大约 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

3 个答案:

答案 0 :(得分:0)

您将作为 str 放入,因此请删除您拥有的 ""。

答案 1 :(得分:0)

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

因此,如果您尝试使用 user.add_roles(role),它应该会起作用。

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

所以它看起来像这样;

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

答案 2 :(得分:0)

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

希望现在应该可以了:)