27 lines
1.2 KiB
Python
27 lines
1.2 KiB
Python
import asyncio,re
|
|
import aiohttp
|
|
from discord.ext import commands
|
|
URL="https://discordstatus.com/metrics-display/5k2rt9f7pmny/day.json"
|
|
async def local():
|
|
try:
|
|
p=await asyncio.create_subprocess_exec("ping","-c","1","-W","5","discord.com",stdout=asyncio.subprocess.PIPE,stderr=asyncio.subprocess.STDOUT)
|
|
out,_=await asyncio.wait_for(p.communicate(),8)
|
|
m=re.search(r"time[=<]\s*([\d.]+)\s*ms",out.decode(),re.I)
|
|
return round(float(m[1])) if m else None
|
|
except Exception:return None
|
|
async def glob():
|
|
try:
|
|
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10)) as h,h.get(URL) as r:
|
|
return int((await r.json(content_type=None))["summary"]["last"])
|
|
except Exception:return None
|
|
class ping(commands.Cog):
|
|
def __init__(s,bot):s.bot=bot
|
|
@commands.command()
|
|
async def ping(s,ctx):
|
|
l,g=await asyncio.gather(local(),glob())
|
|
f=lambda v:f"{v}ms" if v is not None else "n/a"
|
|
w=s.bot.latency
|
|
w=round(w*1000) if w==w and w!=float("inf") else None
|
|
await ctx.send(f"websocket ping: {f(w)}\nlocal discord ping: {f(l)}\nglobal discord ping: {f(g)}")
|
|
async def setup(bot):await bot.add_cog(ping(bot))
|