【问题标题】:Discord.py add_roles problemsDiscord.py add_roles 问题
【发布时间】:2021-05-02 16:24:55
【问题描述】:

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

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)

【问题讨论】:

  • 您是否要向服务器所有者添加角色?
  • 不,我不是,我正在尝试将其添加到加入的新成员中。不过,这是我的 alt 帐户,这会有影响吗?

标签: python discord discord.py


【解决方案1】:

我没有发现您的代码有任何问题。您说机器人具有add roles 权限,但您可以尝试检查您的机器人赋予成员的角色是否高于您的机器人角色。

【讨论】:

  • 在上面是否意味着它高于机器人的角色?所以就像在服务器设置角色侧面板中一样,我是否需要让机器人的角色高于它试图赋予的角色。
  • @Cyber​​-Yosh 机器人的角色必须高于您要通过机器人分配的角色。
【解决方案2】:

你可以试试:

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

【讨论】:

  • 代码没有问题,只是机器人的角色低于我试图分配的角色,不过谢谢。
猜你喜欢
  • 2020-12-26
  • 2021-06-04
  • 1970-01-01
  • 2021-05-18
  • 2019-04-04
  • 1970-01-01
  • 1970-01-01
  • 2021-04-18
  • 2018-09-16
相关资源
最近更新 更多