【发布时间】:2013-06-02 19:57:33
【问题描述】:
我有一个 IRC 机器人,用于自动化操作。
这是它的一个sn-p:
def analyseIRCText(connection, event):
global adminList, userList, commandPat, flood
userName = extractUserName(event.source())
userCommand = event.arguments()[0]
escapedChannel = cleanUserCommand(config.channel).replace('\\.', '\\\\.')
escapedUserCommand = cleanUserCommand(event.arguments()[0])
#print userName, userCommand, escapedChannel, escapedUserCommand
if flood.has_key(userName):
flood[userName] += 1
else:
flood[userName] = 1
... (if flood[userName] > certain number do...)
所以这个想法是洪水的东西是一本字典,其中列出了最近在机器人命令中输入的用户列表......保留了一些时间,以及他们在其中说了多少次。那个时间段。
这就是我遇到麻烦的地方。必须有一些东西可以重置这个字典,以便用户可以每隔一段时间说一些东西,不是吗?我认为像这样的小事就可以解决问题。
def floodClear():
global flood
while 1:
flood = {} # Clear the list
time.sleep(4)
但是最好的方法是什么? 在程序的最后,我有一行叫做:
thread.start_new_thread(floodClear,())
这样这个东西就不会被调用,就会陷入一个无限循环,从而停止其他一切。这是一个好的解决方案还是我可以做的更好?
【问题讨论】:
-
您真的应该将
analyseIRCText重构为一个类,并将adminList、userList、commandPat和flood作为实例属性。全局变量通常是bad idea。