-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathecho.py
More file actions
67 lines (60 loc) · 2.78 KB
/
echo.py
File metadata and controls
67 lines (60 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from typing import Optional
from time import time
from html import escape
from mautrix.types import TextMessageEventContent, MessageType, Format, RelatesTo, RelationType
from maubot import Plugin, MessageEvent
from maubot.handlers import command
class EchoBot(Plugin):
@staticmethod
def plural(num: float, unit: str, decimals: Optional[int] = None) -> str:
num = round(num, decimals)
if num == 1:
return f"{num} {unit}"
else:
return f"{num} {unit}s"
@classmethod
def prettify_diff(cls, diff: int) -> str:
if abs(diff) < 10 * 1_000:
return f"{diff} ms"
elif abs(diff) < 60 * 1_000:
return cls.plural(diff / 1_000, 'second', decimals=1)
minutes, seconds = divmod(diff / 1_000, 60)
if abs(minutes) < 60:
return f"{cls.plural(minutes, 'minute')} and {cls.plural(seconds, 'second')}"
hours, minutes = divmod(minutes, 60)
if abs(hours) < 24:
return (f"{cls.plural(hours, 'hour')}, {cls.plural(minutes, 'minute')}"
f" and {cls.plural(seconds, 'second')}")
days, hours = divmod(hours, 24)
return (f"{cls.plural(days, 'day')}, {cls.plural(hours, 'hour')}, "
f"{cls.plural(minutes, 'minute')} and {cls.plural(seconds, 'second')}")
@command.new("ping", help="Ping")
@command.argument("message", pass_raw=True, required=False)
async def ping_handler(self, evt: MessageEvent, message: str = "") -> None:
diff = int(time() * 1000) - evt.timestamp
pretty_diff = self.prettify_diff(diff)
text_message = f'"{message[:20]}" took' if message else "took"
html_message = f'"{escape(message[:20])}" took' if message else "took"
content = TextMessageEventContent(
msgtype=MessageType.NOTICE, format=Format.HTML,
body=f"{evt.sender}: Pong! (ping {text_message} {pretty_diff} to arrive)",
formatted_body=f"<a href='https://matrix.to/#/{evt.sender}'>{evt.sender}</a>: Pong! "
f"(<a href='https://matrix.to/#/{evt.room_id}/{evt.event_id}'>ping</a> {html_message} "
f"{pretty_diff} to arrive)",
relates_to=RelatesTo(
rel_type=RelationType("xyz.maubot.pong"),
event_id=evt.event_id,
))
pong_from = evt.sender.split(":", 1)[1]
content.relates_to["from"] = pong_from
content.relates_to["ms"] = diff
content["pong"] = {
"ms": diff,
"from": pong_from,
"ping": evt.event_id,
}
await evt.respond(content)
@command.new("echo", help="Repeat a message")
@command.argument("message", pass_raw=True)
async def echo_handler(self, evt: MessageEvent, message: str) -> None:
await evt.respond(message)