discord bot sending multiple messages

Question:

my scheduled message bot code is working but I have no idea how to prevent from sending multiple messages at the same time

@Bot.event
async def on_ready():
    print("Bot is ready")
    while True:
        time = datetime.datetime.today()
        if time.hour == 2:
         if time.minute == 39:
           await Bot.get_channel(<channel id>).send(f"Good Morning")
Asked By: s4vis

||

Answers:

You can use a boolean variable that indicates whether you have already sent the message at 2:39 am.

@Bot.event
async def on_ready():
    print("Bot is ready")
    sent = False
    while True:
        time = datetime.datetime.today()
        if time.hour == 2:
         if time.minute == 39:
           if not sent:
             sent = True
             await Bot.get_channel(<channel id>).send(f"Good Morning")
         else:
             sent = False

It depends on a thousand factors how you want to implement the control. You can also sleep for 1 minute using sleep(60), or exit the loop once the event is triggered (break)

Answered By: Tommaso Ventafridda
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.