summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormakefu <github@syntax-fehler.de>2021-03-18 22:30:20 +0100
committermakefu <github@syntax-fehler.de>2021-03-18 22:30:20 +0100
commite26f529c1a449e7568b1eab99e525a0c22ca34b0 (patch)
tree55d5962dfe5d872979c50e800b07d8ebb3187d08
parent8b581534e256569c2f694d70ef84119acae1ac1e (diff)
bot.py: add !oof
-rw-r--r--kalauerbot/bot.py49
1 files 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 <name>` 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 <username> 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()