Yesterday my Discord bot was working fine. But after updating discord.py to use slash commands, it’s giving tons of errors. I’m new to Python, so I’m not sure if I messed up or if something’s wrong with the library.
Here’s my code:
import discord
from discord.ext import commands
from secretkeys import *
bot = commands.Bot(command_prefix=['?', '#', '+', '%', '@'])
@bot.event
async def on_ready():
print('Bot is online!')
print('-------------')
@bot.event
async def greet(ctx):
await ctx.send('Hi there!')
@bot.event
async def on_new_member(member):
with open('welcome.jpg', 'rb') as f:
img = discord.File(f)
channel = bot.get_channel(123456789012345678)
await channel.send('Welcome! Take this to start your journey!', file=img)
@bot.event
async def farewell(ctx):
await ctx.send('See you later!')
@bot.event
async def kaboom(ctx):
with open('explosion.gif', 'rb') as f:
anim = discord.File(f)
await ctx.send('Kaboom!', file=anim)
bot.run(SECRET_TOKEN)
When I run it, I get a bunch of errors about unauthorized access and closed event loops. Can someone explain what’s going on and how to fix it? I’ve tried moving code around but can’t figure out the problem. If you need more info, just ask!
I’ve dealt with similar issues when updating Discord libraries. It seems the main problem is that you’re using @bot.event for command functions, which isn’t correct. Commands should use @bot.command() instead; only on_ready and on_new_member should remain as events.
You’ll also need to update your bot initialization to include intents:
Make sure you’ve enabled the necessary intents in the Discord Developer Portal as well. These changes should resolve most of your errors. If issues persist, double-check your token and permissions.
I’ve encountered similar issues when updating discord.py. The problem likely stems from the transition to slash commands, which requires some adjustments to your code structure.
First, you’ll need to change your bot initialization to include intents:
Also, your event decorators should be @bot.command() instead of @bot.event for most of your functions. The on_ready and on_new_member can stay as events.
For slash commands, you’ll need to use @bot.tree.command() and add a sync step after the bot is ready.
Lastly, ensure you’re using the correct token and have the necessary permissions in your Discord Developer Portal. These changes should resolve most of the errors you’re seeing. Let me know if you need more specific guidance!
yo, sounds like a headache! i had similar probs when updatin discord.py. try changin @bot.event to @bot.command() for most of ur functions. also, add intents when initializing the bot. might need to tweak some stuff in the developer portal too. if ur still stuck, hit me up and we can troubleshoot togther!