403 Forbidden(错误代码:50001):添加角色时缺少访问权限 |不和谐.py

问题描述

我正在尝试快速为人们分配不同的角色,以给用户一种他们的名字是彩虹的印象(是的,我知道它反对 TOS),并且我开始在删除他们之前向人们添加角色.但是,在添加角色时,我在这篇文章的标题中得到了错误.我对此进行了调查并尝试了很多方法来解决它.机器人的角色比分配的角色更高.这是我的代码和输出:

I am trying to ass different roles to people rapidly to give users the impression of their name being rainbow ( yes I know its against TOS ), and I am starting by adding roles to people before I remove them. However, when adding roles I get the error in the title of this post. I have looked into this and tried quite a few ways to fix it. The bot has a higher role than the roles being give out. Here is my code and the output:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="$")
role_name = "Rainbow Six Seige"
peopleWithRole = []
guild = discord.Guild 

@bot.event
async def on_ready():
    print("Logged in as")
    print(bot.user.name)
    print("------")
    guild = bot.guilds[0]
    colours = [discord.utils.get(guild.roles, name='red'),
               discord.utils.get(guild.roles, name='green'),
               discord.utils.get(guild.roles, name='blue')
               ]

    role = discord.utils.find(
        lambda r: r.name == role_name, guild.roles)
        
    for user in guild.members:
        if role in user.roles:
            peopleWithRole.append(user)

    for color in colours:
        for user in peopleWithRole:
            await user.add_roles(color)

bot.run("my token")

输出:

Logged in as
test bot
------
Ignoring exception in on_ready
Traceback (most recent call last):
  File "C:UsersUserAppDataLocalProgramsPythonPython38-32libsite-packagesdiscordclient.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "C:UsersUserDesktop	est	est.py", line 29, in on_ready
    await user.add_roles(color)
  File "C:UsersUserAppDataLocalProgramsPythonPython38-32libsite-packagesdiscordmember.py", line 641, in add_roles
    await req(guild_id, user_id, role.id, reason=reason)
  File "C:UsersUserAppDataLocalProgramsPythonPython38-32libsite-packagesdiscordhttp.py", line 241, in request
    raise Forbidden(r, data)
discord.errors.Forbidden: 403 Forbidden (error code: 50001): Missing Access


解决方案

确保您拥有授予机器人的角色,该角色可以访问 managing roles.此外,当您从开发门户添加机器人时,您可以为其授予权限.

Make sure you have a role given to the bot which has access to managing roles. Also when you add the bot from the dev portal you can give it permissions.

最佳做法是创建一个名为 BOT 的角色,并拥有所有权限并将其提供给您在服务器中拥有的所有机器人

Best practice is to make a role called BOT with all the permissions and give it to all the bots you have in the server

相关文章