refactor: clean up initialization and reloading
This commit is contained in:
parent
32817f735e
commit
04ef3d1c83
@ -13,7 +13,6 @@ RELOADABLE_MODULES = [
|
||||
"constants",
|
||||
"core",
|
||||
"events",
|
||||
"player",
|
||||
"utils",
|
||||
"voice",
|
||||
"youtubedl",
|
||||
|
19
core.py
19
core.py
@ -16,6 +16,9 @@ from state import command_locks
|
||||
|
||||
|
||||
async def on_message(message):
|
||||
if not message.content.startswith(constants.PREFIX):
|
||||
return
|
||||
|
||||
tokens = commands.tokenize(message.content)
|
||||
if not tokens:
|
||||
return
|
||||
@ -122,16 +125,16 @@ async def on_message(message):
|
||||
def rreload(reloaded_modules, module):
|
||||
reloaded_modules.add(module.__name__)
|
||||
importlib.reload(module)
|
||||
|
||||
if "__reload_module__" in dir(module):
|
||||
module.__reload_module__()
|
||||
|
||||
with contextlib.suppress(AttributeError):
|
||||
for submodule in filter(
|
||||
lambda v: inspect.ismodule(v)
|
||||
and v.__name__ in constants.RELOADABLE_MODULES
|
||||
and v.__name__ not in reloaded_modules,
|
||||
map(lambda attr: getattr(module, attr), dir(module)),
|
||||
):
|
||||
rreload(reloaded_modules, submodule)
|
||||
for submodule in filter(
|
||||
lambda v: inspect.ismodule(v)
|
||||
and v.__name__ in constants.RELOADABLE_MODULES
|
||||
and v.__name__ not in reloaded_modules,
|
||||
vars(module).values(),
|
||||
):
|
||||
rreload(reloaded_modules, submodule)
|
||||
|
||||
importlib.reload(module)
|
||||
|
22
events.py
22
events.py
@ -1,7 +1,25 @@
|
||||
import core
|
||||
import events
|
||||
from state import client
|
||||
|
||||
dynamic_handlers = {}
|
||||
|
||||
|
||||
async def trigger_dynamic_handlers(event_type: str, *data):
|
||||
if event_type in dynamic_handlers:
|
||||
for message_handler in dynamic_handlers[event_type]:
|
||||
await message_handler(*data)
|
||||
for dynamic_handler in dynamic_handlers[event_type]:
|
||||
await dynamic_handler(*data)
|
||||
|
||||
|
||||
@client.event
|
||||
async def on_message_edit(before, after):
|
||||
await events.trigger_dynamic_handlers("on_message_edit", before, after)
|
||||
|
||||
await core.on_message(after)
|
||||
|
||||
|
||||
@client.event
|
||||
async def on_message(message):
|
||||
await events.trigger_dynamic_handlers("on_message", message)
|
||||
|
||||
await core.on_message(message)
|
||||
|
19
main.py
19
main.py
@ -1,31 +1,14 @@
|
||||
import time
|
||||
|
||||
import constants
|
||||
import core
|
||||
import events
|
||||
from state import client, start_time
|
||||
|
||||
|
||||
@client.event
|
||||
async def on_ready():
|
||||
await events.trigger_dynamic_handlers("on_ready")
|
||||
print(f"logged in as {client.user} in {round(time.time() - start_time, 1)}s")
|
||||
|
||||
|
||||
@client.event
|
||||
async def on_message_edit(before, after):
|
||||
await events.trigger_dynamic_handlers("on_message_edit", before, after)
|
||||
|
||||
await on_message(after)
|
||||
|
||||
|
||||
@client.event
|
||||
async def on_message(message):
|
||||
await events.trigger_dynamic_handlers("on_message", message)
|
||||
|
||||
if not message.content.startswith(constants.PREFIX):
|
||||
return
|
||||
|
||||
await core.on_message(message)
|
||||
|
||||
|
||||
client.run(constants.SECRETS["TOKEN"])
|
||||
|
Loading…
x
Reference in New Issue
Block a user