refactor: rewrite reload system
This commit is contained in:
parent
8985999d6c
commit
6d7b46a7e5
@ -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("_")})
|
||||
|
@ -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__()
|
||||
|
@ -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__()
|
||||
|
@ -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__()
|
||||
|
@ -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",
|
||||
|
15
events.py
15
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__()
|
||||
|
43
main.py
43
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"])
|
||||
|
1
state.py
1
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
|
||||
|
13
ytdlp.py
13
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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user