Skip to content
← Back to Community
await error
Profile icon
RmyDahGod

hi. i am trying to make a discord chat bot.
when i put in “await”
there is this error appearing : Syntax Error : ‘await’ outside function

Answered by KHZ [earned 5 cycles]
View Answer
Voters
Profile icon
HelderHouben
Profile icon
Arcccc
Profile icon
RmyDahGod
Comments
hotnewtop
Profile icon
KHZ

@RmyDahGod Btw your repl is private so we can't look at your code. If you don't want us to see the code try attaching a screenshot of the code where the error is so we can take a look at the error.

Profile icon
RmyDahGod

@KHZ ohh shoot i didn’t see, let me put it in public

Profile icon
RmyDahGod

@KHZ ight, it’s in public.

Profile icon
KHZ

@RmyDahGod Yup the problem is that you are using await outside of an async function. The last two lines should be like this:

@client.event async def on_message(msg): if msg.author == client.user: return if msg.startswith('hello'): await ctx.send('yo, whats good.')

I also made some necessary changes. And you should put the token in a .env file so that even if the repl is public you don't need to remove the token to show it to someone.

Profile icon
19wintersp

You can only use await inside of an async function.

Profile icon
19wintersp

@xxpertHacker That's unrelated, this is Python

Profile icon
xxpertHacker

@19wintersp How was I supposed to have known?

Screenshot 2021-02-08 at 7.06.57 AM