Python Discord.py添加_角色问题

Python Discord.py添加_角色问题,python,discord,discord.py,Python,Discord,Discord.py,我正在尝试制作一个机器人,在成员加入时为他们提供角色。但是,它不断出现错误消息 Traceback (most recent call last): File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped ret = await coro(*args, **kwargs) File "ma

我正在尝试制作一个机器人,在成员加入时为他们提供角色。但是,它不断出现错误消息

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 34, in lol
    await member.add_roles(probation)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/member.py", line 764, in add_roles
    await req(guild_id, user_id, role.id, reason=reason)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/http.py", line 248, in request
    raise Forbidden(r, data)
discord.errors.Forbidden: 403 Forbidden (error code: 50013): Missing Permissions

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
我给了它“管理角色”权限(见下文),但它仍然会出现这个错误。

有解决办法吗? 还有我的python代码:

import discord
from discord.ext import commands
from dotenv import load_dotenv
import os
import json
from datetime import datetime, timedelta
import asyncio

load_dotenv()
token = os.getenv('Gov')
intents=discord.Intents.all()

bot = commands.Bot(command_prefix='!', intents = intents)

@bot.event
async def on_member_join(member):
  join_guild = await bot.fetch_guild(793659768606425140)
  probation = join_guild.get_role(838212050332549142)

  
  await member.add_roles(probation)

我没有发现您的代码有任何问题。您说bot具有添加角色的权限,但您能否尝试检查bot赋予成员的角色是否位于bot角色之上。

您可以尝试:

@bot.event async def on_member_join(member):
    channel =discord.utils.get(member.guild.text_channels,name="channelnamehere")
    role = discord.utils.get(member.guild.roles, name='rolenamehere')
    await member.add_roles(role)
    await channel.send(f"{member.mention} welcome, you have been assigned {role}!")
这应该可以工作,而不必拉通道ID和诸如此类!
如果这不起作用,我最好的建议是检查您的机器人是否高于您尝试分配的所有角色!

您是否正在尝试向服务器所有者添加角色?不,我没有,我正在尝试将其添加到加入的新成员。但这是我的alt帐户,这会有影响吗?顶部是否意味着它高于机器人的角色?像这样在“服务器设置角色”侧面板中,是否需要将机器人的角色设置为高于其尝试分配的角色。@Cyber Yosh机器人的角色必须高于您希望通过机器人分配的角色。代码没有问题,只是我将机器人的角色设置为低于我尝试分配的角色,谢谢。