Hey everyone! I’m working on a Discord bot and I’m running into a problem. I’m trying to make the bot ignore its own messages, but I keep getting a NameError saying ‘message’ isn’t defined. Here’s what my code looks like:
@bot.event
async def on_message(event):
# Attempt to ignore our own messages
if event.author == event.bot.user:
return
if event.content.lower() == 'hello':
await event.channel.send('Hi there!')
I’ve tried changing event.author
to str(event.author)
, but it’s still not working. Any ideas what I’m doing wrong? I’m pretty new to Discord bot development, so I might be missing something obvious. Thanks in advance for any help!
I’ve encountered a similar issue when developing Discord bots. The problem lies in how you’re referencing the event object. In the on_message event, you should use ‘message’ instead of ‘event’. Try modifying your code like this:
@bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.lower() == 'hello':
await message.channel.send('Hi there!')
This should resolve the NameError you’re experiencing. The ‘message’ parameter contains all the information about the received message, including the author and content. Make sure you’re also using the correct bot instance name in your code. If you’re still having issues, double-check your bot’s permissions and make sure it has the necessary access to read messages in the channels where you’re testing it.
The issue you’re facing is quite common for newcomers to Discord bot development. The parameter in the on_message event should be ‘message’, not ‘event’. Here’s the corrected version of your code:
@bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.lower() == 'hello':
await message.channel.send('Hi there!')
This should resolve the NameError. Remember, consistency in naming is crucial in Python. Always ensure you’re using the correct object names throughout your code. If you’re still encountering issues, double-check your bot’s permissions and make sure it’s properly connected to your Discord server.
hey there! looks like ur mixing up event and message. Try changing all ‘event’ to ‘message’ in ur code. like this:
@bot.event
async def on_message(message):
if message.author == bot.user:
return
should fix the nameerror. good luck with ur bot!