-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathnano.py
519 lines (373 loc) · 14.9 KB
/
nano.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# coding=utf-8
import asyncio
import importlib
import logging
import os
import sys
import time
import discord
import traceback
from core.serverhandler import ServerHandler
from core.stats import NanoStats
from core.translations import TranslationManager
from core.utils import log_to_file
from core.confparser import get_settings_parser, PLUGINS_DIR
__title__ = "Nano"
__author__ = 'DefaltSimon'
__version__ = '3.8.3'
# EVENTS
ON_MESSAGE = "on_message"
ON_REACTION_ADD = "on_reaction_add"
ON_READY = "on_ready"
ON_MESSAGE_DELETE = "on_message_delete"
ON_MESSAGE_EDIT = "on_message_edit"
ON_CHANNEL_DELETE = "on_channel_delete"
ON_CHANNEL_CREATE = "on_channel_create"
ON_CHANNEL_UPDATE = "on_channel_update"
ON_MEMBER_JOIN = "on_member_join"
ON_MEMBER_REMOVE = "on_member_remove"
ON_MEMBER_UPDATE = "on_member_update"
ON_MEMBER_BAN = "on_member_ban"
ON_MEMBER_UNBAN = "on_member_unban"
ON_GUILD_JOIN = "on_guild_join"
ON_GUILD_REMOVE = "on_guild_remove"
ON_ERROR = "on_error"
ON_SHUTDOWN = "on_shutdown"
ON_PLUGINS_LOADED = "on_plugins_loaded"
EVENTS = ["on_message", "on_guild_join", "on_channel_create", "on_channel_delete",
"on_channel_update", "on_message_edit", "on_message_delete", "on_ready",
"on_member_join", "on_member_remove", "on_member_update", "on_member_ban",
"on_member_unban", "on_guild_remove", "on_error", "on_shutdown",
"on_plugins_loaded", "on_reaction_add"]
# Ensure there are no duplicates
assert len(set(EVENTS)) == len(EVENTS)
# Other constants
IS_RESUME = False
# LOGGING
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
# Set logging levels for external modules
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("discord").setLevel(logging.INFO)
logging.getLogger("websockets.protocol").setLevel(logging.INFO)
# Disable redis debug messages
logging.getLogger("core.stats").setLevel(logging.INFO)
logging.getLogger("plugins.statistics").setLevel(logging.INFO)
# Config parser setup
parser = get_settings_parser()
# Loop, discord.py and Nano core modules initialization
loop = asyncio.get_event_loop()
# NOW USES AUTOSHARDING
custom_intents = discord.Intents.default()
custom_intents.members = True
client = discord.AutoShardedClient(
loop=loop,
intents=custom_intents,
chunk_guilds_at_startup=True,
guild_ready_timeout=2,
guild_subscriptions=True
)
log.info("Initializing ServerHandler and NanoStats...")
# Setup the server data and stats
handler = ServerHandler.get_handler(loop)
stats = NanoStats(loop, *ServerHandler.get_redis_credentials())
trans = TranslationManager()
class PluginObject:
def __init__(self, lib, instance):
self.plugin = lib
self.handler = getattr(lib, "NanoPlugin")
self.instance = instance
self.events = self.handler.events
# Singleton metaclass
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class Nano(metaclass=Singleton):
def __init__(self):
# Variables
self.boot_time = time.time()
self.version = __version__
self.owner_id = parser.getint("Settings", "ownerid")
self.dev_server = parser.getint("Dev", "server")
# Plugin-related
self.plugin_names = []
self.plugins = {}
self.plugin_events = {a: [] for a in EVENTS}
self.event_types = set(self.plugin_events.keys())
# Updates the plugin list
self.update_plugins()
def update_plugins(self):
started = time.monotonic()
plugin_names = [pl[:-3] for pl in os.listdir(PLUGINS_DIR)
if os.path.isfile(os.path.join(PLUGINS_DIR, pl))
and pl.endswith(".py")]
self._update_plugins(plugin_names)
log.info("Plugins loaded in {}s".format(round(time.monotonic() - started, 3)))
def _update_plugins(self, names: list):
log.info("Loading plugins...")
loaded = []
failed = []
disabled = []
PLUGINS_NAMESPACE = PLUGINS_DIR.replace("/", "").replace("\\", "")
for plug_name in list(names):
# Import the plugin
try:
plugin = importlib.import_module("{}.{}".format(PLUGINS_NAMESPACE, plug_name))
except ImportError:
log.warning("Couldn't import {}".format(plug_name))
log.critical(traceback.format_exc())
failed.append(plug_name)
continue
# Plugin loaded, check validity
# Plugin must have a class NanoPlugin, see examples in plugins/
if not hasattr(plugin, "NanoPlugin"):
log.warning("Plugin {} does not have the required NanoPlugin class".format(plug_name))
failed.append(plug_name)
del plugin
continue
info = getattr(plugin, "NanoPlugin")
handler_cls = info.handler
# Make an instance
try:
inst = handler_cls(client=client,
loop=loop,
handler=handler,
nano=self,
stats=stats,
trans=trans)
# A plugin can raise RuntimeError to indicate it doesn't want to be loaded
except RuntimeError:
disabled.append(plug_name)
del plugin
continue
# Other exceptions make it fail
except Exception:
log.warning("Failed to instantiate {}".format(plug_name))
log.critical(traceback.format_exc())
del plugin
continue
self.plugins[plug_name] = PluginObject(plugin, inst)
loaded.append(plug_name)
self.plugin_names = loaded
log.debug("Plugins: {}".format(self.plugin_names))
# Warn if any plugins failed to load
if disabled:
log.warning("Disabled plugins: {}".format(", ".join(disabled)))
if failed:
log.warning("Failed plugins: {}".format(", ".join(failed)))
self._parse_priorities()
asyncio.ensure_future(self.dispatch_event(ON_PLUGINS_LOADED))
async def reload_plugin(self, name: str):
log.info("Reloading plugin: {}".format(name))
if name.endswith(".py"):
name = name[:-3]
# Verify that the plugin is actually already loaded
if name not in self.plugins.keys():
raise NotImplementedError
plug = self.get_plugin(name)
assert isinstance(plug, PluginObject)
# Gracefully reload if the plugin has ON_SHUTDOWN event
if ON_SHUTDOWN in plug.events.keys():
await getattr(plug.instance, ON_SHUTDOWN)()
# Reload the plugin
try:
plugin = importlib.reload(plug.plugin)
except ImportError:
log.warning("Couldn't reload {}".format(name))
log.critical(traceback.format_exc())
raise RuntimeError
# Plugin loaded, check validity
# Plugin must have a class NanoPlugin, see examples in plugins/
if not hasattr(plugin, "NanoPlugin"):
log.warning("Plugin {} does not have the required NanoPlugin class".format(name))
del plugin
raise RuntimeError
info = getattr(plugin, "NanoPlugin")
events = info.events
handler_cls = info.handler
# Make an instance
try:
inst = handler_cls(client=client,
loop=loop,
handler=handler,
nano=self,
stats=stats,
trans=trans)
# A plugin can raise RuntimeError to indicate it doesn't want to be loaded
except RuntimeError:
del plugin
raise RuntimeError
# Other exceptions make it fail
except Exception:
log.warning("Failed to reload and instantiate {}".format(name))
log.critical(traceback.format_exc())
del plugin
raise RuntimeError
self.plugins[name] = PluginObject(plugin, inst)
self._parse_priorities()
# Call ON_PLUGINS_LOADED if the plugin requires it
if ON_PLUGINS_LOADED in events.keys():
await getattr(inst, ON_PLUGINS_LOADED)()
log.info("Plugin reloaded: {}".format(name))
return True
def _parse_priorities(self):
log.info("Parsing priorities...")
temp = {}
for p in self.plugins.values():
assert isinstance(p, PluginObject)
for ev_name, priority in p.events.items():
if not temp.get(ev_name):
temp[ev_name] = []
temp[ev_name].append({"callback": getattr(p.instance, ev_name), "importance": priority})
# Order callbacks
for event, unordered in temp.items():
ordered = sorted(unordered, key=lambda a: a["importance"])
ordered = [i["callback"] for i in ordered]
self.plugin_events[event] = ordered
def get_plugin(self, name: str) -> dict:
if name.endswith(".py"):
name = name[:-3]
return self.plugins[name]
async def dispatch_event(self, event_type, *args, **kwargs):
"""
Dispatches any discord event (for example: on_message)
"""
if event_type not in self.event_types:
log.warning("No such event: {}".format(event_type))
return
# If there is no registered event, quit
if not self.plugin_events[event_type]:
return
# Plugins have already been ordered from most important to least important
for cb in self.plugin_events[event_type]:
# log.debug("Executing plugin {}:{}".format(cb.strip(".py"), event_type))
# Execute the corresponding method in the plugin
resp = await cb(*args, **kwargs)
# COMMUNICATION
# If data is passed, assign proper variables
if not resp:
continue
if type(resp) is not list:
resp = (resp, )
# Multiple commands can be passed in a form of a tuple
for cmd in resp:
# Parse additional variables
if type(cmd) in (tuple, list, set):
# Unpacks parameters
cmd, arguments, *safe = cmd
# No additional arguments
else:
arguments = ()
# Makes communication between the core and plugins possible
# RETURN
# Exits the current event immediately and doesn't call any more plugins
if cmd == "return":
return
# ADD_VAR
# Adds a variable to the current kwargs
elif cmd == "add_var":
if type(arguments) is tuple:
# Arguments must be a dict
for k, v in arguments[0].items():
kwargs[k] = v
else:
for k, v in arguments.items():
kwargs[k] = v
# SHUTDOWN
# Calls the ON_SHUTDOWN event, then exists
elif cmd == "shutdown":
try:
await self.dispatch_event(ON_SHUTDOWN)
finally:
# Sys.exit is usually handled by developer.py in the ON_SHUTDOWN event
# but it is here as backup as well
sys.exit(0)
nano = Nano()
# DISCORD EVENTS -> NANO EVENTS
@client.event
async def on_message(message):
await nano.dispatch_event(ON_MESSAGE, message)
@client.event
async def on_reaction_add(reaction, user):
await nano.dispatch_event(ON_REACTION_ADD, reaction, user)
@client.event
async def on_message_delete(message):
await nano.dispatch_event(ON_MESSAGE_DELETE, message)
@client.event
async def on_message_edit(before, after):
await nano.dispatch_event(ON_MESSAGE_EDIT, before, after)
@client.event
async def on_channel_delete(channel):
await nano.dispatch_event(ON_CHANNEL_DELETE, channel)
@client.event
async def on_channel_create(channel):
await nano.dispatch_event(ON_CHANNEL_DELETE, channel)
@client.event
async def on_channel_update(before, after):
await nano.dispatch_event(ON_CHANNEL_UPDATE, before, after)
@client.event
async def on_member_join(member):
await nano.dispatch_event(ON_MEMBER_JOIN, member)
@client.event
async def on_member_remove(member):
await nano.dispatch_event(ON_MEMBER_REMOVE, member)
@client.event
async def on_member_update(before, after):
await nano.dispatch_event(ON_MEMBER_UPDATE, before, after)
@client.event
async def on_member_ban(guild, user):
await nano.dispatch_event(ON_MEMBER_BAN, guild, user)
@client.event
async def on_member_unban(guild, member):
await nano.dispatch_event(ON_MEMBER_UNBAN, guild, member)
@client.event
async def on_guild_join(guild):
await nano.dispatch_event(ON_GUILD_JOIN, guild)
@client.event
async def on_guild_remove(guild):
await nano.dispatch_event(ON_GUILD_REMOVE, guild)
@client.event
async def on_error(event, *args, **kwargs):
await nano.dispatch_event(ON_ERROR, event, *args, **kwargs)
# Do I really need all of this? No, I don't.
@client.event
async def on_ready():
await client.wait_until_ready()
# Just prints "Resumed connection" if that's the way it is
global IS_RESUME
if IS_RESUME:
print("Resumed connection...")
log_to_file("Resumed connection as {}".format(client.user.name))
return
IS_RESUME = True
print("Bot connected!")
print("BOT name: {} ({})".format(client.user.name, client.user.id))
log_to_file("Connected as {} ({})".format(client.user.name, client.user.id))
await nano.dispatch_event(ON_READY)
async def start():
if not parser.has_option("Credentials", "token"):
log.critical("Token not found. Check your settings.ini")
log_to_file("Could not start: Token not specified")
return
token = parser.get("Credentials", "token")
await client.login(token)
await client.connect()
def main():
try:
print("Connecting to Discord...", end="")
loop.run_until_complete(start())
except Exception as e:
loop.run_until_complete(client.logout())
log.critical("Something went wrong, quitting (see log bugs.txt)")
log_to_file("CRITICAL, shutting down: {}".format(e), "bug")
# Attempts to save plugin state
log.critical("Dispatching ON_SHUTDOW...")
loop.run_until_complete(nano.dispatch_event(ON_SHUTDOWN))
log.critical("Shutting down...")
finally:
loop.close()
if __name__ == '__main__':
main()