From 75fc0c14ab15bd23e3d07a15fba45bae2489026b Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 2 Nov 2018 01:17:23 +0200 Subject: [PATCH] Initial commit --- .gitignore | 1 + LICENSE | 21 +++++++++++++++++++++ README.md | 2 ++ build.sh | 2 ++ echo.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ maubot.ini | 5 +++++ 6 files changed, 84 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100755 build.sh create mode 100644 echo.py create mode 100644 maubot.ini diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a60ef10 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.mbp diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..bfdfe68 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2018 Tulir Asokan + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..2a8a29d --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# echo +A simple [maubot](https://github.com/maubot/maubot) that echoes pings and other stuff. diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..ea4bb8b --- /dev/null +++ b/build.sh @@ -0,0 +1,2 @@ +#!/bin/bash +zip -9r echo.mbp echo.py maubot.ini diff --git a/echo.py b/echo.py new file mode 100644 index 0000000..73b6cf4 --- /dev/null +++ b/echo.py @@ -0,0 +1,53 @@ +from time import time + +from maubot import Plugin, CommandSpec, Command, Argument, MessageEvent + +COMMAND_PING = "ping" +ARG_ECHO = "$echo" +COMMAND_ECHO = f"echo {ARG_ECHO}" + + +class EchoBot(Plugin): + async def start(self) -> None: + self.set_command_spec(CommandSpec( + commands=[Command( + syntax=COMMAND_PING, + description="Ping the bot", + ), Command( + syntax=COMMAND_ECHO, + description="Echo something", + arguments={ + ARG_ECHO: Argument(matches=".+", required=True, + description="The content to echo"), + }, + )], + )) + self.client.add_command_handler(COMMAND_PING, self.ping_handler) + self.client.add_command_handler(COMMAND_ECHO, self.echo_handler) + + async def stop(self) -> None: + self.client.remove_command_handler(COMMAND_PING, self.ping_handler) + self.client.remove_command_handler(COMMAND_ECHO, self.echo_handler) + + @staticmethod + def time_since(ms: int) -> str: + diff = int(time() * 1000) - ms + if abs(diff) < 10 * 1_000: + return f"{diff} ms" + elif abs(diff) < 60 * 1_000: + return f"{round(diff / 1_000, 1)} seconds" + minutes, seconds = divmod(diff / 1_000, 60) + if abs(minutes) < 60: + return f"{minutes} minutes and {seconds} seconds" + hours, minutes = divmod(minutes, 60) + if abs(hours) < 24: + return f"{hours} hours, {minutes} minutes and {seconds} seconds" + days, hours = divmod(hours, 24) + return f"{days} days, {hours} hours, {minutes} minutes and {seconds} seconds" + + async def ping_handler(self, evt: MessageEvent) -> None: + await evt.reply(f"Pong! (ping took {self.time_since(evt.timestamp)} to arrive)") + + @staticmethod + async def echo_handler(evt: MessageEvent) -> None: + await evt.respond(evt.content.command.arguments[ARG_ECHO]) diff --git a/maubot.ini b/maubot.ini new file mode 100644 index 0000000..2dacfd8 --- /dev/null +++ b/maubot.ini @@ -0,0 +1,5 @@ +[maubot] +ID = xyz.maubot.echo +Version = 1.0.0 +Modules = echo +MainClass = EchoBot