0

When ever I run the code I get an error with async, I have imported asyncio to attempt to fix this but the errors keep flooding in

import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio

bot = command.Bot(command_prefix="=")

@bot.event
async def on_ready():
    print ("Bot Onine!")
    print ("Hello I Am " + bot.user.name)
    print ("My ID Is " + bot.user.id)

The error I get:

async def on_ready():
    ^
SyntaxError: invalid syntax

Please if anyone knows a fix

1 Answer 1

1

To use the async keyword you need python 3.5 or above. If you're using python 3.4 or below you can't use async.

the alternative is to decorate the method with coroutine. This code

@bot.event
async def on_ready():
    ...

Becomes:

@bot.event
@asyncio.coroutine
def on_ready():
    ....

Since discord events already need the bot.event decorator, discord.py api provides the async_event decorator to do both the coroutine and the event decoration in a single call, so you can also write it like this:

@bot.async_event
def on_ready():
    ....

In the same vein, you can't use await either, so you must use yield from instead.

await bot.say('Some response')

becomes

yield from bot.say('Some response')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.