-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbotmodule.py
40 lines (31 loc) · 1.02 KB
/
botmodule.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
class CmdException(Exception):
pass
class BotModule:
def __init__(self, bot):
self.bot = bot
print('Loaded ' + self.__class__.__name__)
def unload(self):
print('Unloaded ' + self.__class__.__name__)
def handle_connect(self, conn):
pass
def handle_privmsg(self, conn, sender, target, msg, respond):
if len(msg) > 0 and msg[0] == '.':
split = msg.split()
cmd = split[0][1:]
params = split[1:]
func = getattr(self, 'handle_cmd_' + cmd.lower(), None)
if func:
try:
func(conn=conn, sender=sender, target=target, params=params, respond=respond)
except CmdException as e:
conn.msg_privmsg(respond, str(e))
def handle_msg_privmsg(self, conn, msg):
if msg['params'][0].lower() == self.bot.nick.lower():
respond = msg['prefix']['nick']
else:
respond = msg['params'][0]
self.handle_privmsg(conn, msg['prefix'], msg['params'][0], msg['params'][1], respond)
def handle_msg(self, conn, msg):
func = getattr(self, 'handle_msg_' + msg['command'].lower(), None)
if func:
func(conn, msg)