具有扭曲的重复功能调用

时间:2012-11-30 02:19:17

标签: python twisted

我有一个基本的IRC机器人看起来像这样(见下文),我想做的是使用类似_5_mins函数的东西,每隔5分钟调用LoopingCall < / p>

import sys
import re
from twisted.internet import reactor, task, defer, protocol
from twisted.python import log
from twisted.words.protocols import irc
from twisted.application import internet, service
import time

HOST, PORT = 'irc.freenode.net', 6667

class IrcProtocol(irc.IRCClient):
    nickname = 'BOTSNAME'
    password = 'NICKPASSWORD'
    timeout = 600.0

    def signedOn(self):
        pMess = "IDENTIFY %s" % self.password
        self.msg("NickServ",pMess)
        time.sleep(10)
        for channel in self.factory.channels:
            self.join(channel)

    def _5_mins(self):
        self.msg(self.factory.channels[0],"5 minutes have elapsed")

class IrcFactory(protocol.ReconnectingClientFactory):
    channels = ['#BOTCHANNEL']
    protocol = IrcProtocol

if __name__ == '__main__':
    reactor.connectTCP(HOST, PORT, IrcFactory())
    log.startLogging(sys.stdout)
    reactor.run()

elif __name__ == '__builtin__':
    application = service.Application('IrcBot')
    ircService = internet.TCPClient(HOST, PORT, IrcFactory())
    ircService.setServiceParent(application)

如何使用signedOn功能更改task.LoopingCall功能,还是有更好的方法?

编辑:我真的很接近解决方案,以下就是我的用途

def signedOn(self):
    pMess = "IDENTIFY %s" % self.password
    self.msg("NickServ",pMess)
    time.sleep(10)
    for channel in self.factory.channels:
        self.join(channel)
    lc = task.LoopingCall(self._5_mins)
    lc.start(self.timeout)

1 个答案:

答案 0 :(得分:2)

def signedOn(self):
    pMess = "IDENTIFY %s" % self.password
    self.msg("NickServ",pMess)
    time.sleep(10)
    for channel in self.factory.channels:
        self.join(channel)
    lc = task.LoopingCall(self._5_mins)
    lc.start(self.timeout)