简体   繁体   中英

sending a message in a loop only once discord.py

as the title suggests i'm trying to send a message (only once) inside a while loop. This is the code so far:

while hour >= 11 and hour < 12:
   msg = await bot.wait_for_message(timeout=1, author=message.author)
   if msg:
      pass
   hour = int(time.strftime("%H"))
   minute = int(time.strftime("%M"))
   if hour == 11 and minute == 22:
      await bot.send_message(message.channel, "<@258621320898543616> you have 5 min of break left. Finish up whatever your doing quickly tatsu san!")

the code now send the message multiple times when its 11:22. I tried putting the if statement outside the loop, but it didn't send a message at all. I also tried putting asyncio.sleep(60) in the if statement but that didn't work either. Not sure how to get around this. Help please?

after researching a bit I found out a way to do this. I created a variable called "seconds" which stored (obviously) seconds. This is my new code:

while hour >= 11 and hour < 12:
   msg = await bot.wait_for_message(timeout=1, author=message.author)
   if msg:
      pass
   hour = int(time.strftime("%H"))
   minute = int(time.strftime("%M"))
   seconds = int(time.strftime("%S"))
   if hour == 11 and minute == 22 and seconds == 0:
      await bot.send_message(message.channel, "<@258621320898543616> you have 5 min of break left. Finish up whatever your doing quickly tatsu san!")

this did the trick. The statement only gets sent once as well

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM