]> git.aero2k.de Git - urlbot-v3.git/commitdiff
hungry-mod
authorThorsten <mail@aero2k.de>
Thu, 2 May 2024 09:25:36 +0000 (11:25 +0200)
committerThorsten <mail@aero2k.de>
Thu, 2 May 2024 09:25:36 +0000 (11:25 +0200)
src/distbot/bot/bot.py
src/distbot/plugins/basic.py

index 61c454774f6421819263dc313347a1b6ea4bf4b1..5b6aa2b3b22747cfded0bb9cd4e06f8162c557bf 100644 (file)
@@ -55,7 +55,7 @@ class Bot(slixmpp.ClientXMPP):
             pika.URLParameters(conf_get("amqp_uri")),
             on_open_callback=self.action_worker.open_connection,
             on_open_error_callback=lambda con, err: logger.error("Could not connect: %s", err),
-            on_close_callback=lambda con, e, x: logger.info("closing connection of actionworker (%s)", e)
+            on_close_callback=lambda con, e, x=None: logger.info("closing connection of actionworker (%s)", e)
         )
         logger.debug("connection state: %s", connection.connection_state)
 
index c6946430c0d3bc31869b244ca88cee0f1a3e3c47..613f88653ea1ad5fb579176470c2f7206b8c715e 100644 (file)
@@ -1,8 +1,8 @@
 # -*- coding: utf-8 -*-
+import json
 import logging
-import time
-
 import random
+import time
 
 from distbot.bot.worker import Worker
 from distbot.common import config
@@ -245,10 +245,34 @@ class Choose(Worker):
         (('Yes.', 'Yeah!', 'Ok!', 'Aye!', 'Great!'), 6),
         (('No.', 'Naah..', 'Meh.', 'Nay.', 'You stupid?'), 6),
         (('Maybe.', 'Dunno.', 'I don\'t care.'), 3),
-        (('Decision tree growing, check back in 20 years.', "OpSec, sorry"
+        (('Decision tree growing, check back in 20 years.', "OpSec, sorry",
           "Can't tell right now.", "I need more information.", "Sorry, did you say something?"), 1)
     )
 
+    with open("mampfdb.json", "r") as fd:
+        try:
+            mampfdb = json.load(fd)
+        except:
+            mampfdb = {}
+
+    def is_mampf(self, choice: str):
+        logger.debug(f"is {choice} mampf?")
+        if choice == "mampf":
+            return True
+        else:
+            choice = "nachtisch" if choice == "dessert" else choice
+            return choice.lower() in self.mampfdb
+
+    def choose_mampf(self, choice: str):
+        logger.debug(f"choosing mampf {choice}")
+        if choice == "mampf":
+            all_mampf = [item for entry in self.mampfdb.values() for item in entry]
+            logger.debug(f"all mampf: {all_mampf}")
+            return random.choice(all_mampf)
+        else:
+            choice = "nachtisch" if choice == "dessert" else choice
+            return random.choice(self.mampfdb.get(choice.lower()))
+
     @staticmethod
     def weighted_choice(choices):
         total = sum(w for c, w in choices)
@@ -289,18 +313,11 @@ class Choose(Worker):
         alternatives = words[1:]
         logger.debug("Alternatives: %s", str(alternatives))
 
-        # single or no choice
-        if len(alternatives) < 2:
-            return Action(msg='{}: {}'.format(sender, self.binary_choice(sudo=sudo)))
-
-        elif 'choose' not in alternatives:
-            choice = random.choice(alternatives)
-            return Action(msg='{}: I prefer {}!'.format(sender, choice))
-
-        def choose_between(options):
+        def choose_between(options: list[str]):
             responses = []
             current_choices = []
 
+            # TODO test that shit
             for item in options:
                 if item == 'choose':
                     if len(current_choices) < 2:
@@ -316,8 +333,21 @@ class Choose(Worker):
                 responses.append(random.choice(current_choices))
             return responses
 
-        logger.debug('sent multiple random choices')
-        return Action(msg='%s: My choices are: %s!' % (sender, ', '.join(choose_between(alternatives))))
+        # single or no choice
+        if len(alternatives) < 2:
+            if len(alternatives) == 1 and self.is_mampf(alternatives[0]):
+                mampf = self.choose_mampf(alternatives[0])
+                msg = [f"{sender}: {mampf}", f"{sender}: es gibt {mampf}", f"{sender}: du kriegst {mampf}",
+                       f"{sender} macht für alle {mampf}", f"{mampf} für {sender}, 230V für mich", "nix gibts!"]
+                return Action(msg=random.choice(msg))
+            else:
+                return Action(msg='{}: {}'.format(sender, self.binary_choice(sudo=sudo)))
+        elif 'choose' not in alternatives:
+            choice = random.choice(alternatives)
+            return Action(msg='{}: I prefer {}!'.format(sender, choice))
+        else:
+            logger.debug('sent multiple random choices')
+            return Action(msg='%s: My choices are: %s!' % (sender, ', '.join(choose_between(alternatives))))
 
 
 class Magic8Ball(Worker):