forked from jessamynsmith/talkbackbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtalkbackbot.py
73 lines (55 loc) · 2.29 KB
/
talkbackbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import logging
from twisted.words.protocols import irc
from twisted.internet import reactor, protocol
from quotation_selector import QuotationSelector
class TalkBackBot(irc.IRCClient):
def connectionMade(self):
irc.IRCClient.connectionMade(self)
logging.info("connectionMade")
def connectionLost(self, reason):
irc.IRCClient.connectionLost(self, reason)
logging.info("connectionLost")
# callbacks for events
def signedOn(self):
"""Called when bot has successfully signed on to server."""
logging.info("Signed on")
self.join(self.factory.channel)
def joined(self, channel):
"""This will get called when the bot joins the channel."""
logging.info("[%s has joined %s]"
% (self.nickname, self.factory.channel))
def privmsg(self, user, channel, msg):
"""This will get called when the bot receives a message."""
trigger_found = False
send_to = channel
if self.factory.settings.NICKNAME.startswith(channel) or \
channel.startswith(self.factory.settings.NICKNAME):
trigger_found = True
send_to = user.split('!')[0]
else:
for trigger in self.factory.settings.TRIGGERS:
if msg.lower().find(trigger) >= 0:
trigger_found = True
break
if trigger_found:
quote = self.factory.quotation.select()
self.msg(send_to, quote)
logging.info("sent message to %s:\n\t%s" % (send_to, quote))
class TalkBackBotFactory(protocol.ClientFactory):
def __init__(self, settings):
self.settings = settings
self.channel = self.settings.CHANNEL
self.quotation = QuotationSelector(self.settings.QUOTES_FILE)
def buildProtocol(self, addr):
bot = TalkBackBot()
bot.factory = self
bot.password = self.settings.PASSWORD
bot.nickname = self.settings.NICKNAME
bot.realname = self.settings.REALNAME
return bot
def clientConnectionLost(self, connector, reason):
logging.info("connection lost, reconnecting")
connector.connect()
def clientConnectionFailed(self, connector, reason):
logging.info("connection failed: %s" % (reason))
reactor.stop()