Added message service

This commit is contained in:
Sven Heidemann 2021-11-16 19:06:09 +01:00
parent d2c233a855
commit 26e1ed7496
5 changed files with 79 additions and 5 deletions

View File

@ -21,10 +21,12 @@
"Prefix": "!dev-g",
"Servers": [
{
"Id": "511824600884051979"
"Id": "511824600884051979",
"MessageDeleteTimer": 2
},
{
"Id": "910199451145076828"
"Id": "910199451145076828",
"MessageDeleteTimer": 2
}
]
}

View File

@ -7,9 +7,10 @@ from cpl_core.dependency_injection import (ServiceCollectionABC,
ServiceProviderABC)
from cpl_core.environment import ApplicationEnvironment
from cpl_core.logging import LoggerABC
from gismo_core.abc.bot_service_abc import BotServiceABC
from gismo_core.abc.message_service_abc import MessageServiceABC
from gismo_core.service.bot_service import BotService
from gismo_core.service.message_service import MessageService
from modules.boot_log.boot_log import BootLog
from modules_core.abc.module_abc import ModuleABC
from modules_core.abc.module_service_abc import ModuleServiceABC
@ -41,6 +42,7 @@ class Startup(StartupABC):
services.add_singleton(ModuleServiceABC, ModuleService)
services.add_singleton(BotServiceABC, BotService)
services.add_transient(MessageServiceABC, MessageService)
services.add_transient(ModuleABC, BootLog)

View File

@ -0,0 +1,24 @@
from abc import ABC, abstractmethod
from typing import Union
import discord
from cpl_query.extension import List
from discord.ext.commands import Context
class MessageServiceABC(ABC):
@abstractmethod
def __init__(self): pass
@abstractmethod
async def delete_message(self, messages: List[discord.Message]): pass
@abstractmethod
async def send_channel_message(self, channel: discord.TextChannel, message: str): pass
@abstractmethod
async def send_dm_message(self, message: str, receiver: Union[discord.User, discord.Member]): pass
@abstractmethod
async def send_ctx_msg(self, ctx: Context, message: str, file: discord.File = None): pass

View File

@ -9,15 +9,21 @@ class ServerSettings(ConfigurationModelABC):
def __init__(self):
ConfigurationModelABC.__init__(self)
self._id: int = ''
self._id: int = 0
self._message_delete_timer: int = 0
@property
def id(self) -> str:
return self._id
@property
def message_delete_timer(self) -> int:
return self._message_delete_timer
def from_dict(self, settings: dict):
try:
self._id = settings['Id']
self._id = int(settings['Id'])
self._message_delete_timer = int(settings['MessageDeleteTimer'])
except Exception as e:
Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in settings')
Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')

View File

@ -0,0 +1,40 @@
import asyncio
from typing import Union
import discord
from cpl_core.configuration.configuration_abc import ConfigurationABC
from cpl_core.logging import LoggerABC
from cpl_query.extension import List
from discord.ext.commands import Context
from gismo_core.abc.bot_service_abc import BotServiceABC
from gismo_core.abc.message_service_abc import MessageServiceABC
from gismo_core.configuration.server_settings import ServerSettings
class MessageService(MessageServiceABC):
def __init__(self, config: ConfigurationABC, logger: LoggerABC, bot: BotServiceABC):
self._config = config
self._logger = logger
self._bot = bot
async def delete_message(self, messages: List[discord.Message]):
self._logger.debug(__name__, f'Try to delete {messages.count()} messages')
for message in messages:
message: discord.Message = message
server_st: ServerSettings = self._config.get_configuration(f'DSERVER_{message.g.id}')
await asyncio.sleep(server_st.message_delete_timer)
try:
self._logger.trace(__name__, f'Try to delete message: {message.content}')
await message.delete()
self._logger.trace(__name__, 'Deleted message')
except Exception as e:
self._logger.warn(__name__, f'Deleting message failed')
self._logger.debug(__name__, 'Deleting messages finished')
async def send_channel_message(self, channel: discord.TextChannel, message: str): pass
async def send_dm_message(self, message: str, receiver: Union[discord.User, discord.Member]): pass
async def send_ctx_msg(self, ctx: Context, message: str, file: discord.File = None): pass