From 32d6b2ffed682464c7db42e68826b62ac7fdff5d Mon Sep 17 00:00:00 2001 From: Thorsten Date: Thu, 2 May 2024 11:25:36 +0200 Subject: [PATCH] hungry-mod --- src/distbot/bot/bot.py | 2 +- src/distbot/plugins/basic.py | 58 +++++++++++++++++++++++++++--------- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/distbot/bot/bot.py b/src/distbot/bot/bot.py index 61c4547..5b6aa2b 100644 --- a/src/distbot/bot/bot.py +++ b/src/distbot/bot/bot.py @@ -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) diff --git a/src/distbot/plugins/basic.py b/src/distbot/plugins/basic.py index c694643..613f886 100644 --- a/src/distbot/plugins/basic.py +++ b/src/distbot/plugins/basic.py @@ -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): -- 2.39.2