Ask coding questions

← Back to all posts
await error
h
RmyDahGod (19)

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 (122) [earned 5 cycles]
View Answer
Comments
hotnewtop
KHZ (122)

@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.

RmyDahGod (19)

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

RmyDahGod (19)

@KHZ ight, it’s in public.

KHZ (122)

@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.

19wintersp (1121)

You can only use await inside of an async function.

19wintersp (1121)

@xxpertHacker That's unrelated, this is Python

xxpertHacker (855)

@19wintersp How was I supposed to have known?