A-0.3 - Basismodul #36

Merged
edraft merged 48 commits from 0.3 into Alpha 2021-12-27 18:17:42 +01:00
2 changed files with 21 additions and 1 deletions
Showing only changes of commit 68c1bc0bc3 - Show all commits

View File

@ -3,6 +3,7 @@ from typing import Union
import discord import discord
from cpl_core.configuration.configuration_abc import ConfigurationABC from cpl_core.configuration.configuration_abc import ConfigurationABC
from cpl_core.database.context.database_context_abc import DatabaseContextABC
from cpl_core.logging import LoggerABC from cpl_core.logging import LoggerABC
from cpl_query.extension import List from cpl_query.extension import List
from discord.ext.commands import Context from discord.ext.commands import Context
@ -10,14 +11,17 @@ from discord.ext.commands import Context
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 gismo_data.abc.client_repository_abc import ClientRepositoryABC
class MessageService(MessageServiceABC): class MessageService(MessageServiceABC):
def __init__(self, config: ConfigurationABC, logger: LoggerABC, bot: BotServiceABC): def __init__(self, config: ConfigurationABC, logger: LoggerABC, bot: BotServiceABC, clients: ClientRepositoryABC, db: DatabaseContextABC):
self._config = config self._config = config
self._logger = logger self._logger = logger
self._bot = bot self._bot = bot
self._clients = clients
self._db = db
async def delete_messages(self, messages: List[discord.Message]): async def delete_messages(self, messages: List[discord.Message]):
self._logger.debug(__name__, f'Try to delete {messages.count()} messages') self._logger.debug(__name__, f'Try to delete {messages.count()} messages')
@ -29,11 +33,14 @@ class MessageService(MessageServiceABC):
server_st: ServerSettings = self._config.get_configuration(f'DSERVER_{message.guild.id}') server_st: ServerSettings = self._config.get_configuration(f'DSERVER_{message.guild.id}')
await asyncio.sleep(server_st.message_delete_timer) await asyncio.sleep(server_st.message_delete_timer)
self._logger.debug(__name__, f'Try to delete message:\n\t{message}\n\t{message.content}') self._logger.debug(__name__, f'Try to delete message:\n\t{message}\n\t{message.content}')
guild_id = message.guild.id
try: try:
await message.delete() await message.delete()
except Exception as e: except Exception as e:
self._logger.error(__name__, f'Deleting message failed', e) self._logger.error(__name__, f'Deleting message failed', e)
else: else:
self._clients.apppend_deleted_message_count(self._bot.user.id, guild_id)
self._db.save_changes()
self._logger.info(__name__, f'Deleted message {message}') self._logger.info(__name__, f'Deleted message {message}')
async def send_channel_message(self, channel: discord.TextChannel, message: str): async def send_channel_message(self, channel: discord.TextChannel, message: str):
@ -45,6 +52,8 @@ class MessageService(MessageServiceABC):
self._logger.error(__name__, f'Send message to channel {channel.id} failed', e) self._logger.error(__name__, f'Send message to channel {channel.id} failed', e)
else: else:
self._logger.info(__name__, f'Sent message to channel {channel.id}') self._logger.info(__name__, f'Sent message to channel {channel.id}')
self._clients.apppend_sent_message_count(self._bot.user.id, channel.guild.id)
self._db.save_changes()
await self.delete_message(msg) await self.delete_message(msg)
async def send_dm_message(self, message: str, receiver: Union[discord.User, discord.Member]): async def send_dm_message(self, message: str, receiver: Union[discord.User, discord.Member]):
@ -54,6 +63,8 @@ class MessageService(MessageServiceABC):
except Exception as e: except Exception as e:
self._logger.error(__name__, f'Send message to user {receiver.id} failed', e) self._logger.error(__name__, f'Send message to user {receiver.id} failed', e)
else: else:
self._clients.apppend_sent_message_count(self._bot.user.id, receiver.guild.id)
self._db.save_changes()
self._logger.info(__name__, f'Sent message to user {receiver.id}') self._logger.info(__name__, f'Sent message to user {receiver.id}')
async def send_ctx_msg(self, ctx: Context, message: Union[str, discord.File]): async def send_ctx_msg(self, ctx: Context, message: Union[str, discord.File]):
@ -73,4 +84,6 @@ class MessageService(MessageServiceABC):
self._logger.error(__name__, f'Send message to channel {ctx.channel.id} failed', e) self._logger.error(__name__, f'Send message to channel {ctx.channel.id} failed', e)
else: else:
self._logger.info(__name__, f'Sent message to channel {ctx.channel.id}') self._logger.info(__name__, f'Sent message to channel {ctx.channel.id}')
self._clients.apppend_sent_message_count(self._bot.user.id, ctx.guild.id)
self._db.save_changes()
await self.delete_message(msg) await self.delete_message(msg)

View File

@ -42,6 +42,13 @@ class Base(ModuleABC, OnMemberJoinABC, OnMessageABC, OnVoiceStateUpdateABC):
except Exception as e: except Exception as e:
self._logger.error(__name__, f'Cannot edit client {self._bot.user.id}@{g_id}') self._logger.error(__name__, f'Cannot edit client {self._bot.user.id}@{g_id}')
def _apppend_deleted_message_count(self, g_id: int):
try:
self._clients.apppend_received_message_count(self._bot.user.id, g_id, 1)
self._db.save_changes()
except Exception as e:
self._logger.error(__name__, f'Cannot edit client {self._bot.user.id}@{g_id}')
async def on_member_join(self, member: discord.Member): async def on_member_join(self, member: discord.Member):
self._logger.debug(__name__, f'Module {type(self)} started') self._logger.debug(__name__, f'Module {type(self)} started')