【发布时间】:2019-06-21 19:56:33
【问题描述】:
我正在用 Python 编写一个非常简单的 Twitch 聊天机器人,并且我的所有“命令”都在 if 语句中运行(基本上,如果聊天机器人在聊天中看到它激活的内容)。
但是,我想为 per 命令(或 if 语句)添加大约 3 秒或真正的时间量的冷却时间,以便我可以根据需要对其进行自定义。为此,我尝试了这个(伪代码)
if command is seen in chat
newtimestamp = create timestamp at now
if (newtimestamp - previoustimestamp) > 30
execute the command
make (previoustimestamp)
else
return
但这不起作用,因为显然它在第一次运行时没有检测到 (previoustimestamp),因为它尚未声明,但是当你声明它时,它会在每次运行命令时声明它。
那么有没有办法在 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
【问题讨论】:
标签: python if-statement bots chatbot twitch