0

I'm making a bot for discord. It's basically the same problem as this one: How can I run an async function using the schedule library?

except nothing worked in the link above. In my case I'm getting "myfunction was never awaited"

async def birthday():        
    channel = client.get_channel(xxxxxx)
    fileName = open("birthdayFile.txt", "r")
    today = time.strftime('%m%d')

    for line in fileName:
            if today in line:
                    line = line.split(' ')

                    line[-1] = line[-1].strip()
                    if line[-1] != line[1]:
                            bdayperson = line[1]+' '+line[2]
                    else:
                            bdayperson = line[1]

                    await channel.send(f"Happy Birthday to "+ bdayperson + "! " )


schedule.every().day.at("18:36").do(client.loop.call_soon_threadsafe, birthday)
soerface
  • 6,417
  • 6
  • 29
  • 50
ivey kun
  • 17
  • 1
  • 7
  • Can you please show the code in which you tried to follow the advices from answers to the [other question](https://stackoverflow.com/questions/51530012/how-can-i-run-an-async-function-using-the-schedule-library)? As you point out yourself, this question is a duplicate, but maybe the answers in the other one could be improved to more clearly show how to resolve the issue. – user4815162342 Feb 24 '20 at 12:48

1 Answers1

0

Your birthday method is an async method also called coroutines. This is different from the "normal"/non-async methods.

The schedule library you are using only works with non-async methods. You have to wrap your birthday async method in a normal method. The normal method can be called by your schedule library.

However in order for the discord.py library to function it needs a running event loop. The schedule library however is a blocking method. It has it's own internal logic and blocks other scripts from executing. Meaning you cannot use the schedule module with the discord module.

You need to find a library that has built-in asyncio support or write your own logic for scheduling.

You can get over the problem of blocking by using a different thread as linked in the other Stackoverflow question: How can I run an async function using the schedule library?
which you haven't followed.

In general this is a very hacky approach and I would recommend you to write your own scheduling logic. The schedule module does not work well with the discord module.

Tin Nguyen
  • 5,250
  • 1
  • 12
  • 32
  • The solution in that link is unfortunately not working for me and the OP of that question... I found a lib called "aioschedule" which seems to be async scheduler and should be non-blocking. I'll try to work around. Thanks! – ivey kun Feb 25 '20 at 01:17