Reputation: 54823
The twisted documentation provides an example of how to create an IRC bot
Here is the code I currently have (derived from the above example):
from twisted.words.protocols import irc
from twisted.internet import protocol
from twisted.internet import reactor
class Bot(irc.IRCClient):
def _get_nickname(self):
return self.factory.nickname
nickname = property(_get_nickname)
def signedOn(self):
self.join(self.factory.channel)
print "Signed on as %s." % (self.nickname,)
def joined(self, channel):
print "Joined %s." % (channel,)
def privmsg(self, user, channel, msg):
print msg
class BotFactory(protocol.ClientFactory):
protocol = Bot
def __init__(self, channel, nickname='test-nick-name'):
self.channel = channel
self.nickname = nickname
def clientConnectionLost(self, connector, reason):
print "Lost connection (%s), reconnecting." % (reason,)
connector.connect()
def clientConnectionFailed(self, connector, reason):
print "Could not connect: %s" % (reason,)
if __name__ == "__main__":
channel = '#test-channel-123'
reactor.connectTCP('irc.freenode.net', 6667, BotFactory(channel))
reactor.run()
Now I want to add the functionality of sending a message say every 5 seconds to the channel. How do I go about doing that? How do I get the handle to the Bot.msg method from outside?
Upvotes: 1
Views: 1079
Reputation: 101142
sending a message say every 5 seconds to the channel
Have a look at LoopingCall
, which you can use to call a method every n seconds.
from twisted.internet import task
task.LoopingCall(yourSendingMethodHere).start(5.0)
How do I get the handle to the Bot.msg method from outside?
It's up to you. You create the instance of the BotFactory
, and every Bot
has a reference to its factory.
Upvotes: 2