diff --git a/commands/__init__.py b/commands/__init__.py index c054852..f433740 100644 --- a/commands/__init__.py +++ b/commands/__init__.py @@ -1,21 +1,6 @@ -import importlib -import inspect - -from state import reloaded_modules - from . import bot, tools, utils, voice from .utils import * def __reload_module__(): - for name, module in globals().items(): - if ( - inspect.ismodule(module) - and name not in constants.RELOAD_BLACKLISTED_MODULES - ): - importlib.reload(module) - if "__reload_module__" in dir(module) and name not in reloaded_modules: - reloaded_modules.add(name) - module.__reload_module__() - globals().update({k: v for k, v in vars(utils).items() if not k.startswith("_")}) diff --git a/commands/bot.py b/commands/bot.py index 0bacb95..428dd7e 100644 --- a/commands/bot.py +++ b/commands/bot.py @@ -1,12 +1,9 @@ -import importlib -import inspect import time import arguments import commands -import constants import utils -from state import reloaded_modules, start_time +from state import start_time async def uptime(message): @@ -28,15 +25,3 @@ async def uptime(message): await utils.reply(message, f"{round(start_time)}") else: await utils.reply(message, f"up {round(time.time() - start_time)} seconds") - - -def __reload_module__(): - for name, module in globals().items(): - if ( - inspect.ismodule(module) - and name not in constants.RELOAD_BLACKLISTED_MODULES - ): - importlib.reload(module) - if "__reload_module__" in dir(module) and name not in reloaded_modules: - reloaded_modules.add(name) - module.__reload_module__() diff --git a/commands/tools.py b/commands/tools.py index ddf5303..cb85a05 100644 --- a/commands/tools.py +++ b/commands/tools.py @@ -1,11 +1,8 @@ -import importlib -import inspect import re import arguments import commands -import constants -from state import reloaded_modules +import utils async def clear(message): @@ -63,15 +60,3 @@ async def clear(message): ) except: pass - - -def __reload_module__(): - for name, module in globals().items(): - if ( - inspect.ismodule(module) - and name not in constants.RELOAD_BLACKLISTED_MODULES - ): - importlib.reload(module) - if "__reload_module__" in dir(module) and name not in reloaded_modules: - reloaded_modules.add(name) - module.__reload_module__() diff --git a/commands/voice.py b/commands/voice.py index b0e7cef..c9e2926 100644 --- a/commands/voice.py +++ b/commands/voice.py @@ -1,12 +1,8 @@ -import importlib -import inspect - import arguments import commands -import constants import utils import ytdlp -from state import client, playback_queue, reloaded_modules +from state import client, playback_queue async def queue_or_play(message): @@ -225,15 +221,3 @@ def generate_queue_list(queue: list): for i, queued in enumerate(queue) ] ) - - -def __reload_module__(): - for name, module in globals().items(): - if ( - inspect.ismodule(module) - and name not in constants.RELOAD_BLACKLISTED_MODULES - ): - importlib.reload(module) - if "__reload_module__" in dir(module) and name not in reloaded_modules: - reloaded_modules.add(name) - module.__reload_module__() diff --git a/constants.py b/constants.py index 21bf88a..d81f69e 100644 --- a/constants.py +++ b/constants.py @@ -1,9 +1,10 @@ import os +import sys EMBED_COLOR = 0xFF6600 OWNERS = [531392146767347712] PREFIX = "%" -RELOAD_BLACKLISTED_MODULES = ["re", "argparse"] +RELOAD_BLACKLISTED_MODULES = [*sys.builtin_module_names] YTDL_OPTIONS = { "default_search": "auto", diff --git a/events.py b/events.py index a02176b..e16ba7c 100644 --- a/events.py +++ b/events.py @@ -1,6 +1,4 @@ import contextlib -import importlib -import inspect import io import textwrap import traceback @@ -10,7 +8,6 @@ import disnake_paginator import commands import constants import utils -from state import reloaded_modules async def on_message(message): @@ -99,15 +96,3 @@ async def on_message(message): message, f"exception occurred while processing command: ```\n{''.join(traceback.format_exception(e)).replace('`', '\\`')}```", ) - - -def __reload_module__(): - for name, module in globals().items(): - if ( - inspect.ismodule(module) - and name not in constants.RELOAD_BLACKLISTED_MODULES - ): - importlib.reload(module) - if "__reload_module__" in dir(module) and name not in reloaded_modules: - reloaded_modules.add(name) - module.__reload_module__() diff --git a/main.py b/main.py index c998f52..57f04a1 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +import contextlib import importlib import inspect import time @@ -6,7 +7,7 @@ import commands import constants import core import events -from state import client, reloaded_modules, start_time +from state import client, start_time @client.event @@ -33,20 +34,40 @@ async def on_message(message): if message.author.id in constants.OWNERS and commands.match(message.content) == [ commands.Command.RELOAD ]: - for name, module in globals().items(): - if ( - inspect.ismodule(module) - and name not in constants.RELOAD_BLACKLISTED_MODULES - ): - importlib.reload(module) - if "__reload_module__" in dir(module) and name not in reloaded_modules: - reloaded_modules.add(name) - module.__reload_module__() - reloaded_modules.clear() + reloaded_modules = set() + for module in filter( + lambda v: inspect.ismodule(v) + and v.__name__ not in constants.RELOAD_BLACKLISTED_MODULES, + globals().values(), + ): + rreload(reloaded_modules, module) + await message.add_reaction("✅") return await events.on_message(message) +def rreload(reloaded_modules, module): + reloaded_modules.add(module) + importlib.reload(module) + if "__reload_module__" in dir(module): + module.__reload_module__() + + with contextlib.suppress(AttributeError): + for module in filter( + lambda m: m.__spec__.origin != "frozen", + filter( + lambda v: inspect.ismodule(v) + and ( + v.__name__.split(".")[-1] + not in constants.RELOAD_BLACKLISTED_MODULES + ) + and (v not in reloaded_modules), + map(lambda attr: getattr(module, attr), dir(module)), + ), + ): + rreload(reloaded_modules, module) + + client.run(constants.SECRETS["TOKEN"]) diff --git a/state.py b/state.py index 6659e11..35fc7c8 100644 --- a/state.py +++ b/state.py @@ -5,7 +5,6 @@ import disnake start_time = time.time() playback_queue = {} -reloaded_modules = set() intents = disnake.Intents.default() intents.message_content = True diff --git a/ytdlp.py b/ytdlp.py index be8bfe3..59b5fde 100644 --- a/ytdlp.py +++ b/ytdlp.py @@ -1,13 +1,10 @@ import asyncio -import importlib -import inspect from typing import Any, Optional import disnake import yt_dlp import constants -from state import reloaded_modules ytdl = yt_dlp.YoutubeDL(constants.YTDL_OPTIONS) @@ -45,15 +42,5 @@ class YTDLSource(disnake.PCMVolumeTransformer): def __reload_module__(): - for name, module in globals().items(): - if ( - inspect.ismodule(module) - and name not in constants.RELOAD_BLACKLISTED_MODULES - ): - importlib.reload(module) - if "__reload_module__" in dir(module) and name not in reloaded_modules: - reloaded_modules.add(name) - module.__reload_module__() - global ytdl ytdl = yt_dlp.YoutubeDL(constants.YTDL_OPTIONS)