-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbotcommands.py
211 lines (156 loc) · 6.23 KB
/
botcommands.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import os
import imp
import importlib
import sys
from connection import Connection
from ircmessage import IRCMessage
from botstate import BotState
class BotCommands:
_modules = []
def __init__(self):
self._conn = Connection()
self.botstate = BotState()
self.load_modules()
def load_modules(self):
for _, _, files in os.walk("./commands"):
for f in files:
path, ext = os.path.splitext(f)
if path == "__init__":
continue
if ext == ".py":
try:
mod_name = "commands.%s" % path
mod = importlib.import_module(mod_name)
self._modules.append(mod)
except:
print("Cannot load module %s (%s)" % (f, sys.exc_info()))
def execute(self, ircmsg):
# Pre-defined commands
if ircmsg.cmd == "list":
self._list_modules(ircmsg)
return
elif ircmsg.cmd == "quit":
self._quit(ircmsg)
return
elif ircmsg.cmd == "reload":
self._reload_modules(ircmsg)
return
elif ircmsg.cmd == "join" or ircmsg.cmd == "j":
self._join(ircmsg)
return
elif ircmsg.cmd == "channels":
self._list_channels(ircmsg)
return
elif ircmsg.cmd == "help" or ircmsg.cmd == "h":
self._help(ircmsg)
return
# Commands created by external modules
mod_name = "commands.%s" % ircmsg.cmd
cmd_found = False
for i in self._modules:
if i.__name__ == mod_name:
cmd_found = True
if not cmd_found:
# We try to load a module that has the same name as the command
try:
mod = importlib.import_module(mod_name)
line = getattr(sys.modules[mod_name], "run")(self.botstate, ircmsg, self._conn)
print("Loaded module %s\n" % mod_name)
if len(line) > 0:
self.send_command(line)
self._modules.append(mod)
except:
msg = "Module %s does not exist or has errors (%s)" % (ircmsg.cmd, sys.exc_info())
self.send_message(ircmsg.reply_to, msg)
else:
line = getattr(sys.modules[mod_name], "run")(self.botstate, ircmsg, self._conn)
if len(line) > 0:
self.send_command(line)
def send_command(self, line):
self._conn.send(line)
def send_message(self, to, msg):
line = "PRIVMSG %s :%s" % (to, msg)
self.send_command(line)
def join(self, channel):
ircmsg = IRCMessage("")
ircmsg.msg_type = "JOIN"
ircmsg.cmd = "join"
ircmsg.args = channel
self._join(ircmsg)
def _read_line(self):
return self._conn.read_line()
def _list_modules(self, ircmsg):
line = "Loaded modules: "
if len(self._modules) > 0:
loaded_modules = [m.__name__[m.__name__.find(".")+1:] for m in self._modules]
line += ", ".join(loaded_modules)
else:
line = "No modules have been loaded yet"
self.send_message(ircmsg.reply_to, line)
def _quit(self, ircmsg):
if not ircmsg.args:
line = "QUIT :%s" % " ".join(ircmsg.args)
else:
line = "QUIT :Don't kill meeeeeeeeeeeeeeeeee"
self.send_command(line)
self._conn.disconnect()
def _reload_modules(self, ircmsg):
'''
Reloads specified module. If no module is specified, reload them all
If module specified has not been loaded yet, load it
'''
reloaded_modules = []
module_found = False
if len(ircmsg.args) > 0:
for i in ircmsg.args:
mod_name = "commands.%s" % i
try:
for j in self._modules:
if mod_name == j.__name__:
imp.reload(j)
reloaded_modules.append(i)
module_found = True
if module_found == False:
mod = importlib.import_module(mod_name)
reloaded_modules.append(i)
self._modules.append(mod)
except:
msg = "Module %s cannot be reloaded (%s)" % (i, sys.exc_info())
self.send_message(ircmsg.reply_to, msg)
else:
self.load_modules() # Loads any new modules that have been created
for i in self._modules:
try:
imp.reload(i)
name = i.__name__
reloaded_modules.append(name[name.find(".")+1:]) # Remove commands. from the module name
except:
msg = "Module %s cannot be reloaded (%s)" % (i.__name__, sys.exc_info())
self.send_message(ircmsg.reply_to, msg)
if len(reloaded_modules) > 0:
msg = "The following modules were reloaded: %s" % " ".join(reloaded_modules)
else:
msg = "No modules have been reloaded"
self.send_message(ircmsg.reply_to, msg)
def _join(self, ircmsg):
if not ircmsg.args:
return
for i in ircmsg.args:
line = "JOIN %s" % i
self.send_command(line)
def _list_channels(self, ircmsg):
state = self.botstate
channels = []
for i in state._joined_channels:
channels.append(i.channel)
msg = "In channels: %s" % " ".join(channels)
self.send_message(ircmsg.reply_to, msg)
def _help(self, ircmsg):
if not ircmsg.args:
return
cmd = ircmsg.args[0]
if cmd in [m.__name__[m.__name__.find(".")+1:] for m in self._modules]:
mod_name = "commands.%s" % cmd
help_text = getattr(sys.modules[mod_name], "help")()
for l in help_text.split("\n"):
self.send_message(ircmsg.from_nick, l)