2

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())
1
  • 3
    Bot1 isn't async but you have awaits within it. Commented Jan 16, 2022 at 11:01

2 Answers 2

2

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())
Sign up to request clarification or add additional context in comments.

4 Comments

I tried what you did, and the it sends me this message: RuntimeWarning: coroutine 'bot.<locals>.Bot1' was never awaited. Any idea what it means?
check the question. i've made some edits
seems to work, but it prints it on the terminal instead of the channel :(
i dont know, man, its related to discord API. but one thing is sure. you cant have await in non-async func
0

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

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.