繁体   English   中英

Discord.py add_roles 问题

[英]Discord.py add_roles problems

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

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)

我没有看到你的代码有任何问题。 您说机器人具有add roles权限,但您可以尝试检查您的机器人赋予成员的角色是否在您的机器人角色之上。

你可以试试:

@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 等,如果这不起作用,我最好的建议是检查您的机器人是否高于您要分配的所有角色!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM