Fix homeserver ping

This commit is contained in:
Nexus 2024-08-19 19:02:45 +01:00
parent 775be84d26
commit 83e6f7108e

View file

@ -11,21 +11,23 @@ class LatencyModule(niobot.Module):
"""See the bot's latency.""" """See the bot's latency."""
latency = ctx.latency latency = ctx.latency
homeserver = homeserver or await niobot.resolve_homeserver(ctx.message.sender.split(":")[1]) homeserver = homeserver or ctx.message.sender.split(":")[1]
if not homeserver.startswith("http"): if not homeserver.startswith("http"):
homeserver = f"https://{homeserver}" homeserver = f"https://{homeserver}"
parsed = urllib.parse.urlparse(homeserver) parsed = urllib.parse.urlparse(homeserver)
loc = "https://" + parsed.netloc loc = "https://" + parsed.netloc
homeserver = await niobot.resolve_homeserver(parsed.netloc)
async with httpx.AsyncClient(headers={"User-Agent": niobot.__user_agent__}) as client: async with httpx.AsyncClient(headers={"User-Agent": niobot.__user_agent__}) as client:
timings = [] timings = []
for rnd in range(5): for rnd in range(5):
start = time.time() start = time.time()
try: try:
res = await client.get(f"{loc}/_matrix/client/v1/ping") res = await client.get(f"{homeserver}/_matrix/client/v1/ping")
res.raise_for_status() res.raise_for_status()
except httpx.HTTPError: except httpx.HTTPError:
timings.append(-1) timings.append(-1)
else:
end = time.time() end = time.time()
timings.append(end - start) timings.append(end - start)
fed_latency = sum(timings) / len(timings) * 1000 fed_latency = sum(timings) / len(timings) * 1000