klafirc/klafirc/irc.py

62 lines
1.8 KiB
Python
Raw Normal View History

2018-08-28 19:47:28 +00:00
import time
2018-08-02 12:40:42 +00:00
from twisted.words.protocols import irc
from twisted.internet import reactor, protocol
from .bot import Bot
from .settings import logger
2019-04-21 21:14:28 +00:00
2018-08-02 12:40:42 +00:00
class IRCBot(irc.IRCClient):
"""An IRC bot"""
def connectionMade(self):
super(IRCBot, self).connectionMade()
2019-04-21 21:14:28 +00:00
logger.info("{name} is connected".format(name=self.nickname))
2018-08-02 12:40:42 +00:00
self.join(self.factory.channel)
def connectionLost(self, reason):
super(IRCBot, self).connectionLost(reason)
2019-04-21 21:14:28 +00:00
logger.info(
"{name} is disconnected : {reason}".format(
name=self.nickname, reason=reason
)
)
2018-08-02 12:40:42 +00:00
def signedOn(self):
self.join(self.factory.channel)
def joined(self, channel):
2019-04-21 21:14:28 +00:00
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)
2018-08-02 12:40:42 +00:00
def privmsg(self, user, channel, msg):
results = self.factory.bot.get_reaction(user, channel, msg)
2019-04-21 21:14:28 +00:00
logger.debug(self.nickname + " heard " + msg)
2018-08-02 12:40:42 +00:00
if results:
2019-04-21 21:14:28 +00:00
logger.info(self.nickname + " reacting to " + msg)
2018-08-02 12:40:42 +00:00
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")
2018-08-28 19:47:28 +00:00
time.sleep(2)
connector.connect()
2018-08-02 12:40:42 +00:00
def clientConnectionFailed(self, connector, reason):
2019-04-21 21:14:28 +00:00
logger.info("Connection failed : " + str(reason))
2018-08-02 12:40:42 +00:00
reactor.stop()
def buildProtocol(self, addr):
p = IRCBot()
p.factory = self
p.nickname = self.bot.nickname
return p