Added on_message handling

This commit is contained in:
Sven Heidemann 2021-11-17 19:43:00 +01:00
parent 5b61de0bf1
commit 6509f628ca
6 changed files with 45 additions and 14 deletions

View File

@ -8,7 +8,7 @@
"LoggingSettings": { "LoggingSettings": {
"Path": "logs/", "Path": "logs/",
"Filename": "log_$start_time.log", "Filename": "log_dev.log",
"ConsoleLogLevel": "TRACE", "ConsoleLogLevel": "TRACE",
"FileLogLevel": "TRACE" "FileLogLevel": "TRACE"
}, },

View File

@ -1,17 +1,19 @@
from datetime import datetime from datetime import datetime
import discord
from discord import guild
from async_timeout import asyncio
from cpl_core.configuration import ConfigurationABC from cpl_core.configuration import ConfigurationABC
from cpl_core.console import Console from cpl_core.console import Console
from cpl_core.logging import LoggerABC, LoggingLevelEnum, LoggingSettings from cpl_core.logging import LoggerABC, LoggingLevelEnum, LoggingSettings
from discord import guild
from gismo_core.abc.bot_service_abc import BotServiceABC from gismo_core.abc.bot_service_abc import BotServiceABC
from gismo_core.abc.message_service_abc import MessageServiceABC from gismo_core.abc.message_service_abc import MessageServiceABC
from gismo_core.configuration.server_settings import ServerSettings from gismo_core.configuration.server_settings import ServerSettings
from modules_core.abc.module_abc import ModuleABC from modules_core.abc.module_abc import ModuleABC
from modules_core.abc.on_ready_abc import OnReadyABC
class BootLog(ModuleABC): class BootLog(ModuleABC, OnReadyABC):
def __init__( def __init__(
self, self,

View File

@ -1,13 +1,8 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from cpl_query.extension import List
from modules_core.events_enum import EventsEnum import discord
class ModuleABC(ABC): class ModuleABC(ABC):
@abstractmethod @abstractmethod
def __init__(self): pass def __init__(self): pass
# @property
# @abstractmethod
# def events(self) -> List[EventsEnum]: pass

View File

@ -0,0 +1,11 @@
from abc import ABC, abstractmethod
import discord
class OnMessageABC(ABC):
@abstractmethod
def __init__(self): pass
@abstractmethod
async def on_message(self, message: discord.Message): pass

View File

@ -0,0 +1,10 @@
from abc import ABC, abstractmethod
class OnReadyABC(ABC):
@abstractmethod
def __init__(self): pass
@abstractmethod
async def on_ready(self): pass

View File

@ -1,3 +1,4 @@
from async_timeout import asyncio
import discord import discord
from discord.ext import commands from discord.ext import commands
@ -7,7 +8,8 @@ from cpl_core.logging import LoggerABC
from cpl_query.extension import List from cpl_query.extension import List
from modules_core.abc.module_abc import ModuleABC from modules_core.abc.module_abc import ModuleABC
from modules_core.abc.module_service_abc import ModuleServiceABC from modules_core.abc.module_service_abc import ModuleServiceABC
from modules_core.events_enum import EventsEnum from modules_core.abc.on_message_abc import OnMessageABC
from modules_core.abc.on_ready_abc import OnReadyABC
class ModuleService(ModuleServiceABC): class ModuleService(ModuleServiceABC):
@ -21,10 +23,21 @@ class ModuleService(ModuleServiceABC):
async def on_ready(self): async def on_ready(self):
self._logger.debug(__name__, 'Start on_ready modules') self._logger.debug(__name__, 'Start on_ready modules')
modules = self._modules.where(lambda m: hasattr(m, EventsEnum.on_ready.value) and callable(m.on_ready)) modules = self._modules.where(lambda m: issubclass(m, OnReadyABC))
for module_type in modules: for module_type in modules:
module = self._services.get_service(module_type) module_type = module_type
module: OnReadyABC = self._services.get_service(module_type)
await module.on_ready() await module.on_ready()
self._logger.debug(__name__, 'Stopped on_ready modules')
async def on_message(self, message: discord.Message): async def on_message(self, message: discord.Message):
pass self._logger.debug(__name__, 'Start on_message modules')
modules = self._modules.where(lambda m: issubclass(m, OnMessageABC))
for module_type in modules:
module_type = module_type
module: OnMessageABC = self._services.get_service(module_type)
await module.on_message(message)
self._logger.debug(__name__, f'Stopped on_message modules')