@@ -263,6 +263,17 @@ async def load_plugin(self, plugin):
263
263
logger .error ("Plugin load failure: %s" , plugin .ext_string , exc_info = True )
264
264
raise InvalidPluginError ("Cannot load extension, plugin invalid." ) from exc
265
265
266
+ async def unload_plugin (self , plugin : Plugin ) -> None :
267
+ try :
268
+ await self .bot .unload_extension (plugin .ext_string )
269
+ except commands .ExtensionError as exc :
270
+ raise exc
271
+
272
+ ext_parent = "." .join (plugin .ext_string .split ("." )[:- 1 ])
273
+ for module in list (sys .modules .keys ()):
274
+ if module == ext_parent or module .startswith (ext_parent + "." ):
275
+ del sys .modules [module ]
276
+
266
277
async def parse_user_input (self , ctx , plugin_name , check_version = False ):
267
278
if not self .bot .config ["enable_plugins" ]:
268
279
embed = discord .Embed (
@@ -376,7 +387,7 @@ async def plugins_add(self, ctx, *, plugin_name: str):
376
387
logger .warning ("Unable to download plugin %s." , plugin , exc_info = True )
377
388
378
389
embed = discord .Embed (
379
- description = f"Failed to download plugin, check logs for error.\n { type (e )} : { e } " ,
390
+ description = f"Failed to download plugin, check logs for error.\n { type (e ). __name__ } : { e } " ,
380
391
color = self .bot .error_color ,
381
392
)
382
393
@@ -394,7 +405,7 @@ async def plugins_add(self, ctx, *, plugin_name: str):
394
405
logger .warning ("Unable to load plugin %s." , plugin , exc_info = True )
395
406
396
407
embed = discord .Embed (
397
- description = f"Failed to download plugin, check logs for error.\n { type (e )} : { e } " ,
408
+ description = f"Failed to load plugin, check logs for error.\n { type (e ). __name__ } : { e } " ,
398
409
color = self .bot .error_color ,
399
410
)
400
411
@@ -435,7 +446,7 @@ async def plugins_remove(self, ctx, *, plugin_name: str):
435
446
436
447
if self .bot .config .get ("enable_plugins" ):
437
448
try :
438
- await self .bot . unload_extension (plugin . ext_string )
449
+ await self .unload_plugin (plugin )
439
450
self .loaded_plugins .remove (plugin )
440
451
except (commands .ExtensionNotLoaded , KeyError ):
441
452
logger .warning ("Plugin was never loaded." )
@@ -477,9 +488,10 @@ async def update_plugin(self, ctx, plugin_name):
477
488
await self .download_plugin (plugin , force = True )
478
489
if self .bot .config .get ("enable_plugins" ):
479
490
try :
480
- await self .bot . unload_extension (plugin . ext_string )
491
+ await self .unload_plugin (plugin )
481
492
except commands .ExtensionError :
482
493
logger .warning ("Plugin unload fail." , exc_info = True )
494
+
483
495
try :
484
496
await self .load_plugin (plugin )
485
497
except Exception :
@@ -526,17 +538,20 @@ async def plugins_reset(self, ctx):
526
538
for ext in list (self .bot .extensions ):
527
539
if not ext .startswith ("plugins." ):
528
540
continue
541
+ logger .error ("Unloading plugin: %s." , ext )
529
542
try :
530
- logger .error ("Unloading plugin: %s." , ext )
531
- await self .bot .unload_extension (ext )
532
- except Exception :
533
- logger .error ("Failed to unload plugin: %s." , ext )
534
- else :
535
- if not self .loaded_plugins :
536
- continue
537
543
plugin = next ((p for p in self .loaded_plugins if p .ext_string == ext ), None )
538
544
if plugin :
545
+ await self .unload_plugin (plugin )
539
546
self .loaded_plugins .remove (plugin )
547
+ else :
548
+ await self .bot .unload_extension (ext )
549
+ except Exception :
550
+ logger .error ("Failed to unload plugin: %s." , ext )
551
+
552
+ for module in list (sys .modules .keys ()):
553
+ if module .startswith ("plugins." ):
554
+ del sys .modules [module ]
540
555
541
556
self .bot .config ["plugins" ].clear ()
542
557
await self .bot .config .update ()
0 commit comments