Added logic to handle event priority
This commit is contained in:
		| @@ -6,3 +6,6 @@ class ModuleABC(ABC): | ||||
|  | ||||
|     @abstractmethod | ||||
|     def __init__(self): pass | ||||
|      | ||||
|     @abstractmethod | ||||
|     def get_priority(self, t: type) -> int: pass | ||||
|   | ||||
| @@ -1,4 +1,4 @@ | ||||
| from abc import ABCMeta | ||||
| from abc import ABC, ABCMeta | ||||
| from datetime import datetime | ||||
| from typing import Optional, Sequence, Union | ||||
| import discord | ||||
| @@ -25,17 +25,24 @@ class ModuleService(ModuleServiceABC, commands.Cog, metaclass=_MetaCogABC): | ||||
|         self._env = env | ||||
|         self._modules: List[ModuleABC] = List() | ||||
|         self._modules.extend(ModuleABC.__subclasses__()) | ||||
|  | ||||
|          | ||||
|     def _get_modules(self, t: type) -> List[ModuleABC]: | ||||
|         module_types = self._modules.where(lambda m: issubclass(m, t)) | ||||
|         modules = List(t) | ||||
|         for module_type in module_types: | ||||
|             modules.append(self._services.get_service(module_type)) | ||||
|              | ||||
|         return modules.order_by(lambda m: m.get_priority(t)) | ||||
|      | ||||
|     async def on_connect(self): pass | ||||
|  | ||||
|     async def on_disconnect(self): pass | ||||
|  | ||||
|     async def on_ready(self): | ||||
|         self._logger.debug(__name__, 'Start on_ready modules') | ||||
|         modules = self._modules.where(lambda m: issubclass(m, OnReadyABC)) | ||||
|         for module_type in modules: | ||||
|             module: OnReadyABC = self._services.get_service(module_type) | ||||
|         self._logger.debug(__name__, f'Start on_ready modules') | ||||
|         for module in self._get_modules(OnReadyABC): | ||||
|             await module.on_ready() | ||||
|          | ||||
|         self._logger.debug(__name__, 'Stopped on_ready modules') | ||||
|  | ||||
|     async def on_resume(self): pass | ||||
|   | ||||
		Reference in New Issue
	
	Block a user