From 5c564d3a8b04c3022ef48efa85c2b47118bc9992 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 19 Jul 2022 12:03:13 +0200 Subject: [PATCH] Added shutdown command --- src/bot/startup_discord_extension.py | 2 + src/bot/translation/de.json | 3 +- src/modules/admin/command/restart_command.py | 2 +- src/modules/admin/command/shutdown_command.py | 51 +++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/modules/admin/command/shutdown_command.py diff --git a/src/bot/startup_discord_extension.py b/src/bot/startup_discord_extension.py index 25884bba61..6a02dd0fd3 100644 --- a/src/bot/startup_discord_extension.py +++ b/src/bot/startup_discord_extension.py @@ -6,6 +6,7 @@ from cpl_discord import get_discord_collection from cpl_discord.discord_event_types_enum import DiscordEventTypesEnum from modules.admin.command.restart_command import RestartCommand +from modules.admin.command.shutdown_command import ShutdownCommand from modules.base.command.afk_command import AFKCommand from modules.base.command.help_command import HelpCommand from modules.base.command.info_command import InfoCommand @@ -36,6 +37,7 @@ class StartupDiscordExtension(StartupExtensionABC): """ commands """ # admin dc.add_command(RestartCommand) + dc.add_command(ShutdownCommand) # moderator dc.add_command(PurgeCommand) # simple diff --git a/src/bot/translation/de.json b/src/bot/translation/de.json index dbf7880a02..cc847887ea 100644 --- a/src/bot/translation/de.json +++ b/src/bot/translation/de.json @@ -7,7 +7,8 @@ }, "modules": { "admin": { - "restart_message": "Bin gleich wieder da :D" + "restart_message": "Bin gleich wieder da :D", + "shutdown_message": "Trauert nicht um mich, es war eine logische Entscheidung. Das Wohl von Vielen, es wiegt schwerer als das Wohl von Wenigen oder eines Einzelnen. Ich war es und ich werde es immer sein, Euer Freund. Lebt lange und in Frieden :)" }, "moderator": {}, "base": { diff --git a/src/modules/admin/command/restart_command.py b/src/modules/admin/command/restart_command.py index fc7da711a2..d2290b955e 100644 --- a/src/modules/admin/command/restart_command.py +++ b/src/modules/admin/command/restart_command.py @@ -47,6 +47,6 @@ class RestartCommand(DiscordCommandABC): self._config.add_configuration('IS_RESTART', 'true') await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.admin.restart_message')) - await self._bot.logout() + await self._bot.stop_async() self._logger.trace(__name__, f'Finished restart command') diff --git a/src/modules/admin/command/shutdown_command.py b/src/modules/admin/command/shutdown_command.py new file mode 100644 index 0000000000..5f954127c7 --- /dev/null +++ b/src/modules/admin/command/shutdown_command.py @@ -0,0 +1,51 @@ +from cpl_core.configuration import ConfigurationABC +from cpl_core.logging import LoggerABC +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 modules.permission.abc.permission_service_abc import PermissionServiceABC + + +class ShutdownCommand(DiscordCommandABC): + + def __init__( + self, + logger: LoggerABC, + config: ConfigurationABC, + message_service: MessageServiceABC, + bot: DiscordBotServiceABC, + client_utils: ClientUtilsServiceABC, + translate: TranslatePipe, + permissions: PermissionServiceABC, + ): + DiscordCommandABC.__init__(self) + + self._logger = logger + self._config = config + self._message_service = message_service + self._bot = bot + self._client_utils = client_utils + self._t = translate + self._permissions = permissions + + self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') + + @commands.command() + async def shutdown(self, ctx: Context): + self._logger.debug(__name__, f'Received command shutdown {ctx}') + self._client_utils.received_command(ctx.guild.id) + + 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 shutdown command') + return + + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.admin.shutdown_message')) + await self._bot.stop_async() + + self._logger.trace(__name__, f'Finished shutdown command')