24 lines
870 B
Python
24 lines
870 B
Python
import niobot
|
|
import time
|
|
import httpx
|
|
|
|
|
|
class LatencyModule(niobot.Module):
|
|
@niobot.command("latency")
|
|
async def latency(self, ctx: niobot.Context):
|
|
"""See the bot's latency."""
|
|
latency = ctx.latency
|
|
|
|
homeserver = await niobot.resolve_homeserver(ctx.message.sender.split(":")[1])
|
|
async with httpx.AsyncClient(headers={"User-Agent": niobot.__user_agent__}) as client:
|
|
timings = []
|
|
for rnd in range(5):
|
|
start = time.time()
|
|
await client.get(f"{homeserver}/_matrix/client/v1/ping")
|
|
end = time.time()
|
|
timings.append(end - start)
|
|
fed_latency = sum(timings) / len(timings) * 1000
|
|
|
|
return await ctx.respond(
|
|
"Latency: {:,.2f}ms (federation latency to your homeserver: {:,.2f})".format(latency, fed_latency)
|
|
)
|