if data.find('!google') != -1:
nick = data.split('!')[ 0 ].replace(':','')
try:
gs = GoogleSearch(args)
gs.results_per_page = 1
results = gs.get_results()
for res in results:
sck.send('PRIVMSG ' + chan + " " + res.title.encode("utf8") + '\r\n')
sck.send('PRIVMSG ' + chan + " " + res.url.encode("utf8") + '\r\n')
print
except SearchError, e:
sck.send('PRIVMSG ' + chan + " " + "Search failed: %s" % e + " " + '\r\n')我试着让脚本在其他用户可以"!google“之前等待几秒钟,以防止用户泛滥频道或机器人,不确定我是否应该使用sleep()函数,因为这可能会停止整个脚本,我只想让它等待几秒钟,然后任何人都可以再次使用"!google”。
发布于 2011-04-12 08:14:02
time module中有一个sleep function。
但是,要使脚本不阻塞,可以调用time module中的time function并将其存储。如果当前时间小于这个值,再加上5秒,就不要让他们使用它。
例如:
last_google = 0
# somewhere later in the script where last_google is still in scope...
if data.find('!google') != -1:
if last_google + 5 < time.time():
# throttled
return
last_google = time.time()
# do something herehttps://stackoverflow.com/questions/5628976
复制相似问题