Home > Enterprise >  discord bot sending multiple messages
discord bot sending multiple messages

Time:11-02

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")

CodePudding user response:

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)

  • Related