From 2401e58827145d6549b83b5a049105206aaef92a Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 13 Jan 2023 12:39:45 +0100 Subject: [PATCH] Fixed client utils & Added add, remove and reset commands #179 --- kdb-bot/src/bot/translation/de.json | 6 ++ kdb-bot/src/bot_core/abc/client_utils_abc.py | 1 + .../bot_core/service/client_utils_service.py | 5 +- .../src/modules/base/command/user_group.py | 69 +++++++++++++++---- .../base_on_voice_state_update_event.py | 4 +- 5 files changed, 68 insertions(+), 17 deletions(-) diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index f62a3422..17524fc7 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -187,7 +187,13 @@ "type_error": "Der angegebene Wert ist keine Zahl! :(" } }, + "add": { + "xp": "Die {} von {} wurden um {} erhöht" + }, "remove": { + "xp": "Die {} von {} wurden um {} verringert" + }, + "reset": { "xp": "Die {} von {} wurden entfernt", "ontime": "Die {} von {} wurden entfernt" }, diff --git a/kdb-bot/src/bot_core/abc/client_utils_abc.py b/kdb-bot/src/bot_core/abc/client_utils_abc.py index deb70ed4..50c934e0 100644 --- a/kdb-bot/src/bot_core/abc/client_utils_abc.py +++ b/kdb-bot/src/bot_core/abc/client_utils_abc.py @@ -1,3 +1,4 @@ +import datetime from abc import ABC, abstractmethod from datetime import datetime from typing import Callable diff --git a/kdb-bot/src/bot_core/service/client_utils_service.py b/kdb-bot/src/bot_core/service/client_utils_service.py index f596a033..a893a40d 100644 --- a/kdb-bot/src/bot_core/service/client_utils_service.py +++ b/kdb-bot/src/bot_core/service/client_utils_service.py @@ -17,6 +17,7 @@ from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from bot_core.configuration.feature_flags_settings import FeatureFlagsSettings from bot_data.abc.client_repository_abc import ClientRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_joined_voice_channel_repository_abc import UserJoinedVoiceChannelRepositoryABC from bot_data.abc.user_message_count_per_hour_repository_abc import UserMessageCountPerHourRepositoryABC from bot_data.model.user import User from bot_data.model.user_message_count_per_hour import UserMessageCountPerHour @@ -33,8 +34,8 @@ class ClientUtilsService(ClientUtilsABC): bot: DiscordBotServiceABC, servers: ServerRepositoryABC, clients: ClientRepositoryABC, - umcphs: UserMessageCountPerHourRepositoryABC, user_joined_vc: UserJoinedVoiceChannelRepositoryABC, + umcphs: UserMessageCountPerHourRepositoryABC, message_service: MessageServiceABC, db: DatabaseContextABC, t: TranslatePipe, @@ -90,8 +91,6 @@ class ClientUtilsService(ClientUtilsABC): async def check_if_bot_is_ready_yet_and_respond(self, ctx: Context) -> bool: result = await self.check_if_bot_is_ready_yet() if not result: - await self._message_service.send_ctx_msg(ctx, self._t.transform('common.errors.bot_not_ready_yet'), - without_tracking=True) await self._message_service.send_ctx_msg( ctx, self._t.transform('common.errors.bot_not_ready_yet'), diff --git a/kdb-bot/src/modules/base/command/user_group.py b/kdb-bot/src/modules/base/command/user_group.py index 5b2ce3b3..9bb0db0a 100644 --- a/kdb-bot/src/modules/base/command/user_group.py +++ b/kdb-bot/src/modules/base/command/user_group.py @@ -68,6 +68,35 @@ class UserGroup(DiscordCommandABC): self._atr_list = [(key, self._atr_dict[key]) for key in self._atr_dict] + async def _handle_atr_calc(self, ctx: Context, atr: str, value: int, member: discord.Member, is_remove=False): + if member is None or not isinstance(member, discord.Member): + member = ctx.author + + server = self._servers.find_server_by_discord_id(ctx.guild.id) + user = self._users.find_user_by_discord_id_and_server_id(member.id, server.server_id) + + if atr == 'xp': + if is_remove: + user.xp -= value + else: + user.xp += value + self._users.update_user(user) + self._db.save_changes() + await self._level.check_level(member) + + else: + await self._message_service.send_interaction_msg( + ctx.interaction, + self._t.transform('modules.base.user.error.atr_not_found').format(atr) + ) + return + + type = 'add' if not is_remove else 'remove' + await self._message_service.send_interaction_msg( + ctx.interaction, + self._t.transform(f'modules.base.user.{type}.{atr.lower()}').format(atr, value, member.mention) + ) + @commands.hybrid_group() @commands.guild_only() async def user(self, ctx: Context): @@ -180,7 +209,7 @@ class UserGroup(DiscordCommandABC): @commands.guild_only() @CommandChecks.check_is_ready() @CommandChecks.check_is_member_moderator() - async def set(self, ctx: Context, atr: str, value: str, member: discord.Member = None): + async def set(self, ctx: Context, atr: str, value: int, member: discord.Member = None): self._logger.debug(__name__, f'Received command user-set {atr} {ctx}:{member}') if member is None or not isinstance(member, discord.Member): @@ -190,14 +219,8 @@ class UserGroup(DiscordCommandABC): user = self._users.find_user_by_discord_id_and_server_id(member.id, server.server_id) if atr == 'xp': - if not value.isnumeric(): - await self._message_service.send_interaction_msg( - ctx.interaction, self._t.transform('modules.base.user.set.error.value_type_not_numeric') - ) - return - try: - user.xp = int(value) + user.xp = value except TypeError as te: self._logger.error(__name__, f'String value couldn\'t be converted to int', te) await self._message_service.send_interaction_msg( @@ -227,6 +250,32 @@ class UserGroup(DiscordCommandABC): atr_list = [('xp', self._atr_dict['xp'])] return [app_commands.Choice(name=value, value=key) for key, value in atr_list] + @user.command() + @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_moderator() + async def add(self, ctx: Context, atr: str, value: int, member: discord.Member = None): + self._logger.debug(__name__, f'Received command user-add {atr}-={value} {ctx}:{member}') + await self._handle_atr_calc(ctx, atr, value, member) + + @add.autocomplete('atr') + async def set_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]: + atr_list = [('xp', self._atr_dict['xp'])] + return [app_commands.Choice(name=value, value=key) for key, value in atr_list] + + @user.command() + @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_moderator() + async def remove(self, ctx: Context, atr: str, value: int, member: discord.Member = None): + self._logger.debug(__name__, f'Received command user-remove {atr}-={value} {ctx}:{member}') + await self._handle_atr_calc(ctx, atr, value, member, is_remove=True) + + @remove.autocomplete('atr') + async def set_autocomplete(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]: + atr_list = [('xp', self._atr_dict['xp'])] + return [app_commands.Choice(name=value, value=key) for key, value in atr_list] + @user.command() @commands.guild_only() @CommandChecks.check_is_ready() @@ -246,10 +295,6 @@ class UserGroup(DiscordCommandABC): self._db.save_changes() await self._level.check_level(member) - elif atr == 'ontime': - self._user_joined_voice_channel.delete_user_joined_voice_channel_by_user_id(user.user_id) - self._db.save_changes() - else: await self._message_service.send_interaction_msg( ctx.interaction, diff --git a/kdb-bot/src/modules/base/events/base_on_voice_state_update_event.py b/kdb-bot/src/modules/base/events/base_on_voice_state_update_event.py index 73ec95ce..bec155fd 100644 --- a/kdb-bot/src/modules/base/events/base_on_voice_state_update_event.py +++ b/kdb-bot/src/modules/base/events/base_on_voice_state_update_event.py @@ -7,7 +7,7 @@ from cpl_core.database.context import DatabaseContextABC from cpl_core.logging import LoggerABC from cpl_discord.events import OnVoiceStateUpdateABC -from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC +from bot_core.abc.client_utils_abc import ClientUtilsABC from bot_core.helper.event_checks import EventChecks from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC @@ -33,7 +33,7 @@ class BaseOnVoiceStateUpdateEvent(OnVoiceStateUpdateABC): users: UserRepositoryABC, user_joins: UserJoinedServerRepositoryABC, user_joins_vc: UserJoinedVoiceChannelRepositoryABC, - client_utils: ClientUtilsServiceABC, + client_utils: ClientUtilsABC, db: DatabaseContextABC, ): OnVoiceStateUpdateABC.__init__(self)