简体   繁体   中英

Discord.py add_roles problems

I am trying to make a bot that gives a role to members when they join. However, it keeps coming up with the error message

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

I have given it the "manage roles permission (see below), but it still comes up with this error. 在此处输入图像描述

Is there a fix for this? Also my python code:

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)

I didn't see any problem on your code. You said bot has add roles permission but can you try to check the role that your bot gives to member is on top of your bot's role.

You could try:

@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}!")

This should work without having to pull channel IDs and whatnot, If this doesn't work, my best advice would be checking that your bot is above all the roles you are trying to assign!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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