【问题标题】:SyntaxError: 'await' outside function even tho it is inside a asyncSyntaxError:“等待”外部函数,即使它在异步内部
【发布时间】:2021-05-27 19:59:54
【问题描述】:

我正在尝试用 python 制作我的第一个 discord 机器人,它是一个经济型机器人,但它说我不能使用 await 函数,我不知道为什么。

@bot.command()
async def withdraw(ctx,amount = None):
    await open_account(ctx.author)

  if amount == None:
      await ctx.send("Please enter a valid amount")
      return
  
bal = await update_bank(ctx.author)

amount = int(amount)
if amount>bal[1]:
    await ctx.send("You don't have enough potatoes!")
    return
if amount<0:
    await ctx.send("Can only send positive potatoes! No negative!")
    return

await update_bank(ctx.author,amount)
await update_bank(ctx.author,-1*amount, "bank")

await ctx.send(f"You withdrew {amount} potatoes!")

【问题讨论】:

  • 如果这是 discord.rewritten,那么要么不要使用 async,要么确保你使用的是正确的 python 版本。

标签: python discord discord.py


【解决方案1】:

好的,所以我看到了 2 个问题。第一个是@bot.command 必须与async def 对齐,第二个是你有6 个等待函数。请指定哪个 await 函数给您错误。

【讨论】:

    【解决方案2】:

    答案只是将@bot.commandasync def 对齐

    【讨论】:

    • 而且,大概是为了纠正其余的缩进问题。您不能在async def ... 函数之外使用await()。你从bal = await update_bank(ctx.author)(根本没有缩进)开始的行在函数之外,所以这六个等待会引发编译错误。
    • 请点击我的答案上的复选标记,以便此问题有答案
    猜你喜欢
    • 1970-01-01
    • 2021-06-26
    • 2021-01-13
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    相关资源
    最近更新 更多