From 442170eca957c533c7c7ecfb0c03d778d639d3f6 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 17 Nov 2022 16:16:28 +0100 Subject: [PATCH] Added pings to servers to ping command #117 --- kdb-bot/src/bot/config | 2 +- .../src/modules/base/command/ping_command.py | 33 +++++++++++++++++-- .../configuration/base_server_settings.py | 7 ++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/kdb-bot/src/bot/config b/kdb-bot/src/bot/config index bd8d3a5dad..58934dde3c 160000 --- a/kdb-bot/src/bot/config +++ b/kdb-bot/src/bot/config @@ -1 +1 @@ -Subproject commit bd8d3a5dad13e0fdcad79b767c032997b716b1a4 +Subproject commit 58934dde3ca8446212a0319088b8726527bd89ab diff --git a/kdb-bot/src/modules/base/command/ping_command.py b/kdb-bot/src/modules/base/command/ping_command.py index 9ccf1420af..7633d8a060 100644 --- a/kdb-bot/src/modules/base/command/ping_command.py +++ b/kdb-bot/src/modules/base/command/ping_command.py @@ -1,3 +1,4 @@ +import discord from cpl_discord.command import DiscordCommandABC from cpl_discord.service import DiscordBotServiceABC from cpl_translation import TranslatePipe @@ -8,6 +9,10 @@ from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC 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 modules.base.abc.base_helper_abc import BaseHelperABC +from modules.base.configuration.base_server_settings import BaseServerSettings +from modules.permission.abc.permission_service_abc import PermissionServiceABC class PingCommand(DiscordCommandABC): @@ -18,7 +23,10 @@ class PingCommand(DiscordCommandABC): message_service: MessageServiceABC, bot: DiscordBotServiceABC, client_utils: ClientUtilsServiceABC, - translate: TranslatePipe + translate: TranslatePipe, + permissions: PermissionServiceABC, + base_helper: BaseHelperABC, + servers: ServerRepositoryABC, ): DiscordCommandABC.__init__(self) @@ -27,13 +35,34 @@ class PingCommand(DiscordCommandABC): self._bot = bot self._client_utils = client_utils self._t = translate + self._permissions = permissions + self._base_helper = base_helper + self._servers = servers self._logger.trace(__name__, f'Loaded command service: {type(self).__name__}') + @staticmethod + def _get_ping(url: str) -> float: + from icmplib import ping + ping_result = ping(url, count=4, interval=0.2, privileged=False) + return ping_result.avg_rtt + @commands.hybrid_command() @commands.guild_only() @CommandChecks.check_is_ready() async def ping(self, ctx: Context): self._logger.debug(__name__, f'Received command ping {ctx}') - await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.base.pong')) + if self._permissions.is_member_technician(ctx.author): + embed = discord.Embed( + title=self._t.transform('modules.base.info.title'), + description=self._t.transform('modules.base.info.description'), + color=int('ef9d0d', 16) + ) + server = self._servers.get_server_by_discord_id(ctx.guild.id) + settings: BaseServerSettings = self._base_helper.get_config(server.discord_server_id) + for server in settings.ping_urls: + embed.add_field(name=server, value=f'{self._get_ping(server)} ms', inline=False) + await self._message_service.send_ctx_msg(ctx, embed) + else: + await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.base.pong')) self._logger.trace(__name__, f'Finished ping command') diff --git a/kdb-bot/src/modules/base/configuration/base_server_settings.py b/kdb-bot/src/modules/base/configuration/base_server_settings.py index 578839b984..cf7552ae56 100644 --- a/kdb-bot/src/modules/base/configuration/base_server_settings.py +++ b/kdb-bot/src/modules/base/configuration/base_server_settings.py @@ -18,6 +18,7 @@ class BaseServerSettings(ConfigurationModelABC): self._afk_command_channel_id: int = 0 self._help_command_reference_url: str = '' self._help_voice_channel_id: int = 0 + self._ping_urls = List(str) @property def id(self) -> int: @@ -51,6 +52,10 @@ class BaseServerSettings(ConfigurationModelABC): def help_voice_channel_id(self) -> int: return self._help_voice_channel_id + @property + def ping_urls(self) -> List[str]: + return self._ping_urls + def from_dict(self, settings: dict): try: self._id = int(settings['Id']) @@ -62,6 +67,8 @@ class BaseServerSettings(ConfigurationModelABC): self._afk_command_channel_id = settings['AFKCommandChannelId'] self._help_command_reference_url = settings['HelpCommandReferenceUrl'] self._help_voice_channel_id = settings['HelpVoiceChannelId'] + for url in settings['PingURLs']: + self._ping_urls.append(url) except Exception as e: Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {type(self).__name__} settings') Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')