【问题标题】:Why is the function giving an error? discord.py为什么函数会出错?不和谐.py
【发布时间】:2021-08-23 10:59:10
【问题描述】:
def test():
    await message.author.send('ehy')

@bot.event
async def on_message(message):
        if message.author.id == 2980828228298:
            test()

我不明白为什么这个简单的功能不起作用。错误是“等待”。有什么帮助吗?

【问题讨论】:

  • 请分享完整的错误。
  • 我什至无法启动程序,出现语法错误:'await' outside async function
  • await 关键字只允许在标有 async 的函数中使用。
  • 你能给我举个例子吗?这是我第一次使用函数。

标签: python function discord discord.py


【解决方案1】:

我不是discord.py大师,其实我什么都不知道。虽然我明白为了使用await,它必须在async 函数内。使用您提供的代码,我想您可以在 test() 函数中返回一个字符串。

def test():
    return 'ehy'

@bot.event
async def on_message(message):
        if message.author.id == 2980828228298:
            await message.author.send(test())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-04
    • 2019-03-12
    • 2021-09-27
    • 1970-01-01
    • 2020-12-18
    • 2021-11-20
    • 2020-10-15
    相关资源
    最近更新 更多