Source code for pinger_bot.ext.commands.ping

"""Module for the ``ping`` command."""
import re

import lightbulb
from hikari import embeds
from lightbulb import commands
from lightbulb.context import slash
from structlog import stdlib as structlog

from pinger_bot import bot, mc_api
from pinger_bot.config import gettext as _
from pinger_bot.ext import commands as pinger_commands

log = structlog.get_logger()

[docs]plugin = lightbulb.Plugin("ping")
""":class:`lightbulb.Plugin <lightbulb.plugins.Plugin>` object."""
[docs]async def get_fail_embed(ip: str) -> embeds.Embed: """Get the embed for when the ping fails. See source code for more information. Args: ip: The IP address of the server to reference in text. Returns: The embed where ping failed. """ embed = embeds.Embed(title=_("Ping Results {}").format(ip), color=(231, 76, 60)) embed.add_field( name=_("Can't ping the server."), value=_("Maybe you set invalid IP address, or server just offline.") ) return embed
[docs]async def clear_motd(motd: str) -> str: """Clear the :py:attr:`~pinger_bot.mc_api.MCServer.motd` from the non-readable characters. This removes ``&`` and ``§`` from the :py:attr:`~pinger_bot.mc_api.MCServer.motd` (plus next character). Args: motd: :py:attr:`MOTD <pinger_bot.mc_api.MCServer.motd>` of the server. Returns: Clear :py:attr:`~pinger_bot.mc_api.MCServer.motd`. """ motd_clean = re.sub(r"[\xA7|&][0-9A-FK-OR]", "", motd, flags=re.IGNORECASE) if motd_clean == "": motd_clean = _("No info.") return motd_clean
@plugin.command @lightbulb.option("ip", _("The IP address of the server."), type=str) @lightbulb.command("ping", _("Ping the server and give information about it."), pass_options=True) @lightbulb.implements(commands.SlashCommand)
[docs]async def ping(ctx: slash.SlashContext, ip: str) -> None: """Ping the server and give information about it. Args: ctx: The context of the command. ip: The IP address of the server. """ await pinger_commands.wait_please_message(ctx) server = await mc_api.MCServer.status(ip) if isinstance(server, mc_api.FailedMCServer): log.debug(_("Failed ping for {}").format(server.address.display_ip)) await ctx.respond(ctx.author.mention, embed=await get_fail_embed(server.address.display_ip), user_mentions=True) return embed = embeds.Embed( title=_("Ping Results {}").format(server.address.display_ip), description=_("Number IP: {}").format(server.address.num_ip), color=(46, 204, 113), ) embed.add_field(name=_("Latency"), value=str(f"{server.latency:.2f}") + "мс", inline=True) embed.add_field(name=_("Version"), value=server.version, inline=True) embed.add_field(name=_("Players"), value=str(server.players), inline=True) embed.add_field(name=_("MOTD"), value=await clear_motd(server.motd)) embed.set_thumbnail(server.icon) embed.set_footer( _("If you want to get link for editing the server MOTD, use this command: {}").format( f"'/motd {server.address.display_ip}'" ) ) log.debug(_(_("Ping successful for {}")).format(server.address.display_ip)) await ctx.respond(ctx.author.mention, embed=embed, user_mentions=True)
[docs]def load(bot_instance: bot.PingerBot) -> None: """Load the :py:data:`plugin`.""" bot_instance.add_plugin(plugin)