[bot-base] Visiblement ca a l'air de marcher correctement

This commit is contained in:
Louis Chauvet 2020-04-21 17:35:15 +02:00
parent a590a4cba1
commit 10e1b2333c
Signed by: fomys
GPG Key ID: 1ECA046A9615ABA0
3 changed files with 15 additions and 2 deletions

View File

@ -62,7 +62,14 @@ class BotBase(discord.Client):
async def on_ready(self):
self.info("Bot ready.")
def load_module(self, module):
def load_module(self, module: str) -> None:
"""
Try to load module
:raise ModuleNotFoundError: If module is not in module folder
:raise IncompatibleModuleError: If module is incompatible
:param str module: module to load
"""
# Check if module exists
if not os.path.isdir(os.path.join(self.config["modules_folder"], module)):
raise ModuleNotFoundError(f"Module {module} not found in modules folder ({self.config['modules_folder']}.)")
@ -100,7 +107,7 @@ class BotBase(discord.Client):
raise IncompatibleModuleError(f"Module {module} mainclass ({main_class}) does not provide __dispatch__"
f" attribute)")
# Check if __dispatch__ is function
if not inspect.isfunction(dispatch):
if not inspect.isfunction(imported.__main_class__.__dispatch__):
raise IncompatibleModuleError(f"Module {module} mainclass ({main_class}) provides __dispatch__, but it is "
f"not a function ({dispatch}).")
# Check if __dispatch__ can have variable positional and keyword aguments (to avoid future error on each event)
@ -130,6 +137,12 @@ class BotBase(discord.Client):
}
})
def dispatch(self, event, *args, **kwargs):
"""Dispatch event"""
super().dispatch(event, *args, **kwargs)
for module in self.modules.values():
module["dispatch"](event, *args, **kwargs)
# Logging
def info(self, *args, **kwargs):
if self.log: