Discord bot throws syntax error when starting up

I’m just getting started with coding and trying to build my first Discord bot. Whenever I attempt to run the script, it crashes with a syntax error message. I’m not sure what’s causing this issue since I followed a tutorial online.

My code:

import discord
from discord.ext import commands
import asyncio

bot_token = "TOKEN_HERE"
command_prefix = "!"
my_bot = commands.Bot(command_prefix=command_prefix)

@my_bot.event
async def on_ready():
    print("Bot is running!")
    print(f"Bot name: {my_bot.user.name}")
    print(f"Bot ID: {my_bot.user.id}")

@my_bot.command()
async def hello(context):
    await context.send("Hello there!")

my_bot.run(bot_token)

Error message:

Traceback (most recent call last):
  File "discord_bot.py", line 1, in <module>
    import discord
  File "/usr/local/lib/python3.7/site-packages/discord/__init__.py", line 20, in <module>
    from .client import Client, AppInfo, ChannelPermissions
  File "/usr/local/lib/python3.7/site-packages/discord/client.py", line 38, in <module>
    from .state import ConnectionState
  File "/usr/local/lib/python3.7/site-packages/discord/state.py", line 36, in <module>
    from . import utils, compat
  File "/usr/local/lib/python3.7/site-packages/discord/compat.py", line 32
    create_task = asyncio.async
                              ^
SyntaxError: invalid syntax

Any help would be appreciated!

Same thing happen’d to me when I started with Discord bots! Your discord.py version is outdated and doesn’t play nice with newer Python. Run pip uninstall discord.py then pip install discord.py==2.3.2 - that’ll fix the syntax error. Don’t forget to swap in your actual bot token where it says TOKEN_HERE!

Had this exact same issue when setting up my first bot last year. You’re using incompatible versions - your discord.py is too old for your Python version. The asyncio.async function got removed in newer Python versions but your discord.py keeps trying to call it. Here’s what fixed it for me: completely uninstall the old version with pip uninstall discord and pip uninstall discord.py, then do a fresh install with pip install discord.py>=2.0. Also check your Python version with python --version since some discord.py versions need specific Python versions. Your bot code looks fine, so once you sort out the library versions it’ll work perfectly.

This is a version compatibility issue between Python and discord.py. The error’s happening because asyncio.async was deprecated and removed in Python 3.9+, but your discord.py version still tries to use it. You’re running newer Python with older discord.py. I hit this exact same problem when I upgraded Python but forgot to update my dependencies. Try pip install --upgrade discord.py first. If that doesn’t work, use pip install py-cord instead. Also check your Python version - discord.py 1.7.3+ needs Python 3.6-3.9, while discord.py 2.0+ supports Python 3.8+.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.