time() 函数(python)
if data.find('!google') != -1:
nick = data.split('!')[ 0 ].replace(':','')
if last_google + 5 > time.time():
sck.send('PRIVMSG ' + chan + " :" + ' Wait a few seconds' + "\r\n")
else:
last_google = time.time()
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”之前等待几秒钟,以防止用户淹没频道或机器人。显然这个脚本不起作用,我错误地实现了时间函数,或者我'我缺少一些东西
if data.find('!google') != -1:
nick = data.split('!')[ 0 ].replace(':','')
if last_google + 5 > time.time():
sck.send('PRIVMSG ' + chan + " :" + ' Wait a few seconds' + "\r\n")
else:
last_google = time.time()
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')
Ok I'm trying to make the script wait a few seconds before another user can "!google" to prevent users from flooding the channel or the bot.. Obviously this script doesnt work, I'm implementing the time function wrong or I'm missing something
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这一切都在循环中吗?
一个问题是您在请求开始时将 last_google 设置为 time.time() 。如果请求很慢,那么当您再次到达 if 语句时,部分时间可能已经过去了。
典型的“等待”块可能看起来像这样:
Is this all in a loop?
One issue is that you set last_google to time.time() at the beginning of your request. If the request is slow, some of that time may already be gone by the time you get to the if statement again.
A typical 'waiting' block might look something like this: