1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 21:44:21 +00:00
royalnet/royalpack/commands/ping.py

42 lines
1.1 KiB
Python
Raw Normal View History

2020-07-01 14:47:30 +00:00
import datetime
2020-06-30 22:11:14 +00:00
import asyncio
from typing import *
import royalnet
import royalnet.commands as rc
class PingCommand(rc.Command):
name: str = "ping"
description: str = "Display the status of the Herald network."
syntax: str = ""
2020-06-30 23:08:09 +00:00
_targets = ["telegram", "discord", "matrix", "constellation"]
2020-06-30 22:11:14 +00:00
async def run(self, args: rc.CommandArgs, data: rc.CommandData) -> None:
await data.reply("📶 Ping...")
2020-06-30 23:08:09 +00:00
tasks = {}
2020-06-30 22:11:14 +00:00
2020-07-01 14:47:30 +00:00
start = datetime.datetime.now()
2020-06-30 23:08:09 +00:00
for target in self._targets:
tasks[target] = self.loop.create_task(self.interface.call_herald_event(target, "pong"))
2020-06-30 22:11:14 +00:00
await asyncio.sleep(10)
lines = ["📶 [b]Pong![/b]", ""]
2020-06-30 23:08:09 +00:00
for name, task in tasks.items():
try:
2020-07-01 14:47:30 +00:00
d = task.result()
2020-06-30 23:08:09 +00:00
except (asyncio.CancelledError, asyncio.InvalidStateError):
lines.append(f"🔴 [c]{name}[/c]")
else:
2020-07-01 14:47:30 +00:00
end = datetime.datetime.fromtimestamp(d["timestamp"])
delta = end - start
lines.append(f"🔵 [c]{name}[/c] ({delta.microseconds // 1000} ms)")
2020-06-30 22:11:14 +00:00
await data.reply("\n".join(lines))