Reputation: 59
I've been learning python for a little bit and decided to take what I've learnt and build a discord bot to test my abilities. I've coded everything and it looks fine to me (which doesn't mean much, I'm sure) but I get a code for syntax error every time I try to run it. So, I scaled it back to the bare minimum and it still won't work. I've looked through a lot of questions and I see that this usually happens when you're not using an updated version of Python, but I've triple checked that I am, indeed, running Python 3.6.4.
Here's what I have:
import discord
import discord.ext.commands
from discord.ext.commands import Bot
import asyncio
bot = commands.Bot(command_prefix='!')
@bot.event
async def on_ready():
print ("Bot Online!")
print (bot.user.name)
bot.run("token was here")
and when I run it through Python Launcher, this is my error:
Kalyns-MacBook-Air:~ kalynwoodbury$ cd
'/Users/kalynwoodbury/Desktop/bot2/' && '/usr/bin/pythonw'
'/Users/kalynwoodbury/Desktop/bot2/Bot1.py' && echo Exit status: $? && exit 1
File "/Users/kalynwoodbury/Desktop/bot2/Bot1.py", line 9
async def on_ready():
^
SyntaxError: invalid syntax
Kalyns-MacBook-Air:bot2 kalynwoodbury$
What am I doing wrong?
EDIT: The application says Python 3.6. Yesterday I uninstalled it and downloaded it again, making sure to press 3.6.4 and the installer said Python 3.6.4. I have never been so sure of anything in my life. Making sure I had the right version was the absolute first thing I looked for.
EDIT: If I try to use python3 bot.py it gives me the same error code, but for that line.
Upvotes: 5
Views: 3145
Reputation:
I think the problem was in your imports. When I ran the same code I got an error saying something about the command prefix. I don't think there should be anything wrong with the async because you already imported it. Try this code:
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
bot = commands.Bot(command_prefix='!')
@bot.event
async def on_ready():
print ("Bot Online!")
print (bot.user.name)
bot.run("TOKEN GOES HERE")
I think when you used command_prefix the code was confused because there was never anything imported that had command prefix in it. Instead of doing import discord.ext.commands, you were supposed to do from discord.ext import commands.
Upvotes: 1