我很高兴为我朋友的频道编写一个不和谐的机器人,但我需要一些帮助: 我正在跟踪特定朋友发送的消息并将他拖回去,但我不想在他发送的每条消息上都拖累他。所以我想在机器人发送一条巨魔消息后让机器人休眠一段时间,比如说 20 秒,然后跟踪未来的消息以再次吸引他。
我尝试使用 time.sleep() 但这只会延迟机器人的响应时间,导致它连续发送大量消息。我只是想在收到巨魔消息后停用一段时间。
我隐藏了部分帐户信息,但基本代码如下:
@client.event
async def on_message(message):
await message.channel.send('insert troll massage here')
提前致谢!
答案 0 :(得分:0)
您不想停止机器人的执行。您只是希望机器人忽略特定时间戳内给出的消息。
作为一个草稿,我们可以使用一个全局变量来做到这一点。要延迟一分钟,请考虑
from datetime import datetime, timedelta
LAST_TROLL = datetime.fromtimestamp(0) # A loooooong time ago
TIME_TO_DELAY = timedelta(minutes=1)
@client.event
async def on_message(message):
global LAST_TROLL
now = datetime.now()
if now - LAST_TROLL > TIME_TO_DELAY:
await message.channel.send('insert troll massage here')
LAST_TROLL = now
如果您正在大规模编写机器人,您会希望机器人被很好地封装在一个类中,然后这个变量将成为该类的一个实例变量。