From a18a971ff2c376ebf6aab11c9cc62c6e01fa5d81 Mon Sep 17 00:00:00 2001 From: printfuck Date: Fri, 11 Oct 2024 22:17:30 +0200 Subject: [PATCH] Pizza Bot initial zeug --- README.md | 2 ++ echo.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++- maubot.yaml | 7 ------ pizzabot.yaml | 7 ++++++ 4 files changed, 73 insertions(+), 8 deletions(-) delete mode 100644 maubot.yaml create mode 100644 pizzabot.yaml diff --git a/README.md b/README.md index 383b0f5..fe0883d 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,5 @@ A simple [maubot](https://github.com/maubot/maubot) that echoes pings and other ## Usage * `!ping` - Reply with "Pong!" and the time it took for the message to reach the bot. * `!echo ` - Reply with the given message +* `!pizza ` - Add Pizza order +* `!order` - Show all orders within the last 4h diff --git a/echo.py b/echo.py index 045129b..c71f428 100644 --- a/echo.py +++ b/echo.py @@ -6,9 +6,13 @@ from mautrix.types import TextMessageEventContent, MessageType, Format, RelatesT from maubot import Plugin, MessageEvent from maubot.handlers import command +import sqlite3 +class PizzaBot(Plugin): + + def __init__(self): + create_db() -class EchoBot(Plugin): @staticmethod def plural(num: float, unit: str, decimals: Optional[int] = None) -> str: num = round(num, decimals) @@ -17,6 +21,65 @@ class EchoBot(Plugin): else: return f"{num} {unit}s" + @classmethod + def create_db () -> None: + s = sqlite3.connect("pizza.db") + c = s.cursor() + c.execute(''' + CREATE TABLE if not exists pizza_table( + id INTEGER PRIMARY KEY AUTOINCREMENT, + order text NOT NULL, + owner text NOT NULL, + count integer DEFAULT 1, + ts integer NOT NULL + );''') + s.commit() + + @classmethod + def ms(): + return round(time.time()*1000) + + @classmethod + def insert(owner, text) -> None: + s = sqlite3.connect("pizza.db") + c = s.cursor() + c.execute("INSERT INTO pizza_table (order,owner,ts) VALUES (?,?,?)", (owner,text, ms())) + s.commit() + + @classmethod + def lookup(text) -> bool: + s = sqlite3.connect("pizza.db") + c = s.cursor() + c.execute("SELECT order FROM pizza_table where owner = ? ORDER BY ts DESC LIMIT 1;", (owner)) + r = c.fetchone() + return r is not None + + @classmethod + def lookup_order() -> str: + s = sqlite3.connect("pizza.db") + c = s.cursor() + c.execute("SELECT order FROM pizza_table where ts > ? ORDER BY ts DESC;", (ms() - 60*60*4)) + r = c.fetchall() + msg = "Saved orders within the last 4 hours:" + for entry in r: + msg += str(entry[0]) + "\n" + return msg + + + @command.new("pizza", help="Add a Pizza") + @command.argument("message", pass_raw=True, required=True) + async def ping_handler(self, evt: MessageEvent, message: str = "") -> None: + owner = evt.sender.split(":", 1)[1] + if not lookup(owner): + insert(owner, message) + + @command.new("order", help="Show complete order") + @command.argument("message", pass_raw=True, required=True) + async def ping_handler(self, evt: MessageEvent, message: str = "") -> None: + owner = evt.sender.split(":", 1)[1] + msg = lookup_order() + await evt.respond(msg) + @classmethod def prettify_diff(cls, diff: int) -> str: if abs(diff) < 10 * 1_000: diff --git a/maubot.yaml b/maubot.yaml deleted file mode 100644 index 4ea7c73..0000000 --- a/maubot.yaml +++ /dev/null @@ -1,7 +0,0 @@ -maubot: 0.1.0 -id: xyz.maubot.echo -version: 1.4.0 -license: MIT -modules: -- echo -main_class: EchoBot diff --git a/pizzabot.yaml b/pizzabot.yaml new file mode 100644 index 0000000..2c538be --- /dev/null +++ b/pizzabot.yaml @@ -0,0 +1,7 @@ +maubot: 0.1.0 +id: cc.eris.pizza +version: 1.0.0 +license: aGPLv3 +modules: +- pizza +main_class: PizzaBot