]> git.aero2k.de Git - urlbot-v3.git/commitdiff
add some machine learning
authorThorsten <mail@aero2k.de>
Fri, 23 Jun 2023 19:19:09 +0000 (21:19 +0200)
committerThorsten <mail@aero2k.de>
Fri, 23 Jun 2023 19:42:04 +0000 (21:42 +0200)
distbot/plugins/didyouknow.py

index 738dd12a5125f3d6e3f459dfa068f96404392b05..f3adbc52461a83ef81e65c281fef4cd4b631c99d 100644 (file)
@@ -1,4 +1,5 @@
 # -*- coding: utf-8 -*-
+import json
 import random
 
 from distbot.bot.worker import Worker
@@ -20,6 +21,10 @@ class DidYouKnow(Worker):
     description = "smart bot"
     usage = "wusstest ihr schon."
 
+    def __init__(self, actionqueue):
+        super().__init__(actionqueue)
+        self.dynamic_db = "didyouknow.json"
+
     answers = [
         "Nö. Aber: ",
         "Olle Kamelle - ",
@@ -29,7 +34,16 @@ class DidYouKnow(Worker):
     ]
 
     def parse_body(self, msg):
-        fact = random.choice(DB)
+        with open(self.dynamic_db, "r+") as fd:
+            try:
+                db = json.load(fd)
+            except Exception as e:
+                db = {"quotes": []}
+            fact = random.choice(DB + db["quotes"])
+            db["quotes"].append(msg["body"])
+
+            json.dump(db, fd, indent=2)
+
         answer = random.choice(self.answers)
         return Action(msg="{} {}".format(answer, fact))