-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
61 lines (47 loc) · 1.85 KB
/
bot.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
import logging
import irc.bot
from plugins import load_plugins
LOG = logging.getLogger()
class IRCBot(irc.bot.SingleServerIRCBot):
def __init__(self, server, channels, nickname, realname,
reconnection_interval=60, **connect_params):
super(IRCBot, self).__init__([server], nickname, realname, reconnection_interval, **connect_params)
LOG.info("Initialize Bot instance")
self._default_channels = channels if isinstance(channels, (list, tuple)) else [channels]
self._plugins = []
def on_welcome(self, connect, event):
for ch in self._default_channels:
LOG.info("Connect to channel %s" % ch)
connect.join(ch)
def on_disconnect(self, c, e):
self._on_disconnect(c, e)
def _message_process(self, connect, event):
LOG.debug("Process message: %s" % event.arguments)
if event.source.nick == self.connection.get_nickname():
LOG.debug("It's my message!")
return
for plugin in self._plugins:
plugin.put((connect, event))
def on_privmsg(self, connect, event):
self._message_process(connect, event)
def on_pubmsg(self, connect, event):
self._message_process(connect, event)
def start(self):
LOG.info("Start bot")
LOG.info("Load plugins")
plugins = load_plugins()
LOG.info("Loaded plugins: %s" % self._plugins)
for p in plugins:
LOG.info('Initialize plugin: %s' % p.name)
p = p(self.connection, self.channels)
LOG.info("Start plugin %s" % p)
p.start()
self._plugins.append(p)
LOG.info("Start loop")
super(IRCBot, self).start()
def die(self):
LOG.info("Destroy bot")
for p in self._plugins:
p.stop()
p.join()
super(IRCBot, self).die("Bye!")