From e26f529c1a449e7568b1eab99e525a0c22ca34b0 Mon Sep 17 00:00:00 2001 From: makefu Date: Thu, 18 Mar 2021 22:30:20 +0100 Subject: bot.py: add !oof --- kalauerbot/bot.py | 49 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/kalauerbot/bot.py b/kalauerbot/bot.py index 95aaac6..71e42d7 100644 --- a/kalauerbot/bot.py +++ b/kalauerbot/bot.py @@ -1,8 +1,10 @@ from matrixbot import MatrixBot from functools import partial import random +import mimetypes import json -import os +from os import listdir,environ +from os.path import isfile, join import logging from datetime import datetime from googletrans import LANGUAGES,Translator @@ -10,13 +12,20 @@ logging.basicConfig(level=logging.DEBUG) log = logging.getLogger("Kalauerbot") version = "1.0.0" + class Kalauerbot(MatrixBot): - def __init__(self,host,display_name,token,user_id,dbpath = "kalauerdb.json",allowlist=None): + def __init__(self,host,display_name,token,user_id,dbpath = + "kalauerdb.json",allowlist=None,oofdir="oof"): log.debug(f"dbpath: {dbpath}") log.debug(f"allowlist: {allowlist}") self.allowlist = allowlist self.dbpath=dbpath self.load_db() + try: + self.oofs = [ join(oofdir,f) for f in listdir(oofdir) if isfile(join(oofdir, f))] + except: + log.info(f"cannot load images from oofdir {oofdir}") + self.oofs = [] super().__init__(host,display_name,token,user_id) def load_db(self): @@ -141,6 +150,29 @@ class Kalauerbot(MatrixBot): say(f"{chosen} was chosen to be the next kalauer könig") else: say(f"Nobody was chosen to be the the kalauer könig, start the process with `!choose`") + elif command in [ "!oof","!ooof","!oooof"]: + if not db['__chosen__']: + say("Nobody is nominated, cannot oof kalauer") + else: + log.info("Increasing weight of all active persons") + for name in self.active_db(): + db[name]['weight'] += 1 + log.info("The kalauer könig gets his weight set to 0") + db[chosen]['weight'] = 0 + db[chosen]['total'] += 1 + if self.oofs: + oof = random.choice(self.oofs) + log.info(f"uploading {oof}") + mime = mimetypes.guess_type(oof)[0] + oofurl = self.client.upload(open(oof,'rb').read(),mime) + room.send_image(url=oofurl,name='oof') + else: + log.info("not uploading image because oofdir is empty") + db[chosen]['rejected'] = db[chosen].get('rejected',0) + 1 + say(f"""{chosen}, your kalauer just got oof'ed! Your total number of approved kalauers is {db[chosen]['total']} and rejected kalauers is {db[chosen]['rejected']}\n\nPlease choose a new potential kalauer könig by typing in `!choose` to let the kalauerbot choose or `!demand ` to force your choice\n\nDo not forget to record your kalauer with `kalauer: the funniest joke`. Someone else can also record the kalauer for you via `!record the funniest joke`""") + log.debug("Unsetting current chosen") + db["__chosen__"] = None + self.save_db() elif command in [ "!ok", "!approved", "!gut", "!passt", "!reicht" ]: if not db['__chosen__']: say("Nobody is nominated, cannot approve kalauer") @@ -246,16 +278,17 @@ class Kalauerbot(MatrixBot): log.debug(e) def main(): - allowlist = os.environ.get("KALAUER_ALLOWLIST",None) + allowlist = environ.get("KALAUER_ALLOWLIST",None) if allowlist: allowlist = allowlist.split(',') bot = Kalauerbot( - host = os.environ.get("MATRIX_SERVER","ext01.citadel.team"), + host = environ.get("MATRIX_SERVER","ext01.citadel.team"), display_name = "Kalauerbot", - token = os.environ['MATRIX_TOKEN'], - user_id = os.environ["MATRIX_ID"], - dbpath = os.environ.get("KALAUER_DBPATH",None) or "kalauerdb.json", - allowlist = allowlist + token = environ['MATRIX_TOKEN'], + user_id = environ["MATRIX_ID"], + dbpath = environ.get("KALAUER_DBPATH",None) or "kalauerdb.json", + allowlist = allowlist, + oofdir = environ.get("KALAUER_OOFDIR","oof") ) bot.start() -- cgit v1.2.3