From e481958bad02da2c7efa8f838abb6e2c247a1277 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 11 Jan 2023 20:13:09 +0100 Subject: [PATCH 1/6] Added logic to handle max message xp per hour #168 --- kdb-bot/src/bot_core/abc/client_utils_abc.py | 4 ++-- kdb-bot/src/bot_core/service/client_utils_service.py | 2 +- .../migration/user_message_count_per_hour_migration.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) 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 4a46ee26..deb70ed4 100644 --- a/kdb-bot/src/bot_core/abc/client_utils_abc.py +++ b/kdb-bot/src/bot_core/abc/client_utils_abc.py @@ -1,13 +1,13 @@ from abc import ABC, abstractmethod +from datetime import datetime from typing import Callable +import discord from cpl_query.extension import List from discord.ext.commands import Context from bot_data.model.user import User from modules.base.configuration.base_server_settings import BaseServerSettings -from bot_data.model.user import User - class ClientUtilsABC(ABC): 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 94adbaf3..f596a033 100644 --- a/kdb-bot/src/bot_core/service/client_utils_service.py +++ b/kdb-bot/src/bot_core/service/client_utils_service.py @@ -1,7 +1,7 @@ +from datetime import datetime from typing import Callable import discord -from bot_data.abc.user_joined_voice_channel_abc import UserJoinedVoiceChannelRepositoryABC from cpl_core.configuration import ConfigurationABC from cpl_core.database.context import DatabaseContextABC from cpl_core.logging import LoggerABC diff --git a/kdb-bot/src/bot_data/migration/user_message_count_per_hour_migration.py b/kdb-bot/src/bot_data/migration/user_message_count_per_hour_migration.py index 8913a4aa..ef8d0194 100644 --- a/kdb-bot/src/bot_data/migration/user_message_count_per_hour_migration.py +++ b/kdb-bot/src/bot_data/migration/user_message_count_per_hour_migration.py @@ -19,7 +19,7 @@ class UserMessageCountPerHourMigration(MigrationABC): str(f""" CREATE TABLE IF NOT EXISTS `UserMessageCountPerHour` ( `Id` BIGINT NOT NULL AUTO_INCREMENT, - `Date` DATETIME(6) NOT NULL, + `Date` VARCHAR(255) NOT NULL, `Hour` BIGINT, `XPCount` BIGINT, `UserId` BIGINT, From 5d3ceff3bfc942d63eb6af992ed0581fae349b1a Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 13 Jan 2023 12:24:58 +0100 Subject: [PATCH 2/6] Renamed remove command #179 --- kdb-bot/src/modules/base/command/user_group.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/kdb-bot/src/modules/base/command/user_group.py b/kdb-bot/src/modules/base/command/user_group.py index c38a7898..5b2ce3b3 100644 --- a/kdb-bot/src/modules/base/command/user_group.py +++ b/kdb-bot/src/modules/base/command/user_group.py @@ -231,8 +231,8 @@ class UserGroup(DiscordCommandABC): @commands.guild_only() @CommandChecks.check_is_ready() @CommandChecks.check_is_member_moderator() - async def remove(self, ctx: Context, atr: str, member: discord.Member = None): - self._logger.debug(__name__, f'Received command user-remove {atr} {ctx}:{member}') + async def reset(self, ctx: Context, atr: str, member: discord.Member = None): + self._logger.debug(__name__, f'Received command user-reset {atr} {ctx}:{member}') if member is None or not isinstance(member, discord.Member): member = ctx.author @@ -259,11 +259,11 @@ class UserGroup(DiscordCommandABC): await self._message_service.send_interaction_msg( ctx.interaction, - self._t.transform(f'modules.base.user.remove.{atr.lower()}').format(atr, member.mention) + self._t.transform(f'modules.base.user.reset.{atr.lower()}').format(atr, member.mention) ) - @remove.autocomplete('atr') - async def remove_autocomplete(self, interaction: discord.Interaction, current: str) -> List[ + @reset.autocomplete('atr') + async def reset_autocomplete(self, interaction: discord.Interaction, current: str) -> List[ app_commands.Choice[str] ]: return [app_commands.Choice(name=value, value=key) for key, value in self._atr_list] From 2401e58827145d6549b83b5a049105206aaef92a Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 13 Jan 2023 12:39:45 +0100 Subject: [PATCH 3/6] 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) From fecb82bf36d97e2c26d8f3843fb5ede4d8e7ebc8 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 13 Jan 2023 23:15:06 +0100 Subject: [PATCH 4/6] Refixed migration #179 --- .../bot_data/migration/user_message_count_per_hour_migration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kdb-bot/src/bot_data/migration/user_message_count_per_hour_migration.py b/kdb-bot/src/bot_data/migration/user_message_count_per_hour_migration.py index ef8d0194..8913a4aa 100644 --- a/kdb-bot/src/bot_data/migration/user_message_count_per_hour_migration.py +++ b/kdb-bot/src/bot_data/migration/user_message_count_per_hour_migration.py @@ -19,7 +19,7 @@ class UserMessageCountPerHourMigration(MigrationABC): str(f""" CREATE TABLE IF NOT EXISTS `UserMessageCountPerHour` ( `Id` BIGINT NOT NULL AUTO_INCREMENT, - `Date` VARCHAR(255) NOT NULL, + `Date` DATETIME(6) NOT NULL, `Hour` BIGINT, `XPCount` BIGINT, `UserId` BIGINT, From 0379c3ed9b401e47fa1fe72e28bc1c925d8315ee Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 13 Jan 2023 23:17:06 +0100 Subject: [PATCH 5/6] Removed unused imports #179 --- kdb-bot/src/bot_core/abc/client_utils_abc.py | 2 -- 1 file changed, 2 deletions(-) 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 50c934e0..8ec7453e 100644 --- a/kdb-bot/src/bot_core/abc/client_utils_abc.py +++ b/kdb-bot/src/bot_core/abc/client_utils_abc.py @@ -1,9 +1,7 @@ -import datetime from abc import ABC, abstractmethod from datetime import datetime from typing import Callable -import discord from cpl_query.extension import List from discord.ext.commands import Context From 460bb4202897ab0a64d2163e0d116cd9fcd7cef3 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 13 Jan 2023 23:26:38 +0100 Subject: [PATCH 6/6] Fixed /user reset command #179 --- kdb-bot/src/modules/base/command/user_group.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/kdb-bot/src/modules/base/command/user_group.py b/kdb-bot/src/modules/base/command/user_group.py index 9bb0db0a..e32f5600 100644 --- a/kdb-bot/src/modules/base/command/user_group.py +++ b/kdb-bot/src/modules/base/command/user_group.py @@ -159,8 +159,11 @@ class UserGroup(DiscordCommandABC): embed.add_field(name=self._t.transform('modules.base.user.atr.lefts'), value=lefts_string) if is_mod or member == ctx.author: - embed.add_field(name=self._t.transform('modules.base.user.atr.warnings'), - value=self._t.transform('common.not_implemented_yet'), inline=False) + embed.add_field( + name=self._t.transform('modules.base.user.atr.warnings'), + value=self._t.transform('common.not_implemented_yet'), + inline=False + ) # send to interaction because of sensitive data await self._message_service.send_interaction_msg(ctx.interaction, embed, wait_before_delete=wait) @@ -295,6 +298,10 @@ 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,