Search code examples
pythondiscord.pyschedule

How to run scheduler so that other function works in discord.py


I thought the schedule wouldn't interfere with the bot commands but it does. After i run a schedule let's say every one minute, it blocks other functions in my bot. I'm looking for a solution to run 1 simple task via scheduler ( i'm using this schedule module ) and keeping all the primary bot functionality - any commands or events.

Example:

@client.event
async def on_message(message):
    if message.author.id == xxxxx:
        print("im working")

def test():
    print("hello")

job = schedule.every().second.do(test)

while 1:
    schedule.run_pending()
 

I would like to run the test function and be able to detect messages via on_message function at the same time.

Thank you for any help


Solution

  • Discord.py has a feature for that, see the full documentation here. Here a short example:

    from discord.ext import tasks
    
    @tasks.loop(seconds=5)
    async def foo():
       print('This function runs every 5 Seconds')