python - 语法错误 : 'await' outside async function in Discord. py

标签 python discord.py

我开始在我的 Discord 机器人中编写一个命令,该命令使用户能够使用消息与机器人进行交互。然而,当我运行我的程序时,我遇到了这个错误:

在异步函数之外“等待”

我查看了其他帖子,这些帖子说缩进可以解决问题,但我在代码中使用了 while 循环。

@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())

最佳答案

你的 Bot1 功能

  def Bot1():

错误。

如果您在函数内使用 await,在您使用的情况下,您必须:

async def Bot1():

在问题 2 之后编辑:

我认为你的问题是这一行

 await ctx.send (Bot1())

这必须转换为

 await ctx.send (Bot1)

 await ctx.send (await Bot1())

关于python - 语法错误 : 'await' outside async function in Discord. py,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70729414/

相关文章:

python - Pandas 根据其他列的条件进行分组求和

python - 在 Python 中绘制随机过程

python - Discord.py 时间表

python - 运行时警告 : coroutine was never awaited

python-3.x - 在应用程序中为 discord.py 安装 ssl 证书

python - 如何使自定义状态discord.py

Python:根据模式创建多个新列

Python:在没有for循环的情况下将值附加到列表

python - 从函数返回值并插入到 Entry 小部件

python - discord.py 邀请 - approximate_presence_count API 逐渐变慢