Discord bot Ignoring exception in on_connect - discord.py

if __name__=="__main__":
for file in os.listdir('./cogs'):
if file.endswith('.py'):
bot.load_extension(f'cogs.{file[:-3]}')
bot.run(bot.token)
Output
Ignoring exception in on_connect
Traceback (most recent call last):
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py",
**line 377, in _run_event**
await coro(*args, **kwargs)
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\bot.py",
**line 1164, in on_connect**
await self.sync_commands()
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\bot.py",
**line 738, in sync_commands**
app_cmds = await self.register_commands(
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\bot.py",
**line 531, in register_commands**
prefetched_commands = await self._bot.http.get_guild_commands(
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\http.py", line 360, in request
raise Forbidden(response, data)
**discord.errors.Forbidden: 403 Forbidden (error code: 50001): Missing Access**

Your bot needs to be invited with the applications.commands scope to sync commands in the command tree to the guild.
discord.py FAQ:
Bots need to be invited with the applications.commands scope in addition to the bot scope. For example, invite the bot with the following URL: https://discord.com/oauth2/authorize?client_id=<client id>&scope=applications.commands+bot

Related

Discord.py new timeout command error: "AttributeError: 'User' object has no attribute 'timeout_for'"

Hello all and happy new year 2022!!
Since the recent add of "timeouts" for discord, I have tried to make the timeout command following some tutorials like:
https://docs.pycord.dev/en/master/api.html?highlight=timeout#discord.Member.timeout
https://youtu.be/c5V4OaTNDtM
But I may get the following error for a reason I don't know:
Ignoring exception in command timeout2:
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 "/home/runner/Russia-Bot/moderator.py", line 42, in timeout2
await member.timeout_for(time)
AttributeError: 'Member' object has no attribute 'timeout_for'
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: AttributeError: 'Member' object has no attribute 'timeout_for'
Here's the command code, i have made 2 different commands but both give the same issue:
Variant 1:
#client.command()
async def timeout(ctx, member: discord.Member, time=None, reason=None):
time = humanfriendly.parse_timespan(time)
await member.timeout(until = discord.utils.utcnow() + datetime.timedelta(seconds=time), reason=reason)
await ctx.send (f"{member} callate un rato anda {time}")
Variant 2
#client.command(pass_context =True)
async def timeout2(ctx, member:discord.User=None, time=None):
#time = humanfriendly.parse_timespan(time)
# tiempo = datetime.timedelta(seconds=time)
user = await client.fetch_user(member)
#await ctx.send (f"{user}")
#await ctx.send (f"{member}")
await user.timeout_for(time)
await ctx.send (f"{user} callate un rato anda {time}")
Best Regards,
Engineer
Update your py-cord library by using pip install -U git+https://github.com/pycord-development/pycord
If works , pls consider accepting answer

Problem with *.shutdown* command on discord.py

I am currently looking to fix this code. I am trying to make a .shutdown command, which basically logs out of the bot and takes the bot down. I have made a code, but it seems like it is not working. Here is my code. Help is very appreciated ;p
#client.command()
async def shutdown(ctx, *, reason):
if ctx.message.author.id(581457749724889102):
ctx.send('Bot is shutting down... ;(')
logs_channel = client.get_channel(825005282535014420)
logs_channel.send(f"Bot is being shutdown for: {reason}")
exit()
else:
ctx.send("I'm sorry, only officer grr#6609 can do that."
Thanks early for the help!
edit:
here is my error
Ignoring exception in on_command_error
Traceback (most recent call last):
File "C:\Users\USER\AppData\Local\Programs\Python\Python39-32\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "c:\Users\USER\Desktop\discord bot folder\Nuova cartella\connor.py", line 173, in shutdown
if ctx.message.author.id(581457749724889102):
TypeError: 'int' object is not callable
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\USER\AppData\Local\Programs\Python\Python39-32\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "c:\Users\USER\Desktop\discord bot folder\Nuova cartella\connor.py", line 82, in on_command_error
raise error
File "C:\Users\USER\AppData\Local\Programs\Python\Python39-32\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\USER\AppData\Local\Programs\Python\Python39-32\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\USER\AppData\Local\Programs\Python\Python39-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: TypeError: 'int' object is not callable
Please always consider to await your functions. You also have some formation and comprehension errors in the code, maybe take another look at the docs
You can check whether the user executing the command is the owner of the bot. If he is not, there is of course an error message.
Have a look at the following code:
#client.command()
#commands.is_owner() # Checks if the bot owner exectued the command
async def shutdown(ctx):
await ctx.send("Logging out.")
await client.logout() # Logging out
#shutdown.error
async def shutdown_error(ctx, error):
if isinstance(error, commands.NotOwner):
await ctx.send("You are not the owner of the bot.") # Error message
What did we do?
awaited most of the functions.
Built in a check to check if the owner executed the command.
Built in an error handler which will give out a message if a non-owner tries to shutdown the bot.

User permission check throws error, but I don't know why

I've been trying for days to find an answer with no luck. I cannot get this to work, so I have to ask it here.
How do I use #commands.has_permissions? I've tried a few things.
Here is my code.
#client.command(name="role")
#commands.has_permissions(manage_roles=True)
async def addrole(ctx, user: discord.Member, role: discord.Role):
await user.add_roles(role)
await ctx.send(f"I gave {user.name} the role {role.name}")
But I keep getting this error.
Ignoring exception in command role:
Traceback (most recent call last):
File "C:\Users\unkno\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "c:/Users/unkno/Desktop/Code_Tests/Python/discord_bot/bot2.py", line 17, in addrole
await user.add_roles(role)
File "C:\Users\unkno\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\member.py", line 641, in add_roles
await req(guild_id, user_id, role.id, reason=reason)
File "C:\Users\unkno\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\http.py", line 241, 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\unkno\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\unkno\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\unkno\AppData\Local\Programs\Python\Python38\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: Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
Please help.
Thanks in advance!
The reason why you're getting the exception is because the bot is missing the Manage Roles permission.
You have set it here:
#commands.has_permissions(manage_roles=True)
To fix this simply give your bot the permissions. You can also handle the exception by adding this to your code:
#addrole.error
async def addrole_error(ctx, error):
if isinstance(error, commands.errors.ErrorNameHere):
await ctx.send("You don't have permission to execute this command!")
You will just have to replace ErrorNameHere with the name of your error.

This code for changing roles doesn't work

I got this code to change a user's role, but it won't work.
#client.command(pass_context=True)
async def ruleBreak(ctx):
member = ctx.message.author
role = discord.utils.get(member.server.roles, name="RuleBreakers")
await client.add_roles(member, role)
But it's causing this error
Ignoring exception in command ruleBreak:
Traceback (most recent call last):
File "C:\Users\unkno\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "c:/Users/unkno/Desktop/Code_Tests/Python/discord_bot/WIP/bot2.py", line 19, in ruleBreak
role = discord.utils.get(member.server.roles, name="RuleBreakers")
AttributeError: 'Member' object has no attribute 'server'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\unkno\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\unkno\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\unkno\AppData\Local\Programs\Python\Python38\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: 'Member' object has no attribute 'server'
What do I do, why is this happening, and where do I change it?
Thanks in advance!
You don't need to pass context as it's done for you in the rewrite branch.
#client.command()
async def ruleBreak(ctx):
member = ctx.message.author
role = discord.utils.get(member.guild.roles, name="RuleBreakers")
await discord.Member.add_roles(member, role)
EDIT: The main issue here was that Client.add_roles changed to Member.add_roles() in the rewrite version. Migrating to v1.0

How do I fix this error with kick command

I wanted to make an kick command but i ran into this error but i couldnt find out how to fix it.
I tried giving permissions to the bot. And giving permissions to me but it all didnt work.
This is my code:
#bot.command(name='kick', aliases=['Kick'])
#commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member):
await ctx.send('What is the reason?')
msg = await bot.wait_for('message')
reason = msg.content
description = f'''
**Member:** = {member}
**Responsible moderator:** {ctx.author.mention}
**Reason:** {reason}
'''
embed = discord.Embed(title='Kick', description=description)
await ctx.send(content=None, embed=embed)
await member.kick(reason=reason)
This is the error:
Traceback (most recent call last):
File "C:\Users\Guido\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
ret = await coro(*args, **kwargs)
File "c:/Users/Guido/Desktop/yeet/overig/coding/discord bots/self coded/oden/code/Oden.py", line 80, in kick
await member.kick(reason=reason)
File "C:\Users\Guido\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\member.py", line 489, in kick
await self.guild.kick(self, reason=reason)
File "C:\Users\Guido\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\guild.py", line 1627, in kick
await self._state.http.kick(user.id, self.id, reason=reason)
File "C:\Users\Guido\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\Guido\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\Guido\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\Guido\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```
It looks like the error is that the bot does not have the permission to kick a user in that server. Make sure that the bot has the Kick Members permission. The bot having permission is the only thing that matters from the API's perspective

Resources