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!
Related
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 :
I want to make a discord bot. Here is what i want: When i say '!yoklama 11.10.2020.txt'(or something) bot sends me a list of participants of which voice channel i am in.[not 1 channel(which i am in)] But i dont know how can i do.
Then i was some research and i find similar things about my request.
But they did not work properly.
Here is my found codes:
import discord
from discord.ext.commands import Bot
from discord.ext import commands
client = commands.Bot(command_prefix='!')
#client.command()
async def attendance(ctx, filename):
channel = client.get_channel(755129236397752441) # Replace with voice channel ID
with open(filename, 'w') as file:
for member in channel.members:
file.write(member.name + '\n')
client.run('mytoken')
This codes are working(when i run they dont give me error) but when i say !attendence test.txt bot
does not say anything and my python shell say to me:
Ignoring exception in command attendance:
Traceback (most recent call last):
File "C:\Users\Yunus Emre KISA\AppData\Local\Programs\Python\Python38-32\lib\site-
packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(args, **kwargs)
File "C:\bot2\bot2.py", line 10, in attendance
for member in channel.members:
AttributeError: 'NoneType' object has no attribute 'members'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Yunus Emre KISA\AppData\Local\Programs\Python\Python38-32\lib\site-
packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Yunus Emre KISA\AppData\Local\Programs\Python\Python38-32\lib\site-
packages\discord\ext\commands\core.py", line 859, in invoke
await injected(ctx.args, **ctx.kwargs)
File "C:\Users\Yunus Emre KISA\AppData\Local\Programs\Python\Python38-32\lib\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: AttributeError:
'NoneType' object has no attribute 'members
You see guys i dont understand anything please help me.
I found these question but it does not work for me:
Discord Python bot creating a file of active member in a vocal channel
#client.command()
async def test(ctx):
guild = ctx.guild
channel = discord.utils.get(guild.voice_channels, name='General')
for member in channel.members:
print('test')
What I would do instead of having to pass in the file in the command, is just in the function writing:
with open("file.txt", "w") as f:
I’ve been tormented by this error for a week, I seriously don’t understand what’s the matter, help plz
'int' object has no attribute 'id'
there is my code:
import discord
import random
import asyncio
from discord import Member
from discord.utils import get
from discord.ext import commands
from discord.ext.commands import Bot
from discord.ext.commands import check
from discord.ext.commands import has_role
ROLE = 730459135170183170
bot = commands.Bot(command_prefix='$')
#bot.command(pass_context = True)
#commands.has_role(730459135170183170)
async def use_shovel(ctx,user: discord.Member):
x = random.randint(1,50)
role = get(user.guild.roles, name =ROLE)
if x == 1:
await ctx.send("You've found REQUIEM ARROW!")
await bot.author.add_roles(731166197030322216)
else:
await ctx.send("meh, nothing")
await user.remove_roles(731166197030322216)
bot.run(TOKEN)
Ignoring exception in command None:
discord.ext.commands.errors.CommandNotFound: Command "use_shove" is not found
Ignoring exception in command use_shovel:
Traceback (most recent call last):
File "C:\Python\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
ret = await coro(*args, **kwargs)
File "meh.py", line 24, in use_shovel
await user.remove_roles(731166197030322216)
File "C:\Python\lib\site-packages\discord\member.py", line 685, in remove_roles
await req(guild_id, user_id, role.id, reason=reason)
AttributeError: 'int' object has no attribute 'id'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Python\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
await ctx.command.invoke(ctx)
File "C:\Python\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Python\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: AttributeError: 'int' object has no attribute 'id'
I really cannot understand why this error happen. So,how can I fix this?I
There are multiple errors in your code :
First, you're looking for a role with 730459135170183170 as a name, so your role variable's value will be None.
ROLE = 730459135170183170
(...)
role = get(user.guild.roles, name =ROLE)
Then, both add_roles and remove_roles take a role object as argument, not an int.
await bot.author.add_roles(731166197030322216)
(...)
await user.remove_roles(731166197030322216)
Finally, never ever share your token on any website:
bot.run('TOKEN')
You should go to your discord bot app and create a new token, so you're the only one who can use your bot app.
To solve your problem, you can get rid of your ROLE variable and replaces these lines:
#commands.has_role("Exact role name")
(...)
role = get(user.guild.roles, name="Exact role name")
(...)
await bot.author.add_roles(role)
(...)
await user.remove_roles(role)
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.
I'm trying do a simple bot with subclass to handler the commands but i got this error
Here Are the TraceBack:
Ignoring exception in command teste:
Traceback (most recent call last):
File "/home/mrtrue/.local/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 863, in invoke
await ctx.command.invoke(ctx)
File "/home/mrtrue/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 721, in invoke
await self.prepare(ctx)
File "/home/mrtrue/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 685, in prepare
await self._parse_arguments(ctx)
File "/home/mrtrue/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 599, in _parse_arguments
transformed = await self.transform(ctx, param)
File "/home/mrtrue/.local/lib/python3.8/site-packages/discord/ext/commands/core.py", line 445, in transform
raise MissingRequiredArgument(param)
discord.ext.commands.errors.MissingRequiredArgument: ctx is a required argument that is missing.
And here are the code:
import discord
from discord.ext import commands
import inspect
class BotLibertarin(commands.Bot):
client = discord.Client()
#client.event
async def on_message(self,message):
print(f"message from {message.author} what he said {message.content}")
await self.process_commands(message)
class CommandsHandler(BotLibertarin):
def __init__(self):
super().__init__(command_prefix=".")
members = inspect.getmembers(self)
for name, member in members:
if isinstance(member,commands.Command):
if member.parent is None:
self.add_command(member)
#commands.command()
async def teste(self,ctx):
await ctx.channel.send("teste")
I really suggest that you spend time reading the docs.
I'd also remind you that it's against ToS to send messages without a specific command being issued (such as !commandname) [Even though no one follows ToS apparently].
Either way. Try this:
import discord
from discord.ext import commands
client = commands.Bot(command.prefix='!')
#client.event
async def on_message(message):
print(f"message from {message.author} what he said {message.content}")
await message.channel.send(message.content)
#client.command()
async def teste(ctx):
await ctx.channel.send("teste")