你有一个条件语句,如果小时和分钟达到特定时间,那么它就会开始 ping 角色。第一个 if 语句完美运行,但是第二个和第三个 if 语句,它只是继续一次发送多条消息
async def on_ready(self):
print('reminder functions are working')
while True:
cst = datetime.datetime.now(tz=pytz.timezone('US/Central')).time()
if cst.hour == 8 and cst.minute == 59:
channel = self.client.get_channel(760182149839716423)
await asyncio.sleep(75)
await channel.send("<@&811801850771800134>")
await channel.send("<@&811808231218610266>")
await channel.send("<@&811802000463101963>")
elif cst.hour == 12 and cst.minute == 59:
channel = self.client.get_channel(760182149839716423)
await asyncio.sleep(75)
await channel.send("<@&811801850771800134>")
await channel.send("<@&811808231218610266>")
await channel.send("<@&811802000463101963>")
if cst.hour == 17 and cst.minute == 59:
channel = self.client.get_channel(760182149839716423)
await asyncio.sleep(75)
await channel.send("<@&811801850771800134>")
await channel.send("<@&811808231218610266>")
await channel.send("<@&811802000463101963>")
答案 0 :(得分:0)
您检查时间是否为精确的小时和精确的分钟。这意味着该条件在一分钟内都为真,因此您的机器人将发送垃圾邮件,直到下一分钟到来。
您应该添加所有 if 条件以检查秒是否为 0。请注意,如果您的循环周期超过 1 秒,您可能会跳过时钟到达所需小时的确切时间第二。你应该重新考虑如何实施它
这就是你的代码的样子
async def on_ready(self):
print('reminder functions are working')
while True:
cst = datetime.datetime.now(tz=pytz.timezone('US/Central')).time()
if cst.hour == 8 and cst.minute == 59 and cst.second == 0:
channel = self.client.get_channel(760182149839716423)
await asyncio.sleep(75)
await channel.send("<@&811801850771800134>")
await channel.send("<@&811808231218610266>")
await channel.send("<@&811802000463101963>")
elif cst.hour == 12 and cst.minute == 59 and cst.second == 0:
channel = self.client.get_channel(760182149839716423)
await asyncio.sleep(75)
await channel.send("<@&811801850771800134>")
await channel.send("<@&811808231218610266>")
await channel.send("<@&811802000463101963>")
if cst.hour == 17 and cst.minute == 59 and cst.second == 0:
channel = self.client.get_channel(760182149839716423)
await asyncio.sleep(75)
await channel.send("<@&811801850771800134>")
await channel.send("<@&811808231218610266>")
await channel.send("<@&811802000463101963>")
此外,您应该小心,在发送的每条消息之间稍等片刻,以免您的机器人被视为垃圾邮件。或者您也可以发送一封包含所有提及内容的消息