-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathModule.py
259 lines (235 loc) · 8.96 KB
/
Module.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import importlib
import types
from ConsoleCommandHandler import ConsoleCommandHandler
import os
import traceback
import string
class Command: # An executable command.
def __init__(self, name, execute, help_data='', privileged=False, owner_only=False,
special_arg_parsing=None, aliases=None, allowed_chars=string.printable, disallowed_chars=""):
self.name = name
self.execute = types.MethodType(execute, self)
self.help_data = help_data or "Command exists, but no help entry found."
self.privileged = privileged
self.owner_only = owner_only
self.aliases = aliases
self.special_arg_parsing = special_arg_parsing
if allowed_chars is not None and " " not in allowed_chars:
allowed_chars += " "
# Space should always be allowed for multiple arguments.
# If you really want to disallow spaces, add one to disallowed_chars.
self.allowed_chars = allowed_chars
self.disallowed_chars = disallowed_chars
class Module: # Contains a list of Commands.
def __init__(self, commands, bot, on_event, on_bot_load, on_bot_stop, module_name):
self.bot = bot
self.commands = commands
self.on_event = on_event
self.on_bot_load = on_bot_load
self.on_bot_stop = on_bot_stop
self.module_name = module_name
self.enabled = True
def command(self, name, args, msg, event):
if not self.enabled:
return False
command = self.find_commands(name)
if command:
if (not command.privileged and not command.owner_only) or isinstance(msg, ConsoleCommandHandler) \
or (command.privileged and (event.user.id in self.bot.privileged_user_ids or event.user.id in self.bot.owner_ids)) \
or (command.owner_only and event.user.id in self.bot.owner_ids):
return command.execute(self.bot, args, msg, event)
else:
return "You don't have the privilege to execute this command."
else:
return False
def get_help(self, name):
match = self.find_commands(name)
if match:
return match.help_data
else:
return ''
def find_commands(self, name):
for command in self.commands:
if command.name == name: return command
elif command.aliases is not None and name in command.aliases: return command
return None
def list_commands(self):
if not self.enabled:
return []
cmd_list = []
for command in self.commands:
cmd_list.append(command)
return cmd_list
class MetaModule: # Contains a list of Modules.
def __init__(self, modules, bot, module_name, path=''):
self.modules = []
self.bot = bot
self.module_name = module_name
self.enabled = True
if path and not path[-1] == '.':
self.path = path + '.'
else:
self.path = ''
for module in modules:
self.modules.append(self.load_module(module))
def command(self, name, args, msg, event):
if not self.enabled:
return False
response = False
for module in self.modules:
response = module.command(name, args, msg, event)
if response is not False:
break
return response
def get_help(self, name):
if not self.enabled:
return False
response = False
for module in self.modules:
if not module.enabled:
continue
response = module.get_help(name)
if response:
break
return response
def load_module(self, file_):
file_ = self.path + file_
try:
module_file = importlib.import_module(file_)
except BaseException as e:
msg = "Error at importing " + file_ + os.linesep
msg += str(e)
msg += os.linesep
msg += traceback.format_exc()
raise ModuleLoadError(msg)
try:
mdls = module_file.modules
try:
module_name = module_file.module_name
except AttributeError:
module_name = file_
if not isinstance(module_name, str):
raise MalformedModuleException("Module: '%s', 'module_name' is not a string." % file_)
if type(mdls) is list:
return MetaModule(mdls, self.bot, module_name, file_[:file_.rfind('.')])
else:
raise MalformedModuleException("Module: '" + file_ + "', 'modules' is not a list.")
except AttributeError:
try:
cmds = module_file.commands
try:
on_event = module_file.on_event
except AttributeError:
on_event = None
try:
on_bot_load = module_file.on_bot_load
except AttributeError:
on_bot_load = None
try:
on_bot_stop = module_file.on_bot_stop
except AttributeError:
on_bot_stop = None
try:
module_name = module_file.module_name
except AttributeError:
module_name = file_
if not isinstance(module_name, str):
raise MalformedModuleException("Module: '%s', 'module_name' is not a string." % file_)
try:
save_subdir = module_file.save_subdir
if isinstance(save_subdir, str):
self.bot.save_subdirs.append(save_subdir)
else:
raise MalformedModuleException("Module: '%s', 'save_subdir' is not a string." % file_)
except AttributeError:
pass
if type(cmds) is list:
return Module(cmds, self.bot, on_event, on_bot_load, on_bot_stop, module_name)
else:
raise MalformedModuleException("Module: '" + file_ + "', 'commands' is not a list.")
except AttributeError:
raise MalformedModuleException("Module: '" + file_ + "' does not contain a variable called either 'modules' or 'commands'.")
def list_commands(self):
if not self.enabled:
return []
cmd_list = []
for module in self.modules:
cmd_list.extend(module.list_commands())
return cmd_list
def get_event_watchers(self):
if not self.enabled:
return []
watchers = []
for m in self.modules:
if isinstance(m, MetaModule):
watchers.extend(m.get_event_watchers())
elif m.on_event is not None:
watchers.append(m.on_event)
return watchers
def get_on_load_methods(self):
if not self.enabled:
return []
on_loads = []
for m in self.modules:
if isinstance(m, MetaModule):
on_loads.extend(m.get_on_load_methods())
elif m.on_bot_load is not None:
on_loads.append(m.on_bot_load)
return on_loads
def get_on_stop_methods(self):
if not self.enabled:
return []
on_stops = []
for m in self.modules:
if isinstance(m, MetaModule):
on_stops.extend(m.get_on_stop_methods())
elif m.on_bot_stop is not None:
on_stops.append(m.on_bot_stop)
return on_stops
def find_module_by_name(self, name):
if not self.enabled:
return None
if self.module_name == name:
return self
for m in self.modules:
if m.module_name == name:
return m
if isinstance(m, MetaModule):
m1 = m.find_module_by_name(name)
if m1 is not None:
return m1
return None
def disable_module(self, name):
if not self.enabled:
return False
if self.module_name == name:
self.enabled = False
return True
for m in self.modules:
if m.module_name == name:
m.enabled = False
return True
if isinstance(m, MetaModule):
m1 = m.disable_module(name)
if m1:
return True
return False
def enable_module(self, name):
if self.module_name == name:
self.enabled = True
return True
if not self.enabled:
return False
for m in self.modules:
if m.module_name == name:
m.enabled = True
return True
if isinstance(m, MetaModule):
m1 = m.enable_module(name)
if m1:
return True
return False
class ModuleLoadError(Exception):
pass
class MalformedModuleException(Exception):
pass