gitrog
gitrog

Reputation: 47

How do I run async code in a Python function?

So, I have a Python program using discord.py where I am trying to have functions manage different message commands, but when I put the "await" in the function, it gives me a syntax error because it's not in the async. Any way I can get around this?

def selectedCommand(message):
    await client.send_message(stuff in here)

@client.event
async def on_message(message):
    selectedCommand(message)

@client.event
async def on_edit_message(message):
    selectedCommand(message)

Upvotes: 1

Views: 140

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 61063

You need to make selectedCommand a coroutine (an async def function) as well, and then await it when it's called:

async def selectedCommand(message):
    await client.send_message(stuff in here)

@client.event
async def on_message(message):
    await selectedCommand(message)

Upvotes: 1

Related Questions