My discord bot is giving me permission errors - python

The problem is that my bot has administrator permissions and I don't see how or why I am getting permission errors. This error pops up when the bot should be giving a role to a joining member.
#bot.event
async def on_member_join(member):
welcome_channel = discord.utils.get(member.guild.text_channels, name='welcome_channel')
guild=member.guild
if guild.member_count % 10 ==1: #to find the last digit of the member count to see if st or nd or rd or th should be placed at the end
embed=discord.Embed()
embed2=discord.Embed()
name=(member)
embed.set_author(name=f"{random.choice(greetings)} {name}. You are the {guild.member_count}st member", icon_url=member.avatar_url)
await welcome_channel.send(embed=embed)
embed2.set_thumbnail(url=member.avatar_url)
await welcome_channel.send(f"{random.choice(greetings)} {member.mention}. You are the {guild.member_count}st member", embed=embed2)
The exact errors that I get are:
Traceback (most recent call last):
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "c:\Users\Lenovo\Desktop\Python\VSC\Discord Bot\Main backup.py", line 53, in on_message
await message.author.add_roles(role)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\member.py", line 777, in add_roles
await req(guild_id, user_id, role.id, reason=reason)
File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\http.py", line 248, in request
raise Forbidden(r, data)
discord.errors.Forbidden: 403 Forbidden (error code: 50013): Missing Permissions

As said in Traceback, the error probably doesn't come from on_member_join(), but from the on_message() event
You should check the order of the roles of your server. If bot's role is below the role you want to give to a user, then your bot doesn't have the permission to do so (as explained here)
Example :

Related

Add Role on Server

I intend, if you send the bot a certain DM that it then gives a role on a certain server.
This is my code what I tried:
#bot.event
async def on_message(message):
if message.author.bot:
return
elif message.content.startswith("++neunundvierzig++"):
role = message.author.guild.get_role(861543456908640298)
guild = bot.get_guild(816021180435529758)
await message.channel.send("Erfolgreich Zerifiziert!")
await member.add_roles(role)
It says in the log:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\aaa12\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "f:\HTML CSS JS Py Projekte\Python\bot_check\bot.py", line 36, in on_message
role = message.author.guild.get_role(861543456908640298)
AttributeError: 'User' object has no attribute 'guild'
I think you defined role wrong. Your code says that message.author.guild doesn't exist, so you should try message.guild.
Like this:
role = message.guild.get_role(861543456908640298)
Aside from that, you don't defined member. Just rename it to await message.author.add_roles(role) instead of await member.add_roles(role)
Sources
Discord Messages

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!

Discord.py rewrite bot won't change my nickname

I've been learning Python 3.8 by building a Discord bot for my small server of friends. I want my friends to be able to change my nickname, but Discord doesn't seem to allow that unless I give everyone the same rank.
My idea was to give my bot the highest role and let them use a bot command to change my nickname (which would be the second-highest role), but I get the following error:
Traceback (most recent call last):
File "/home/cqqlguy/.local/lib/python3.8/site-packages/discord/client.py", line 333, in _run_event
await coro(*args, **kwargs)
File "/home/cqqlguy/CQQLBOT/CQQLBOT.py", line 516, in on_message
await cqqluser.edit(nick = newnick)
File "/home/cqqlguy/.local/lib/python3.8/site-packages/discord/member.py", line 601, in edit
await http.edit_member(guild_id, self.id, reason=reason, **payload)
File "/home/cqqlguy/.local/lib/python3.8/site-packages/discord/http.py", line 241, in request
raise Forbidden(r, data)
discord.errors.Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
The relevant code looks like this:
#bot.event
async def on_message(message):
lowermess = message.content
dMess = lowermess.lower()
if dMess.startswith("$changenick "):
newnick = lowermess.split(" ", 1)[1] #whatever the user enters after the command
changeuser = guild.get_member(id) #id would be my user ID
await changeuser.edit(nick = newnick)
This seems to work on users that aren't me, but the bot has been given a higher role than everyone so I'm not sure why it's not changing it for me.

What is the Solution to MissingRequiredArgument Error in discord.py Module?

I want to create a bot that will display server information such as server name, server owner's name, total number of members, etc. whenever called upon. I read up on the module's document many times and tried many things. Regardless of all attempts, I have been getting this error whenever I invoke the bot by command. I am using Python 3.9.2 and discord.py module of version 1.6.0, by the way. How can I solve the issue? Thanks in advance!
My Code:
#bot.command()
async def server(ctx, guild: discord.Guild):
await ctx.send(guild.name)
await ctx.send(guild.owner)
await ctx.send(guild.owner_id)
await ctx.send(guild.members)
await ctx.send(guild.member_count)
Error:
Ignoring exception in command server:
Traceback (most recent call last):
File "...\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "...\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\core.py", line 856, in invoke
await self.prepare(ctx)
File "...\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\core.py", line 790, in prepare
await self._parse_arguments(ctx)
File "...\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\core.py", line 706, in _parse_arguments
kwargs[name] = await self.transform(ctx, param)
File "...\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\core.py", line 542, in transform
raise MissingRequiredArgument(param)
discord.ext.commands.errors.MissingRequiredArgument: guild is a required argument that is missing.
Do you need the guild parameter, why don't you just do:
#bot.command()
async def server(ctx):
await ctx.send(ctx.guild.name)
await ctx.send(ctx.guild.owner)
await ctx.send(ctx.guild.owner_id)
await ctx.send(ctx.guild.members)
await ctx.send(ctx.guild.member_count)
You have requested 2 arguments , ctx and guild : discord.Guild when you run the command , you have to use the command in the format !server <guild id/name> , since you didn't mention the <guild id/name> the error was triggered , either you can remove the argument as mentioned in answer given by #PythonProgrammer or you can mention the guild you want to request info of

Discord.py Bot that doesn't act like it has permissions

So I am working on a Discord Bot and trying to work on role assignments. I made sure that I am using the new rewrite system, I made sure I gave the bot administrative privileges. I have taken just about every measure I could think of and it simply is not working. I looked at the updated API to handle it, used sample code as a framework, This is the code
import os
import discord
from discord.utils import get as dget
from discord.ext.commands import Bot
from dotenv import load_dotenv
load_dotenv('key.env')
TOKEN = os.getenv('DISCORD_TOKEN')
client = Bot(command_prefix = '!')
#client.event
async def on_ready():
print('Connected')
#client.command()
async def role(ctx):
user = ctx.message.author
role = discord.utils.get(ctx.guild.roles, name="Sample")
await user.add_roles(role)
client.run(TOKEN)
and this is the error that i get:
Ignoring exception in command role:
Traceback (most recent call last):
File "C:\Users\chris\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
ret = await coro(*args, **kwargs)
File "d:/Google Drive/Coding/Khasbot/main.py", line 22, in role
await user.add_roles(role)
File "C:\Users\chris\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\member.py", line 641, in add_roles
await req(guild_id, user_id, role.id, reason=reason)
File "C:\Users\chris\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\http.py", line 221, 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 "C:\Users\chris\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\chris\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\chris\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 92, 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 honestly have no idea what could be going wrong. I have tried endless things and even created a whole new bot altogether. Still the same issue. It worked last night and literally just won't do it.
Make sure that your bot must have Manage Roles permission on your server and the role for adding roles must be lower than your bot top role.
Make you did these 2 things.

Categories

Resources