Skip to content

Commit

Permalink
better unload
Browse files Browse the repository at this point in the history
  • Loading branch information
greeeen-dev committed Oct 3, 2024
1 parent 47f4365 commit bbbce36
Showing 1 changed file with 44 additions and 21 deletions.
65 changes: 44 additions & 21 deletions cogs/lockdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,16 @@

import nextcord
from nextcord.ext import commands
import json
import os
import importlib
from utils import log, ui, langmgr, restrictions as r

try:
import ujson as json # pylint: disable=import-error
except:
pass

restrictions = r.Restrictions()
language = langmgr.partial()
language.load()
Expand All @@ -38,6 +46,41 @@ def __init__(self,bot):
self.bot.locked = False
self.logger = log.buildlogger(self.bot.package,'admin',self.bot.loglevel)

async def preunload(self, extension):
"""Performs necessary steps before unloading."""
info = None
plugin_name = None
if extension.startswith('cogs.'):
extension = extension.replace('cogs.','',1)
for plugin in os.listdir('plugins'):
if extension + '.json' == plugin:
plugin_name = plugin[:-5]
try:
with open('plugins/' + plugin) as file:
info = json.load(file)
except:
continue
break
else:
try:
with open('plugins/' + plugin) as file:
info = json.load(file)
except:
continue
if extension + '.py' in info['modules']:
plugin_name = plugin[:-5]
break
if not plugin_name:
return
if plugin_name == 'system':
return
if not info:
raise ValueError('Invalid plugin')
if not info['shutdown']:
return
script = importlib.import_module('utils.' + plugin_name + '_check')
await script.check(self.bot)

@commands.command(hidden=True,aliases=['globalkill'],description=language.desc('lockdown.lockdown'))
@restrictions.owner()
async def lockdown(self,ctx):
Expand Down Expand Up @@ -92,27 +135,6 @@ def check(interaction):
return await interaction.response.edit_message(view=components_cancel)

self.logger.critical(f'Bot lockdown issued by {ctx.author.id}!')

try:
self.logger.info("Shutting down Revolt client...")
await self.bot.revolt_session.close()
del self.bot.revolt_client
del self.bot.revolt_session
self.bot.unload_extension('cogs.bridge_revolt')
self.logger.info("Revolt client has been shut down.")
except Exception as e:
if not isinstance(e, AttributeError):
self.logger.exception("Shutdown failed.")
try:
self.logger.info("Shutting down Guilded client...")
await self.bot.guilded_client.close()
self.bot.guilded_client_task.cancel()
del self.bot.guilded_client
self.bot.unload_extension('cogs.bridge_guilded')
self.logger.info("Guilded client has been shut down.")
except Exception as e:
if not isinstance(e, AttributeError):
self.logger.exception("Shutdown failed.")
self.logger.info("Backing up message cache...")
await self.bot.bridge.backup()
self.logger.info("Backup complete")
Expand All @@ -124,6 +146,7 @@ def check(interaction):
self.bot.locked = True
for cog in list(self.bot.extensions):
if not cog=='cogs.lockdown':
await self.preunload(cog)
self.bot.unload_extension(cog)
self.logger.info("Lockdown complete")

Expand Down

0 comments on commit bbbce36

Please sign in to comment.