0.3 - /ping pings to urls (#117) #120

Merged
edraft merged 1 commits from #117 into 0.3 2022-11-17 19:54:18 +01:00
3 changed files with 39 additions and 3 deletions
Showing only changes of commit 442170eca9 - Show all commits

@ -1 +1 @@
Subproject commit bd8d3a5dad13e0fdcad79b767c032997b716b1a4
Subproject commit 58934dde3ca8446212a0319088b8726527bd89ab

View File

@ -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')

View File

@ -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()}')