Commit f1573a0c authored by Tulir Asokan's avatar Tulir Asokan 🐈

Add deduplication patch for construct

parent e42a19df
from typing import Optional
from collections import deque
from typing import Optional, Deque
from time import time
from mautrix.types import TextMessageEventContent, MessageType
from mautrix.types import EventID, TextMessageEventContent, MessageType
from maubot import Plugin, MessageEvent
from maubot.handlers import command
DEDUP_SIZE_CAP: int = 5
class EchoBot(Plugin):
dedup: Deque[EventID]
async def start(self):
await super().start()
self.dedup = deque(maxlen=DEDUP_SIZE_CAP)
@staticmethod
def plural(num: float, unit: str, decimals: Optional[int] = None) -> str:
num = round(num, decimals)
......@@ -30,23 +39,27 @@ class EchoBot(Plugin):
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')}, "
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")
async def ping_handler(self, evt: MessageEvent) -> None:
diff = int(time() * 1000) - evt.timestamp
content = TextMessageEventContent(msgtype=MessageType.NOTICE,
body="Pong! (ping took "
f"{self.prettify_diff(diff)} to arrive)")
content["pong"] = {
"ms": diff,
"from": evt.sender.split(":", 1)[1],
"ping": evt.event_id,
}
await evt.reply(content)
if evt.event_id not in self.dedup:
self.dedup.append(evt.event_id)
content = TextMessageEventContent(msgtype=MessageType.NOTICE,
body="Pong! (ping took "
f"{self.prettify_diff(diff)} to arrive)")
content["pong"] = {
"ms": diff,
"from": evt.sender.split(":", 1)[1],
"ping": evt.event_id,
}
await evt.reply(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)
if evt.event_id not in self.dedup:
self.dedup.append(evt.event_id)
await evt.respond(message)
maubot: 0.1.0
id: xyz.maubot.echo
version: 1.1.0
version: 1.1.0+dedup
license: MIT
modules:
- echo
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment