Perform tasks periodically with python irc library

て烟熏妆下的殇ゞ 提交于 2019-12-11 20:12:14

问题


Is there a way to automate periodic tasks: like sending messages to other users or channels, etc. using python irclib or twisted-based youmomdotcom python irc bot.

Example of irclib based irc client:

from irc import client
class OurIRCClient(client.SimpleIRCClient):
    def __init__(self):
        client.SimpleIRCClient.__init__(self)

import sys
client = OurIRCClient()

try:
    client.connect("irc.freenode.net", 6667, myUserId)
    print "connected to irc.freenode.net"
except:
    sys.exit(-1)
    "error: coouldn't connect to irc server"
client.connection.join("#django-hotclub")
client.start()

回答1:


If you use the Twisted-based solution, you can simply use a LoopingCall to schedule whatever periodic method you want to call.

(If you use irclib it's much harder to do this in a way that works properly in all situations, so I will not include that in my answer here.)




回答2:


As Glyph pointed out, i've overridden the instance method connectionMade of the irc client class and made it use LoopingCall.

 def connectionMade(self):
        irc.IRCClient.connectionMade(self)
        task.LoopingCall(lambda : (self.msg(counterpartID, "hi there"))).start(5.0)


来源:https://stackoverflow.com/questions/19259857/perform-tasks-periodically-with-python-irc-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!