Syntax Error: 'await' outside async function in Discord.py

Viewed 472

I started coding a command in my discord bot which enables users to interact with the bot using messages. However, when I run my program, I run into this error:

'await' outside async function

I've checked out other posts which said indentation would solve the problem, but I am using a while loop in my code.

@client.command()
async def Bot (ctx):
  def Bot1():
    while True:
      Initiate = input ("Type in anything to start, type 'Quit' to end ")
      if Initiate == 'Hello':
        await ctx.send ("Hello there!")
      elif Initiate == 'Quit':
        break
        
      else:
        Responses = ['How are you doing today', 'What do you want to talk about', 'The bot is at your service']
        Response = random.choose(Responses)
        await ctx.send (Response)
  await ctx.send (Bot1())
2 Answers

your Bot1 function

  def Bot1():

is wrong.

if you are using await inside the function, in your case you are using, you must have:

async def Bot1():

edit after problem number 2:

i think your problem is this line

 await ctx.send (Bot1())

this must be converted to

 await ctx.send (Bot1)

or

 await ctx.send (await Bot1())

Do

def Bot():
   #Code ...
   return Response

Instead of

await ctx.send (Response)

since you are already sending it outside the function.

You can also remove the sending code outside, and make the function a coroutine which can be done with

async def Bot():
  #your code
Related