diff options
| author | 2020-09-22 20:05:04 +0200 | |
|---|---|---|
| committer | 2020-09-22 20:05:04 +0200 | |
| commit | dfce7e85d747dd66e41294d0dc2c2b0b52d82370 (patch) | |
| tree | c23e6e3ed26fec49bb3f68d07c99e2870d90a898 | |
| parent | Merge pull request #1163 from python-discord/sebastiaan/features/use-async-re... (diff) | |
| parent | Merge branch 'master' into feat/latency (diff) | |
Merge pull request #1168 from python-discord/feat/latency
Latency cog for checking ping.
Diffstat (limited to '')
| -rw-r--r-- | Pipfile | 1 | ||||
| -rw-r--r-- | Pipfile.lock | 10 | ||||
| -rw-r--r-- | bot/exts/utils/ping.py | 59 | 
3 files changed, 69 insertions, 1 deletions
| @@ -7,6 +7,7 @@ name = "pypi"  aio-pika = "~=6.1"  aiodns = "~=2.0"  aiohttp = "~=3.5" +aioping = "~=0.3.1"  aioredis = "~=1.3.1"  "async-rediscache[fakeredis]" = "~=0.1.2"  beautifulsoup4 = "~=4.9" diff --git a/Pipfile.lock b/Pipfile.lock index 97436413d..f75852081 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@  {      "_meta": {          "hash": { -            "sha256": "63eec3557c8bfd42191cb5a7525d6e298471c16863adff485d917e08b72cd787" +            "sha256": "644012a1c3fa3e3a30f8b8f8e672c468dfaa155d9e43d26e2be8713c8dc5ebb3"          },          "pipfile-spec": 6,          "requires": { @@ -50,6 +50,14 @@              "index": "pypi",              "version": "==3.6.2"          }, +        "aioping": { +            "hashes": [ +                "sha256:8900ef2f5a589ba0c12aaa9c2d586f5371820d468d21b374ddb47ef5fc8f297c", +                "sha256:f983d86acab3a04c322731ce88d42c55d04d2842565fc8532fe10c838abfd275" +            ], +            "index": "pypi", +            "version": "==0.3.1" +        },          "aioredis": {              "hashes": [                  "sha256:15f8af30b044c771aee6787e5ec24694c048184c7b9e54c3b60c750a4b93273a", diff --git a/bot/exts/utils/ping.py b/bot/exts/utils/ping.py new file mode 100644 index 000000000..a9ca3dbeb --- /dev/null +++ b/bot/exts/utils/ping.py @@ -0,0 +1,59 @@ +import socket +from datetime import datetime + +import aioping +from discord import Embed +from discord.ext import commands + +from bot.bot import Bot +from bot.constants import Channels, Emojis, STAFF_ROLES, URLs +from bot.decorators import in_whitelist + +DESCRIPTIONS = ( +    "Command processing time", +    "Python Discord website latency", +    "Discord API latency" +) +ROUND_LATENCY = 3 + + +class Latency(commands.Cog): +    """Getting the latency between the bot and websites.""" + +    def __init__(self, bot: Bot) -> None: +        self.bot = bot + +    @commands.command() +    @in_whitelist(channels=(Channels.bot_commands,), roles=STAFF_ROLES) +    async def ping(self, ctx: commands.Context) -> None: +        """ +        Gets different measures of latency within the bot. + +        Returns bot, Python Discord Site, Discord Protocol latency. +        """ +        # datetime.datetime objects do not have the "milliseconds" attribute. +        # It must be converted to seconds before converting to milliseconds. +        bot_ping = (datetime.utcnow() - ctx.message.created_at).total_seconds() / 1000 +        bot_ping = f"{bot_ping:.{ROUND_LATENCY}f} ms" + +        try: +            delay = await aioping.ping(URLs.site, family=socket.AddressFamily.AF_INET) * 1000 +            site_ping = f"{delay:.{ROUND_LATENCY}f} ms" + +        except TimeoutError: +            site_ping = f"{Emojis.cross_mark} Connection timed out." + +        # Discord Protocol latency return value is in seconds, must be multiplied by 1000 to get milliseconds. +        discord_ping = f"{self.bot.latency * 1000:.{ROUND_LATENCY}f} ms" + +        embed = Embed(title="Pong!") + +        for desc, latency in zip(DESCRIPTIONS, [bot_ping, site_ping, discord_ping]): +            embed.add_field(name=desc, value=latency, inline=False) + +        await ctx.send(embed=embed) + + +def setup(bot: Bot) -> None: +    """Load the Latency cog.""" +    bot.add_cog(Latency(bot)) | 
