【问题标题】:SyntaxError: 'await' outside function | discord.py [duplicate]SyntaxError: 'await' 外部函数 | discord.py [重复]
【发布时间】:2021-08-23 06:50:32
【问题描述】:

我知道 - 这个标题有很多问题,但大多数是针对 Dark Sky 的,或者与我的问题不同。

class Counter(discord.ui.View):
    @discord.ui.button(label='0', style=discord.ButtonStyle.red)
    async def counter(self, button: discord.ui.Button, interaction: 
    discord.Interaction):
        number = int(button.label)
        button.label = str(number + 1)
        if number + 1 >= 5:
            button.style = discord.ButtonStyle.green

        await interaction.message.edit(view=self)

view = Counter()
await ctx.send('Press to increment', view=view)

错误信息:

  File "main.py", line 27
    await ctx.send('Press to increment', view=view)
    ^
SyntaxError: 'await' outside function

我对 python 很陌生,完全不知道为什么会这样。任何帮助将不胜感激!

【问题讨论】:

  • 最后一行不在异步函数内,放在异步函数内就行了
  • await 是高级 Python,你说你是 Python 新手。这很难提供帮助。
  • 完整代码请...
  • @VPfB 我认为事实是,很多人正在使用 Discord.py 进入 python(和一般编程),这需要学习至少对异步编程的表面理解。说它太先进对人们没有帮助——总会有人潜入深渊,而且这可能是最好的学习方式

标签: python discord.py


【解决方案1】:

你需要把

view = Counter()
await ctx.send('Press to increment', view=view)

异步函数内部的代码,因为 await 关键字只能在异步函数内部使用。

示例函数

async def my_function(): # this is an async function
   ...
def my_other_function(): # this is a normal function
   ...

必须使用 await 关键字调用异步函数,否则会出现异常

await my_function() # calling an async function
my_other_function() # calling a normal function

正如你在这个例子中看到的,在异步函数中,你可以使用await

async def something():
    print("hello async world")

async def my_function():
    await something()

但如果您尝试在普通函数中等待异步函数

asnyc def something():
    print("hello async world")
def my_other_function():
    await something()

您将收到SyntaxError: 'await' outside function 错误


与不和谐机器人一起执行此操作的最佳方法是将代码添加到侦听器。例如on_message 事件,每当收到消息时就会触发该事件

on_message 事件示例

import discord

client = discord.Client()

@client.event
async def on_message(message): # The event
    if message.content == "your_text" # Insert a text here that is needed to use the "command"
        # your code will be executed whenever the bot receives a message with the content "your_text"
        view = Counter()
        await message.channel.send('Press to increment', view=view)
client.run("your unique token here")

我建议在开始之前先看看https://discordpy.readthedocs.io/en/stable/quickstart.html

根据ctx,您的代码是为客户端的@commands.command 函数编写的。

(这应该可以修复您的代码)

from discord.ext import commands

bot = commands.Bot(command_prefix="!")

class Counter(discord.ui.View):
    @discord.ui.button(label='0', style=discord.ButtonStyle.red)
    async def counter(self, button: discord.ui.Button, interaction: 
    discord.Interaction):
        number = int(button.label)
        button.label = str(number + 1)
        if number + 1 >= 5:
            button.style = discord.ButtonStyle.green

        await interaction.message.edit(view=self)

@bot.command("test")
async def command(ctx):
    view = Counter()
    await ctx.send('Press to increment', view=view)

bot.run("your unique token here")

为了使用您的代码,您需要在您的机器人可以读取内容为 !test 的消息的频道中发送消息以调用命令

【讨论】:

  • 这成功了!谢谢
【解决方案2】:

您的代码的典型结构是 (using sample code from the docs)

@bot.command()
async def foo(ctx, arg):
    await ctx.send(arg)

这也是你使用的ctx 的来源——如果你删除了等待,你会得到一个ctx 没有定义的错误。您的代码将指定与 discord 上的命令或其他事件对应的函数,discord.py 将处理在用户执行这些操作时调用您指定的函数。

您只能在async 函数中使用await 关键字。这些函数背后的想法是,如果你的代码必须做一些需要很长时间的事情,但主要是等待,python 可以在它等待的时候做其他事情。对于 discord.py,由于您的很多操作都涉及等待发送消息等慢速网络操作,因此这一点非常重要。

here's one intro to the ideas behind async/await,我没有看太多,所以如果它不好或有人有更好的建议,请在 cmets 中添加链接。

【讨论】:

  • 链接的讨论是关于 javascript 中的 await,而不是 Python 中的。
  • 哎呀。很好的收获
猜你喜欢
  • 2021-03-05
  • 2022-08-21
  • 2021-06-26
  • 2021-04-25
  • 2016-08-15
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
  • 2019-08-23
相关资源
最近更新 更多