From 7ed30c40be6d8ae79dae7609848ba9598c7fa5d3 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 7 Nov 2022 19:04:41 +0100 Subject: [PATCH] Added list command #29 --- kdb-bot/src/bot/translation/de.json | 30 ++++--- kdb-bot/src/modules/level/command/__init__.py | 26 ++++++ .../src/modules/level/command/level_group.py | 89 +++++++++++++++++++ kdb-bot/src/modules/level/level_module.py | 5 ++ 4 files changed, 140 insertions(+), 10 deletions(-) create mode 100644 kdb-bot/src/modules/level/command/__init__.py create mode 100644 kdb-bot/src/modules/level/command/level_group.py diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index 447b232e..18549e58 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -150,7 +150,17 @@ "login_message": "Ich bin on the line :D\nDer Scheiß hat {} Sekunden gedauert" }, "level": { - "new_level_message": "<@{}> ist nun Level {}" + "new_level_message": "<@{}> ist nun Level {}", + "error": { + "nothing_found": "Keine auto-role Einträge gefunden." + }, + "list": { + "title": "Level:", + "description": "Konfigurierte Level:", + "name": "Name", + "min_xp": "Mindest XP", + "permission_int": "Berechtigungen" + } }, "database": {}, "permission": { @@ -166,15 +176,15 @@ "message": "Dies ist eine Test-Mail vom Krümmelmonster Web Interface\nGesendet von {}-{}" } }, - "auth": { - "confirmation": { - "subject": "E-Mail für {} {} bestätigen", - "message": "Öffne den Link um die E-Mail zu bestätigen:\n{}auth/register/{}" - }, - "forgot_password": { - "subject": "Passwort für {} {} zurücksetzen", - "message": "Öffne den Link um das Passwort zu ändern:\n{}auth/forgot-password/{}" - } + "auth": { + "confirmation": { + "subject": "E-Mail für {} {} bestätigen", + "message": "Öffne den Link um die E-Mail zu bestätigen:\n{}auth/register/{}" + }, + "forgot_password": { + "subject": "Passwort für {} {} zurücksetzen", + "message": "Öffne den Link um das Passwort zu ändern:\n{}auth/forgot-password/{}" } + } } } \ No newline at end of file diff --git a/kdb-bot/src/modules/level/command/__init__.py b/kdb-bot/src/modules/level/command/__init__.py new file mode 100644 index 00000000..c95179bf --- /dev/null +++ b/kdb-bot/src/modules/level/command/__init__.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- + +""" +bot Keksdose bot +~~~~~~~~~~~~~~~~~~~ + +Discord bot for the Keksdose discord Server + +:copyright: (c) 2022 sh-edraft.de +:license: MIT, see LICENSE for more details. + +""" + +__title__ = 'modules.level.command' +__author__ = 'Sven Heidemann' +__license__ = 'MIT' +__copyright__ = 'Copyright (c) 2022 sh-edraft.de' +__version__ = '0.3.dev29' + +from collections import namedtuple + + +# imports + +VersionInfo = namedtuple('VersionInfo', 'major minor micro') +version_info = VersionInfo(major='0', minor='3', micro='dev29') diff --git a/kdb-bot/src/modules/level/command/level_group.py b/kdb-bot/src/modules/level/command/level_group.py new file mode 100644 index 00000000..8de28aa4 --- /dev/null +++ b/kdb-bot/src/modules/level/command/level_group.py @@ -0,0 +1,89 @@ +import discord +from cpl_core.database.context import DatabaseContextABC +from cpl_discord.command import DiscordCommandABC +from cpl_discord.service import DiscordBotServiceABC +from cpl_translation import TranslatePipe +from discord.ext import commands +from discord.ext.commands import Context + +from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC +from bot_core.abc.message_service_abc import MessageServiceABC +from bot_core.logging.command_logger import CommandLogger +from bot_data.abc.level_repository_abc import LevelRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from modules.permission.abc.permission_service_abc import PermissionServiceABC + + +class LevelGroup(DiscordCommandABC): + + def __init__( + self, + logger: CommandLogger, + message_service: MessageServiceABC, + bot: DiscordBotServiceABC, + client_utils: ClientUtilsServiceABC, + permission_service: PermissionServiceABC, + translate: TranslatePipe, + db_context: DatabaseContextABC, + levels: LevelRepositoryABC, + servers: ServerRepositoryABC, + ): + DiscordCommandABC.__init__(self) + + self._logger = logger + self._message_service = message_service + self._bot = bot + self._client_utils = client_utils + self._permissions = permission_service + self._t = translate + self._db_context = db_context + self._levels = levels + self._servers = servers + + self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') + + @commands.hybrid_group() + @commands.guild_only() + async def level(self, ctx: Context): + pass + + @level.command(alias='levels') + @commands.guild_only() + async def list(self, ctx: Context, wait: int = None): + self._logger.debug(__name__, f'Received command level list {ctx}') + if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): + return + + if not self._permissions.is_member_moderator(ctx.author): + await self._message_service.send_ctx_msg(ctx, self._t.transform('common.no_permission_message')) + self._logger.trace(__name__, f'Finished command level list') + return + + if ctx.guild is None: + return + + server = self._servers.get_server_by_discord_id(ctx.guild.id) + levels = self._levels.get_levels_by_server_id(server.server_id) + if levels.count() < 1: + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.level.error.nothing_found')) + self._logger.trace(__name__, f'Finished command level list') + return + + level_name = '' + xp = '' + permissions = '' + for level in levels: + level_name += f'\n{level.name}' + xp += f'\n{level.min_xp}' + permissions += f'\n{level.permissions}' + + embed = discord.Embed( + title=self._t.transform('modules.level.list.title'), + description=self._t.transform('modules.level.list.description'), + color=int('ef9d0d', 16) + ) + embed.add_field(name=self._t.transform('modules.level.list.name'), value=level_name, inline=True) + embed.add_field(name=self._t.transform('modules.level.list.min_xp'), value=xp, inline=True) + embed.add_field(name=self._t.transform('modules.level.list.permission_int'), value=permissions, inline=True) + await self._message_service.send_ctx_msg(ctx, embed, wait_before_delete=wait) + self._logger.trace(__name__, f'Finished command level list') diff --git a/kdb-bot/src/modules/level/level_module.py b/kdb-bot/src/modules/level/level_module.py index 1943e73c..aeb57409 100644 --- a/kdb-bot/src/modules/level/level_module.py +++ b/kdb-bot/src/modules/level/level_module.py @@ -8,6 +8,7 @@ from cpl_discord.service.discord_collection_abc import DiscordCollectionABC from bot_core.abc.module_abc import ModuleABC from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum +from modules.level.command.level_group import LevelGroup from modules.level.events.level_on_message_event import LevelOnMessageEvent from modules.level.events.level_on_voice_state_update_event import LevelOnVoiceStateUpdateEvent from modules.level.level_seeder import LevelSeeder @@ -29,5 +30,9 @@ class LevelModule(ModuleABC): services.add_transient(LevelSeeder) services.add_transient(LevelService) + # commands + self._dc.add_command(LevelGroup) + + # events self._dc.add_event(DiscordEventTypesEnum.on_message.value, LevelOnMessageEvent) self._dc.add_event(DiscordEventTypesEnum.on_voice_state_update.value, LevelOnVoiceStateUpdateEvent)