I’m trying to build my first Discord bot using Python and I keep running into an issue with the event decorator. I followed a tutorial but something seems wrong with my setup.
Here’s the code I’m using:
import discord
BOT_TOKEN = 'MY_SECRET_TOKEN'
bot = discord.Client()
@bot.event()
async def on_ready():
print(bot.user + " is now online")
bot.run(BOT_TOKEN)
When I try to run this script, I get this error message:
Traceback (most recent call last):
File "/home/user/discord_projects/mybot/main.py", line 8, in <module>
@bot.event()
TypeError: event() missing 2 required positional arguments: 'self' and 'coro'
I’m not sure what’s causing this problem. The tutorial I followed seemed straightforward but clearly I’m missing something important about how the event decorator works.
Two issues with your code. First, drop the parentheses from @bot.event() - should just be @bot.event. Those parentheses make Python think you’re calling the event method, which needs arguments you’re not giving it. Second, your print statement will throw a TypeError since you can’t concatenate a User object with a string using +. Fix it with print(f"{bot.user} is now online") or convert the user object to string first. Made these same mistakes when I started discord.py last year. The decorator syntax trips everyone up initially, but once you get that @bot.event doesn’t need parentheses, it all makes sense.
totally feel ya! i had the same issue. just use @bot.event without those parentheses, it confuses Python into thinking you’re calling the method. and yeah, your print statement needs fixing - can’t just add the user obj to a string like that.
The issue originates from the parentheses following @bot.event. Using parentheses makes it seem like you’re calling the method as a function rather than applying the decorator. Simply use @bot.event without those parentheses. Additionally, be mindful of your print statement; concatenating a User object directly with a string with + will lead to errors. Instead, format it as print(f"{bot.user} is now online") or use print(str(bot.user) + " is now online"). I encountered similar mistakes with discord.py initially, as other libraries tend to treat decorators differently.