【问题标题】:Python - AttributeError: 'function' object has no attribute 'author'Python - AttributeError:'function'对象没有属性'author'
【发布时间】:2021-05-08 14:53:56
【问题描述】:

我是 python 和一般编程的新手,我正在创建一个使用 Schoolsofts API 的不和谐机器人(Schoolsoft 是一个面向瑞典学生的网站,您可以在其中获取有关成绩等信息)。

所以我在下面有一个函数:

async def response(): 
    embed = discord.Embed(
    title = 'Matsedel',
    description = '',
    colour = discord.Colour.blue()
    )
    embed.set_author(name='Schoolsoft')
    embed.add_field(name='1', value=lunch1[25], inline=False)
    embed.add_field(name='2', value=lunch1[20], inline=False)
    embed.add_field(name='3', value=lunch1[22], inline=False)
    embed.add_field(name='4', value=lunch1[7], inline=False)
    embed.add_field(name='5', value=lunch1[23], inline=False)

然后我有一个命令可以调用这个函数

@client.command(name='matsedel', help='This returns matsedeln for the week')
async def matsedel(ctx):    
    await ctx.send(response())
    await client.process_commands(response)

当我输入不和谐的命令时,机器人会回复 <coroutine object response at 0x000002CD...> 并且我收到一条错误消息说

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'function' object has no attribute 'author'".

你们知道问题出在哪里吗?提前致谢

【问题讨论】:

  • 你能发布完整的跟踪吗?

标签: python bots discord.py


【解决方案1】:

response 函数是一个协程,如果你尝试调用协程而不等待它,你会得到 <coroutine object func at 0x00000...> 输出

>>> async def foo():
...      return "Foo function"
... 
>>> foo()
<coroutine object foo at 0x...>

要真正得到你需要await它的结果,还要记住协程

中只有await函数
>>> await foo() # Not awaiting inside another coroutine for explanation purposes
"Foo function"

顺便说一句,我看到你并没有真正返回 response func 中的嵌入

async def response():
    # ...
    return embed

您的代码已修复

@client.command(name='matsedel', help='This returns matsedeln for the week')
async def matsedel(ctx):
    embed = await response()
    await ctx.send(embed=embed)

另外你不需要在命令末尾加上process_commands,你只需要把它放在on_message事件中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 2022-01-26
    • 2019-05-17
    • 2017-07-04
    • 2014-06-19
    • 1970-01-01
    相关资源
    最近更新 更多