I’m working on a Discord bot in Python and running into some issues. Every time I try to run my code, I get a traceback error that I can’t figure out. I’m pretty new to Discord bot development so I might be missing something obvious.
Here’s the code I’m using:
import discord
import os
bot = discord.Client()
@bot.event
async def on_ready():
print('Bot is now online as {0.user}'.format(bot))
@bot.event
async def on_message(msg):
if msg.author == bot.user:
return
if msg.content.startswith('adventure begin'):
await msg.channel.send('Welcome to Fantasy Quest! Your epic journey starts now!')
bot.run(os.getenv('BOT_TOKEN'))
The error keeps popping up when I try to start the bot. Has anyone dealt with similar traceback issues before? What am I doing wrong here? Any help would be really appreciated since I’m stuck on this.
hey claire, looks like ur bot token might be an issue. try printing os.getenv('BOT_TOKEN') to see if it’s set correctly. also make sure you’ve got message content enabled in the discord dev portal. good luck!
Had the same issue when I started with Discord bots. Your traceback is likely an intents problem. You’re using discord.Client() but need to pass the intents parameter explicitly. Change it to bot = discord.Client(intents=discord.Intents.default()) and add intents.message_content = True before creating the client if you want to read messages. Also check your BOT_TOKEN environment variable - I once spent hours debugging a typo in my .env file. Discord’s API got stricter recently, so older tutorials miss this.
Your code looks fine, but traceback errors usually stem from a few common issues. First, ensure your bot token is set correctly in your environment variables; a wrong or missing token can cause the bot to fail on startup. Also, verify that you have enabled the message content intent in the Discord Developer Portal under your bot’s settings, as recent updates require this to read messages properly. Finally, if you’re using discord.py version 2.0 or higher, make sure you initialize your bot with discord.Client(intents=discord.Intents.default()) and enable message content intents in your code.