Pizza Bot initial zeug

This commit is contained in:
printfuck 2024-10-11 22:17:30 +02:00
parent 55ea03df06
commit a18a971ff2
4 changed files with 73 additions and 8 deletions

View file

@ -4,3 +4,5 @@ A simple [maubot](https://github.com/maubot/maubot) that echoes pings and other
## Usage ## Usage
* `!ping` - Reply with "Pong!" and the time it took for the message to reach the bot. * `!ping` - Reply with "Pong!" and the time it took for the message to reach the bot.
* `!echo <message>` - Reply with the given message * `!echo <message>` - Reply with the given message
* `!pizza <order>` - Add Pizza order
* `!order` - Show all orders within the last 4h

65
echo.py
View file

@ -6,9 +6,13 @@ from mautrix.types import TextMessageEventContent, MessageType, Format, RelatesT
from maubot import Plugin, MessageEvent from maubot import Plugin, MessageEvent
from maubot.handlers import command from maubot.handlers import command
import sqlite3
class PizzaBot(Plugin):
def __init__(self):
create_db()
class EchoBot(Plugin):
@staticmethod @staticmethod
def plural(num: float, unit: str, decimals: Optional[int] = None) -> str: def plural(num: float, unit: str, decimals: Optional[int] = None) -> str:
num = round(num, decimals) num = round(num, decimals)
@ -17,6 +21,65 @@ class EchoBot(Plugin):
else: else:
return f"{num} {unit}s" 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 @classmethod
def prettify_diff(cls, diff: int) -> str: def prettify_diff(cls, diff: int) -> str:
if abs(diff) < 10 * 1_000: if abs(diff) < 10 * 1_000:

View file

@ -1,7 +0,0 @@
maubot: 0.1.0
id: xyz.maubot.echo
version: 1.4.0
license: MIT
modules:
- echo
main_class: EchoBot

7
pizzabot.yaml Normal file
View file

@ -0,0 +1,7 @@
maubot: 0.1.0
id: cc.eris.pizza
version: 1.0.0
license: aGPLv3
modules:
- pizza
main_class: PizzaBot