Discord bot development: Encountering syntax error with async function

I’m new to Python and trying to make a Discord bot. I’ve stripped down my code to the basics, but I’m still getting a syntax error. Here’s what I’ve got:

import discord
from discord.ext import commands
import asyncio

my_bot = commands.Bot(prefix='>')

@my_bot.event
async def bot_ready():
    print('Bot is up and running!')
    print(f'Logged in as {my_bot.user.name}')

my_bot.run('bot_token_goes_here')

When I run this, I get a syntax error pointing to the async def bot_ready(): line. I’m pretty sure I’m using Python 3.6.4, which should support async functions. I’ve even reinstalled Python to make sure.

I’ve tried running it with python3 bot_script.py, but I get the same error. What could be causing this? Any ideas on how to fix it?

I’ve encountered similar issues while developing Discord bots. The problem likely stems from the event registration. Try modifying your code to use the correct event name:

@my_bot.event
async def on_ready():
print(‘Bot is up and running!’)
print(f’Logged in as {my_bot.user.name}')

Also, ensure you’re using the latest discord.py version by running ‘pip install -U discord.py’. If the error persists, check your Python installation. You can verify the version with ‘python --version’ in the command line. Sometimes, system PATH issues can cause unexpected behavior. Consider using a virtual environment for a clean setup.

Hey there, I’ve been working with Discord bots for a while now, and I think I see what’s going on with your code. The issue isn’t with the async function itself, but with the event decorator. For the ‘on_ready’ event, you need to use @my_bot.event instead of @my_bot.event(). Also, the function name should be ‘on_ready’, not ‘bot_ready’. Here’s how I’d modify your code:

@my_bot.event
async def on_ready():
    print('Bot is up and running!')
    print(f'Logged in as {my_bot.user.name}')

This should resolve the syntax error you’re encountering. Make sure you’re using the latest version of discord.py as well. If you’re still having trouble, double-check your Python version with ‘python --version’ in the command line. Let me know if this helps!

yo mikezhang, i had similar issues when i started. try changing ur function name to ‘on_ready’ instead of ‘bot_ready’. that fixed it for me. also make sure ur using the latest discord.py version. good luck with ur bot!