gpt4 book ai didi

python - if 语句中的冷却时间

转载 作者:太空宇宙 更新时间:2023-11-03 21:19:38 24 4
gpt4 key购买 nike

我正在用 Python 编写一个非常简单的 Twitch 聊天机器人,我的所有“命令”都在 if 语句中运行(基本上,如果聊天机器人在聊天中看到某些内容,它就会激活)。

但是,我想为每个命令(或 if 语句)添加大约 3 秒的冷却时间或实际的时间量,以便我可以根据情况进行自定义。为此,我尝试了这个(伪代码)

if command is seen in chat
newtimestamp = create timestamp at now
if (newtimestamp - previoustimestamp) > 30
execute the command
make (previoustimestamp)
else
return
但这不起作用,因为显然它在第一次运行时没有检测到(先前的时间戳),因为它尚未声明,但是当您声明它时,它会在每次运行命令时声明它。

那么有没有一种方法可以在 if 语句的开头仅声明一次,然后每隔一段时间就忽略它?或者还有其他想法吗?我仍然是一个相当新手的程序员,所以我很抱歉。

这是一个我想要冷却时间的示例代码,非常简单

if "!redb match" in message:    
x = str(randint(1, 100))
realmessage = message.replace('!redb ship ', '')
array = realmessage.split(' ')
person1 = array[0]
person2 = array[1]
if ((9 - 2) > 5):
sendMessage(s, person1 + " and " + person2 + "have a " + x + "% love compatibility <3")
else:
break

最佳答案

我会使用字典来存储时间戳,像 previous_timestamp = command_timestamps.get('!redb match', 0) 一样访问它。如果它在字典中,这将为您提供存储的时间戳,如果您使用 time.time() 作为时间戳,则为 0(1970 年 1 月 1 日),这希望是过去足够远的时间它不会弄乱你的任何冷却时间)。

关于python - if 语句中的冷却时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54408350/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com