From aa16847fbfa6e65a9ecdb89e9234d9d1955d5ce6 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 12 Feb 2023 17:22:58 +0100 Subject: [PATCH] Added minecraft register commands #181 --- kdb-bot/src/bot/translation/de.json | 7 ++ kdb-bot/src/bot_data/model/user.py | 4 + kdb-bot/src/modules/base/base_module.py | 4 + .../modules/base/command/register_group.py | 85 +++++++++++++++++++ .../modules/base/command/unregister_group.py | 64 ++++++++++++++ 5 files changed, 164 insertions(+) create mode 100644 kdb-bot/src/modules/base/command/register_group.py create mode 100644 kdb-bot/src/modules/base/command/unregister_group.py diff --git a/kdb-bot/src/bot/translation/de.json b/kdb-bot/src/bot/translation/de.json index 25c1301e..756ffb76 100644 --- a/kdb-bot/src/bot/translation/de.json +++ b/kdb-bot/src/bot/translation/de.json @@ -200,6 +200,13 @@ "error": { "atr_not_found": "Das Attribut {} konnte nicht gefunden werden :(" } + }, + "register": { + "success": "Spieler wurde mit dem Mitglied verlinkt :D", + "not_found": "Benutzer nicht gefunden!" + }, + "unregister": { + "success": "Verlinkung wurde entfernt :D" } }, "boot_log": { diff --git a/kdb-bot/src/bot_data/model/user.py b/kdb-bot/src/bot_data/model/user.py index 7674e78b..a2f4de81 100644 --- a/kdb-bot/src/bot_data/model/user.py +++ b/kdb-bot/src/bot_data/model/user.py @@ -48,6 +48,10 @@ class User(TableABC): def minecraft_id(self) -> Optional[str]: return self._minecraft_id + @minecraft_id.setter + def minecraft_id(self, value: str): + self._minecraft_id = value + @property def server(self) -> Optional[Server]: return self._server diff --git a/kdb-bot/src/modules/base/base_module.py b/kdb-bot/src/modules/base/base_module.py index 9377c6e9..3c470bbb 100644 --- a/kdb-bot/src/modules/base/base_module.py +++ b/kdb-bot/src/modules/base/base_module.py @@ -14,6 +14,8 @@ from modules.base.command.mass_move_command import MassMoveCommand from modules.base.command.ping_command import PingCommand from modules.base.command.presence_command import PresenceCommand from modules.base.command.purge_command import PurgeCommand +from modules.base.command.register_group import RegisterGroup +from modules.base.command.unregister_group import UnregisterGroup from modules.base.command.user_group import UserGroup from modules.base.events.base_on_command_error_event import BaseOnCommandErrorEvent from modules.base.events.base_on_command_event import BaseOnCommandEvent @@ -53,6 +55,8 @@ class BaseModule(ModuleABC): self._dc.add_command(PurgeCommand) self._dc.add_command(UserGroup) + self._dc.add_command(RegisterGroup) + self._dc.add_command(UnregisterGroup) # events self._dc.add_event(DiscordEventTypesEnum.on_command.value, BaseOnCommandEvent) self._dc.add_event(DiscordEventTypesEnum.on_command_error.value, BaseOnCommandErrorEvent) diff --git a/kdb-bot/src/modules/base/command/register_group.py b/kdb-bot/src/modules/base/command/register_group.py new file mode 100644 index 00000000..b47329cb --- /dev/null +++ b/kdb-bot/src/modules/base/command/register_group.py @@ -0,0 +1,85 @@ +import discord +import requests +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_abc import ClientUtilsABC +from bot_core.abc.message_service_abc import MessageServiceABC +from bot_core.helper.command_checks import CommandChecks +from bot_core.logging.command_logger import CommandLogger +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_repository_abc import UserRepositoryABC + + +class RegisterGroup(DiscordCommandABC): + def __init__( + self, + logger: CommandLogger, + message_service: MessageServiceABC, + bot: DiscordBotServiceABC, + client_utils: ClientUtilsABC, + servers: ServerRepositoryABC, + users: UserRepositoryABC, + db: DatabaseContextABC, + t: TranslatePipe, + ): + DiscordCommandABC.__init__(self) + + self._logger = logger + self._message_service = message_service + self._bot = bot + self._client_utils = client_utils + self._servers = servers + self._users = users + self._db = db + self._t = t + + self._logger.trace(__name__, f"Loaded command service: {type(self).__name__}") + + @commands.hybrid_group() + @commands.guild_only() + async def register(self, ctx: Context): + pass + + @register.command() + @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_moderator() + async def minecraft(self, ctx: Context, member: discord.Member, name: str): + self._logger.debug(__name__, f"Received command register minecraft {ctx}") + + minecraft_id = None + try: + self._logger.debug(__name__, f"Try to get minecraft id for {name}") + response = requests.get(url=f"https://api.mojang.com/users/profiles/minecraft/{name}") + if len(response.content) == 0: + await self._message_service.send_interaction_msg( + ctx.interaction, self._t.transform("modules.base.register.not_found") + ) + return + + minecraft_id = response.json()["id"] + except Exception as e: + self._logger.error(__name__, f"Get minecraft id for {name} failed", e) + await self._message_service.send_interaction_msg( + ctx.interaction, self._t.transform("modules.base.register.not_found") + ) + + if minecraft_id is None: + return + + server = self._servers.get_server_by_discord_id(ctx.guild.id) + user = self._users.get_user_by_discord_id_and_server_id(member.id, server.server_id) + user.minecraft_id = minecraft_id + self._users.update_user(user) + self._db.save_changes() + + await self._message_service.send_interaction_msg( + ctx.interaction, self._t.transform("modules.base.register.success") + ) + + self._logger.trace(__name__, f"Finished register minecraft command") diff --git a/kdb-bot/src/modules/base/command/unregister_group.py b/kdb-bot/src/modules/base/command/unregister_group.py new file mode 100644 index 00000000..c929f934 --- /dev/null +++ b/kdb-bot/src/modules/base/command/unregister_group.py @@ -0,0 +1,64 @@ +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_abc import ClientUtilsABC +from bot_core.abc.message_service_abc import MessageServiceABC +from bot_core.helper.command_checks import CommandChecks +from bot_core.logging.command_logger import CommandLogger +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_repository_abc import UserRepositoryABC + + +class UnregisterGroup(DiscordCommandABC): + def __init__( + self, + logger: CommandLogger, + message_service: MessageServiceABC, + bot: DiscordBotServiceABC, + client_utils: ClientUtilsABC, + servers: ServerRepositoryABC, + users: UserRepositoryABC, + db: DatabaseContextABC, + t: TranslatePipe, + ): + DiscordCommandABC.__init__(self) + + self._logger = logger + self._message_service = message_service + self._bot = bot + self._client_utils = client_utils + self._servers = servers + self._users = users + self._db = db + self._t = t + + self._logger.trace(__name__, f"Loaded command service: {type(self).__name__}") + + @commands.hybrid_group() + @commands.guild_only() + async def unregister(self, ctx: Context): + pass + + @unregister.command() + @commands.guild_only() + @CommandChecks.check_is_ready() + @CommandChecks.check_is_member_moderator() + async def minecraft(self, ctx: Context, member: discord.Member): + self._logger.debug(__name__, f"Received command register minecraft {ctx}") + + server = self._servers.get_server_by_discord_id(ctx.guild.id) + user = self._users.get_user_by_discord_id_and_server_id(member.id, server.server_id) + user.minecraft_id = None + self._users.update_user(user) + self._db.save_changes() + + await self._message_service.send_interaction_msg( + ctx.interaction, self._t.transform("modules.base.unregister.success") + ) + + self._logger.trace(__name__, f"Finished register minecraft command")