Python - AttributeError: 'function' object has no attribute 'author'

2

I'm new to python and programming in general and I'm creating a discord bot that uses Schoolsofts API (Schoolsoft is a website for Swedish students where you can get information about grades and such).

So I have a function below:

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)

And then I have a command to call on this function

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

When I type the command in discord, the bot responds with <coroutine object response at 0x000002CD...> And I get a error message saying

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

Do you guys know what the problem could be? Thanks in advance

python
bots
discord.py
asked on Stack Overflow Feb 4, 2021 by Benjamin • edited Feb 4, 2021 by Łukasz Kwieciński

1 Answer

2

The response function is a coroutine, if you try calling a coroutine without awaiting it you get the <coroutine object func at 0x00000...> output

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

To actually get the result you need to await it, also remember to only await functions inside coroutines

>>> await foo() # Not awaiting inside another coroutine for explanation purposes
"Foo function"

By the way, I'm seeing that you didn't really return the embed in the response func

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

Your code fixed

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

Also you don't need the process_commands at the end of the command, you only put it in the on_message event

answered on Stack Overflow Feb 4, 2021 by Łukasz Kwieciński • edited Feb 4, 2021 by Łukasz Kwieciński

User contributions licensed under CC BY-SA 3.0