From 1e70893bbabd348503eedf7daec1d188f96149db Mon Sep 17 00:00:00 2001 From: Peter DeVita Date: Tue, 28 Nov 2023 21:12:36 -0500 Subject: [PATCH] bug: Fix voice component module not loading correctly --- atsume/bot.py | 8 ++++---- atsume/extensions/loader.py | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/atsume/bot.py b/atsume/bot.py index a29edf4..527a6ac 100644 --- a/atsume/bot.py +++ b/atsume/bot.py @@ -29,7 +29,7 @@ from atsume.db.manager import database from atsume.component.manager import manager as component_manager from atsume.component import Component, ComponentConfig -from atsume.extensions.loader import attach_extensions, load_module_setting +from atsume.extensions.loader import attach_extensions, load_module_class from atsume.utils import module_to_path @@ -69,9 +69,9 @@ def initialize_discord() -> typing.Tuple[hikari.GatewayBot, tanjun.Client]: ) if settings.VOICE_COMPONENT: - bot._voice = load_module_setting( - "VOICE_COMPONENT", hikari.impl.VoiceComponentImpl - ) + bot._voice = load_module_class( + settings.VOICE_COMPONENT, hikari.impl.VoiceComponentImpl + )(bot) global_commands = not settings.DEBUG and settings.GLOBAL_COMMANDS diff --git a/atsume/extensions/loader.py b/atsume/extensions/loader.py index 9a37992..e89ea8e 100644 --- a/atsume/extensions/loader.py +++ b/atsume/extensions/loader.py @@ -24,7 +24,7 @@ def attach_extensions(client: tanjun.Client) -> None: extensions.update(settings.EXTENSIONS) extensions.update(ATSUME_EXTENSIONS) for module_path in extensions: - func = load_module_setting(module_path, ExtensionCallable) + func = load_module_func(module_path, ExtensionCallable) func(client) @@ -36,7 +36,17 @@ def __init__(self, module_path: str): T = typing.TypeVar("T") -def load_module_setting(module_path: str, return_type: typing.Type[T]) -> T: +def load_module_func(module_path: str, return_type: typing.Type[T]) -> T: + path = module_path.split(".") + try: + module = importlib.import_module(".".join(path[:-1])) + func = getattr(module, path[-1]) + except (ModuleNotFoundError, AttributeError): + raise ModulePathNotFound(module_path) + return typing.cast(T, func) + + +def load_module_class(module_path: str, return_type: T) -> T: path = module_path.split(".") try: module = importlib.import_module(".".join(path[:-1]))