仅在一次discord.py中循环发送消息

时间:2018-11-11 11:31:45

标签: python bots discord.py

正如标题所示,我试图在while循环内发送一条消息(仅一次)。到目前为止,这是代码:

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

代码现在在11:22时多次发送消息。我尝试将if语句放在循环外,但它根本没有发送任何消息。我还尝试将asyncio.sleep(60)放在if语句中,但这也不起作用。不知道如何解决这个问题。请帮忙吗?

1 个答案:

答案 0 :(得分:0)

经过研究后,我找到了一种方法。我创建了一个名为“ seconds”的变量,该变量存储(显然)秒。这是我的新代码:

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

这成功了。语句也只发送一次