From 88e712d169c20c4366182c0d8d709f91a73b6d5b Mon Sep 17 00:00:00 2001 From: Thorsten Date: Sat, 9 Dec 2023 14:08:41 +0100 Subject: [PATCH] spread the knowledge --- README.md | 15 ++++++--------- distbot/common/message.py | 5 +++-- distbot/plugins/didyouknow.py | 30 ++++++++++++++++++++++++------ 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index f1d5978..5b7ac3b 100644 --- a/README.md +++ b/README.md @@ -68,15 +68,12 @@ The easiest (and probably most common) thing to achieve is to add another plugin Further extensions (and there's a lot to be desired): -* en-/disabling plugins programmatically - -* decide how plugin configuration should look like - centralised or not - -* decide whether ini is a good choice for configuration, and if runtime storage should use the same - -* rate limiting (hard nut, few moments where it's actually needed) - -* grep TODO in the repository... +- en-/disabling plugins programmatically +- decide how plugin configuration should look like - centralised or not +- decide whether ini is a good choice for configuration, and if runtime storage should use the same +- rate limiting (hard nut, few moments where it's actually needed) +- grep TODO in the repository... +- eval/use https://dramatiq.io/ for queueing tasks # License diff --git a/distbot/common/message.py b/distbot/common/message.py index d46b01a..eb30d26 100644 --- a/distbot/common/message.py +++ b/distbot/common/message.py @@ -3,6 +3,7 @@ import logging # Serializing import shlex +from typing import List import pika from distbot.common.config import conf_get @@ -12,11 +13,11 @@ from slixmpp.jid import JID logger = logging.getLogger(__name__) -def get_words(msg): +def get_words(msg) -> List[str]: return shlex.split(msg["body"]) -def get_nick_from_message(message_obj): +def get_nick_from_message(message_obj) -> str: """ Extract the actual nick :type message_obj: Message diff --git a/distbot/plugins/didyouknow.py b/distbot/plugins/didyouknow.py index ad3f285..f4ebf6b 100644 --- a/distbot/plugins/didyouknow.py +++ b/distbot/plugins/didyouknow.py @@ -2,6 +2,7 @@ import json import random +from distbot.common.message import get_nick_from_message, get_words from distbot.bot.worker import Worker from distbot.common.action import Action @@ -16,7 +17,7 @@ DB = [ class DidYouKnow(Worker): binding_keys = [ "#.wusstet.ihr.#", - "#.wußtet.ihr.#" + "Hi.#" ] description = "smart bot" usage = "wusstest ihr schon." @@ -34,18 +35,35 @@ class DidYouKnow(Worker): ] def parse_body(self, msg): - with open(self.dynamic_db, "r+") as fd: + sender = get_nick_from_message(msg) + words = get_words(msg) + if "wusstet" in words: + fact = self.get_random_fact() + self.insert_fact(msg) + answer = random.choice(self.answers) + return Action(msg="{} {}".format(answer, (fact[0].upper() + fact[1:]) if answer.endswith(".") else fact)) + elif words[0] == "Hi" and sender == "Q": + return Action(msg=self.get_random_fact()) + + + def get_random_fact(self): + with open(self.dynamic_db, "r") as fd: try: db = json.load(fd) except Exception as e: db = {"quotes": []} fact: str = random.choice(DB + db["quotes"]) + return fact + + + def insert_fact(self, msg): + with open(self.dynamic_db, "w") as fd: + try: + db = json.load(fd) + except Exception as e: + db = {"quotes": []} db["quotes"].append(msg["body"]) - fd.seek(0) json.dump(db, fd, indent=2) - answer = random.choice(self.answers) - return Action(msg="{} {}".format(answer, (fact[0].upper() + fact[1:]) if answer.endswith(".") else fact)) - ALL = [DidYouKnow] -- 2.39.2