diff options
| author | 2021-05-09 13:42:29 -0700 | |
|---|---|---|
| committer | 2021-05-09 13:42:29 -0700 | |
| commit | 7c1415b62f599ba89afe385afb9176c3ff99f328 (patch) | |
| tree | c3233bfac93877105a0e0bf5b4ff1425cbbd1414 | |
| parent | Remove test for un-available function. (diff) | |
| parent | Merge pull request #1574 from python-discord/ping-bugs (diff) | |
Merge branch 'main' into annihilate_reddit
| -rw-r--r-- | bot/exts/utils/ping.py | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/bot/exts/utils/ping.py b/bot/exts/utils/ping.py index 572fc934b..750ff46d2 100644 --- a/bot/exts/utils/ping.py +++ b/bot/exts/utils/ping.py @@ -1,4 +1,5 @@ import socket +import urllib.parse from datetime import datetime import aioping @@ -34,11 +35,19 @@ class Latency(commands.Cog): # 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" + if bot_ping <= 0: + bot_ping = "Your clock is out of sync, could not calculate ping." + else: + 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" + url = urllib.parse.urlparse(URLs.site_schema + URLs.site).hostname + try: + delay = await aioping.ping(url, family=socket.AddressFamily.AF_INET) * 1000 + site_ping = f"{delay:.{ROUND_LATENCY}f} ms" + except OSError: + # Some machines do not have permission to run ping + site_ping = "Permission denied, could not ping." except TimeoutError: site_ping = f"{Emojis.cross_mark} Connection timed out." |