Improve latency feedback

This commit is contained in:
Nexus 2024-09-14 21:46:17 +01:00
parent 7a49169428
commit 0f1f6e8e1d
2 changed files with 718 additions and 654 deletions

1348
Pipfile.lock generated

File diff suppressed because it is too large Load diff

View file

@ -11,14 +11,24 @@ class LatencyModule(niobot.Module):
async def latency(self, ctx: niobot.Context, homeserver: str = None):
"""
See the bot's latency.
If you supply a homeserver, it will also show the federation latency to that homeserver.
"""
latency = ctx.latency
homeserver = homeserver or ctx.message.sender.split(":")[1]
if not homeserver.startswith("http"):
homeserver = f"https://{homeserver}"
response = await ctx.respond(
"Latency: {:,.2f}ms{}".format(
latency,
" (pinging homeserver...)"
if homeserver
else ''
)
)
if homeserver:
if not homeserver.startswith("http"):
homeserver = f"https://{homeserver}"
else:
return
parsed = urllib.parse.urlparse(homeserver)
loc = "https://" + parsed.netloc
@ -44,6 +54,12 @@ class LatencyModule(niobot.Module):
else:
end = time.perf_counter()
timings.append(end - start)
if sum(timings) == -5:
return await response.edit(
"Latency: {:,.2f}ms (failed to connect to homeserver)".format(
latency
)
)
fed_latency = sum(timings) / len(timings) * 1000
hs_target = homeserver[8:]