Perform tasks periodically with python irc library

2019-08-25 03:20发布

问题:

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)