'"on_message" of Discord.py can't work after I implemented "schedule" into it

I'm not familiar with Python so I'm afraid that this could be a silly question for all of you but I have already tried my best to solve it.

I worked on a self-project of making a bot to do some silly task just for fun.

First, I have done a function to let it receive a message with "on_message" event, which works well. here's the code

import discord
import os

discordClient = discord.Client()

@discordClient.event  #event register
async def on_ready():  
    print('Logged in as {0.user}'.format(discordClient))
    await routineAnnouncer.createDisocrdRoutine(discordClient)

@discordClient.event
async def on_message(message):
    checked = False

    if message.author == discordClient.user:
      checked = True
      return
    else:
      print("Get Message {0.content} from {0.author.name} ({0.author.id}) @{0.channel.name} ({0.channel.id})".
      format(message))

#-------------------------- RUN ----------------------

print('Registering EVENTS COMPLETE')
discordClient.run(os.environ['discordToken'])

I code it on Repl.it and "os.environ" is its way to retrieve .env.

here are the code in "routineAnnouncer" that is used in the "on_ready" event

import os  #ใช้ดึง .env (secret)
import schedule
import time

def scheduleAnnouncer(discordClient,announceType,announceDetail):
  print("Announcing {} {}".format(announceType,announceDetail))
  lzChannel = discordClient.get_channel(int(os.environ['lz_token_test']))
  discordClient.loop.create_task(lzChannel.send("Announcing {} {}".format(announceType,announceDetail)))

async def createDisocrdRoutine(discordClient):
  print("Registering CRONJOBS...")
  scheduleAnnouncer(discordClient,"headerTest","descTest")
  schedule.every(5).seconds.do(scheduleAnnouncer,discordClient,"header1","desc1")
  schedule.every(10).seconds.do(scheduleAnnouncer,discordClient,"header2","desc2")
  # while True:
    #   schedule.run_pending()
    #   time.sleep(5)

So, it supposed to do createDisocrdRoutine() after the connection is ready and set the scheduling text.

As you can see the last section of the code is commented. It is supposed to be a loop that triggers the scheduled text to be sent via discord to a designated channel.id.

To this point. The code works fine. the "on_message(message)" section able to print out whatever is sent to a channel. The function "scheduleAnnouncer" also works fine. It can send message to the channel. Discord Screen Repl.it Screen

After a loop "while: True" is uncommented in order to let the schedule work. The loop works fine. It prints out the text as shown in the loop. but discord can't detect any text that is sent to the same channel before. Even the function "scheduleAnnouncer" that supposed to send a message is broken. It feels like anything involved with discord is broken as soon as the "while: True" is uncommented. Discord Screen Repl.it Screen

I tried to separate the scheduling into another thread but it didn't work. I tried to use other cronjob managements like cronjob, sched or something else. Most of them let me face other problem.

  • I need to send argument into the task (discordClient, announceType, announceDetail).
  • I need to use the specific date/time to send. Not the interval like every 5 seconds. (The text that needs to be sent differ from time and day. Monday's text is not that same as Friday's text) From both criteria. The "schedule" fits well and suppose to works fine.

Thank you in advance. I don't know what to do or check next. Every time I try to solve it. I always loop back to "schedule" and try to works with it over and over.

I hope that these pieces of information are enough. Thank you so much for your time.



Solution 1:[1]

time.sleep() is not async so it will freeze your program. Use await asyncio.sleep(). This is because it is pausing the whole program, not just that function.

The reason you’d want to use wait() here is because wait() is non-blocking, whereas time.sleep() is blocking. What this means is that when you use time.sleep(), you’ll block the main thread from continuing to run while it waits for the sleep() call to end. wait() solves this problem.

Quote From RealPython

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1