开发者

the time() function (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()
      开发者_StackOverflow社区     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


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:

while time.time() < last_google + 5:
    sleep(1) # You don't want to keep using the cpu, so let your process sleep
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜