Code refactoring #35

This commit is contained in:
Sven Heidemann 2023-02-23 08:58:04 +01:00
parent e5fb9fd504
commit 2d995544c3
3 changed files with 11 additions and 11 deletions

View File

@ -4,7 +4,6 @@ from cpl_core.database.context import DatabaseContextABC
from cpl_query.extension import List from cpl_query.extension import List
from bot_core.logging.database_logger import DatabaseLogger from bot_core.logging.database_logger import DatabaseLogger
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.abc.user_repository_abc import UserRepositoryABC from bot_data.abc.user_repository_abc import UserRepositoryABC
from bot_data.abc.user_warnings_repository_abc import UserWarningsRepositoryABC from bot_data.abc.user_warnings_repository_abc import UserWarningsRepositoryABC
from bot_data.model.user_warnings import UserWarnings from bot_data.model.user_warnings import UserWarnings
@ -16,7 +15,6 @@ class UserWarningsRepositoryService(UserWarningsRepositoryABC):
logger: DatabaseLogger, logger: DatabaseLogger,
db_context: DatabaseContextABC, db_context: DatabaseContextABC,
users: UserRepositoryABC, users: UserRepositoryABC,
servers: ServerRepositoryABC,
): ):
self._logger = logger self._logger = logger
self._context = db_context self._context = db_context

View File

@ -371,12 +371,13 @@ class UserGroup(DiscordCommandABC):
title=member.name, description=self._t.transform("modules.base.user.atr.warnings"), color=int("ef9d0d", 16) title=member.name, description=self._t.transform("modules.base.user.atr.warnings"), color=int("ef9d0d", 16)
) )
warnings = self._user_warnings.get_user_warnings_by_user_id(user.id)
warnings_id_string = "" warnings_id_string = ""
for warning in self._user_warnings.get_user_warnings_by_user_id(user.id): for warning in warnings:
warnings_id_string += f"{warning.id}\n" warnings_id_string += f"{warning.id}\n"
warnings_description_string = "" warnings_description_string = ""
for warning in self._user_warnings.get_user_warnings_by_user_id(user.id): for warning in warnings:
warnings_description_string += f"{warning.description}\n" warnings_description_string += f"{warning.description}\n"
embed.add_field( embed.add_field(
@ -399,8 +400,8 @@ class UserGroup(DiscordCommandABC):
async def add(self, ctx: Context, member: discord.Member, description: str): async def add(self, ctx: Context, member: discord.Member, description: str):
self._logger.debug(__name__, f"Received command user warning add {ctx}:{member},{description}") self._logger.debug(__name__, f"Received command user warning add {ctx}:{member},{description}")
try: try:
await self._message_service.send_ctx_msg(ctx, self._t.transform("modules.base.warnings.add.success"))
await self._user_warnings_service.add_warnings(member, description, ctx.author.id) await self._user_warnings_service.add_warnings(member, description, ctx.author.id)
await self._message_service.send_ctx_msg(ctx, self._t.transform("modules.base.warnings.add.success"))
except Exception as e: except Exception as e:
self._logger.error(__name__, f"Adding user warning failed", e) self._logger.error(__name__, f"Adding user warning failed", e)
await self._message_service.send_ctx_msg(ctx, self._t.transform("modules.base.warnings.add.failed")) await self._message_service.send_ctx_msg(ctx, self._t.transform("modules.base.warnings.add.failed"))
@ -413,8 +414,8 @@ class UserGroup(DiscordCommandABC):
async def remove(self, ctx: Context, warning_id: int): async def remove(self, ctx: Context, warning_id: int):
self._logger.debug(__name__, f"Received command user warning remove {ctx}:{warning_id}") self._logger.debug(__name__, f"Received command user warning remove {ctx}:{warning_id}")
try: try:
await self._message_service.send_ctx_msg(ctx, self._t.transform("modules.base.warnings.remove.success"))
await self._user_warnings_service.remove_warnings(warning_id) await self._user_warnings_service.remove_warnings(warning_id)
await self._message_service.send_ctx_msg(ctx, self._t.transform("modules.base.warnings.remove.success"))
except Exception as e: except Exception as e:
self._logger.error(__name__, f"Removing user warning failed", e) self._logger.error(__name__, f"Removing user warning failed", e)
await self._message_service.send_ctx_msg(ctx, self._t.transform("modules.base.warnings.remove.failed")) await self._message_service.send_ctx_msg(ctx, self._t.transform("modules.base.warnings.remove.failed"))

View File

@ -57,13 +57,14 @@ class UserWarningsService:
else: else:
translation = self._t.transform("modules.base.warnings.team_warned").format(member.mention, description) translation = self._t.transform("modules.base.warnings.team_warned").format(member.mention, description)
await self._message_service.send_channel_message(channel, translation) self._bot.loop.create_task(self._message_service.send_channel_message(channel, translation))
except Exception as e: except Exception as e:
self._logger.error(__name__, f"Team notification for user warning failed!", e) self._logger.error(__name__, f"Team notification for user warning failed!", e)
async def notify_user(self, member: discord.Member, message: str): async def notify_user(self, member: discord.Member, message: str):
try: try:
await self._message_service.send_dm_message(message, member) # run as task to keep the interaction alive
self._bot.loop.create_task(self._message_service.send_dm_message(message, member))
except Exception as e: except Exception as e:
self._logger.error(__name__, f"User notification for user warning failed!", e) self._logger.error(__name__, f"User notification for user warning failed!", e)
@ -71,7 +72,7 @@ class UserWarningsService:
existing_warnings = self._warnings.get_user_warnings_by_user_id(user.id) existing_warnings = self._warnings.get_user_warnings_by_user_id(user.id)
if existing_warnings.count() == 1: if existing_warnings.count() == 1:
await self._message_service.send_dm_message(self._t.transform("modules.base.warnings.first"), member) await self.notify_user(member, self._t.transform("modules.base.warnings.first"))
elif existing_warnings.count() == 2: elif existing_warnings.count() == 2:
server = self._servers.get_server_by_discord_id(member.guild.id) server = self._servers.get_server_by_discord_id(member.guild.id)
@ -84,7 +85,7 @@ class UserWarningsService:
user.xp = new_level.min_xp user.xp = new_level.min_xp
self._users.update_user(user) self._users.update_user(user)
self._db.save_changes() self._db.save_changes()
await self._message_service.send_dm_message(self._t.transform("modules.base.warnings.second"), member) await self.notify_user(member, self._t.transform("modules.base.warnings.second"))
elif existing_warnings.count() == 3: elif existing_warnings.count() == 3:
server = self._servers.get_server_by_discord_id(member.guild.id) server = self._servers.get_server_by_discord_id(member.guild.id)
@ -96,7 +97,7 @@ class UserWarningsService:
user.xp = new_level.min_xp user.xp = new_level.min_xp
self._users.update_user(user) self._users.update_user(user)
self._db.save_changes() self._db.save_changes()
await self._message_service.send_dm_message(self._t.transform("modules.base.warnings.third"), member) await self.notify_user(member, self._t.transform("modules.base.warnings.third"))
elif existing_warnings.count() >= 4: elif existing_warnings.count() >= 4:
user.xp = 0 user.xp = 0