62 lines
1.8 KiB
Python
62 lines
1.8 KiB
Python
import time
|
|
|
|
from twisted.words.protocols import irc
|
|
from twisted.internet import reactor, protocol
|
|
|
|
from .bot import Bot
|
|
from .settings import logger
|
|
|
|
|
|
class IRCBot(irc.IRCClient):
|
|
"""An IRC bot"""
|
|
|
|
def connectionMade(self):
|
|
super(IRCBot, self).connectionMade()
|
|
logger.info("{name} is connected".format(name=self.nickname))
|
|
self.join(self.factory.channel)
|
|
|
|
def connectionLost(self, reason):
|
|
super(IRCBot, self).connectionLost(reason)
|
|
logger.info(
|
|
"{name} is disconnected : {reason}".format(
|
|
name=self.nickname, reason=reason
|
|
)
|
|
)
|
|
|
|
def signedOn(self):
|
|
self.join(self.factory.channel)
|
|
|
|
def joined(self, channel):
|
|
logger.info(self.nickname + " joined " + self.factory.channel)
|
|
if self.factory.bot.on_join is not None:
|
|
self.say(self.factory.channel, self.factory.bot.on_join)
|
|
|
|
def privmsg(self, user, channel, msg):
|
|
results = self.factory.bot.get_reaction(user, channel, msg)
|
|
logger.debug(self.nickname + " heard " + msg)
|
|
if results:
|
|
logger.info(self.nickname + " reacting to " + msg)
|
|
for r in results:
|
|
self.say(self.factory.channel, r)
|
|
|
|
|
|
class IRCBotFactory(protocol.ClientFactory):
|
|
def __init__(self, bot):
|
|
self.bot = bot
|
|
self.channel = bot.channel
|
|
|
|
def clientConnectionLost(self, connector, reason):
|
|
logger.info("Client connexion lost")
|
|
time.sleep(2)
|
|
connector.connect()
|
|
|
|
def clientConnectionFailed(self, connector, reason):
|
|
logger.info("Connection failed : " + str(reason))
|
|
reactor.stop()
|
|
|
|
def buildProtocol(self, addr):
|
|
p = IRCBot()
|
|
p.factory = self
|
|
p.nickname = self.bot.nickname
|
|
return p
|