diff options
author | makefu <github@syntax-fehler.de> | 2020-08-15 14:15:10 +0200 |
---|---|---|
committer | makefu <github@syntax-fehler.de> | 2020-08-15 14:15:39 +0200 |
commit | 6ca24980fd9aef34ace440ec683eed81a22ca6a8 (patch) | |
tree | 4f9745212acfbac85dd744b17fa01276577a9088 | |
parent | 797f2e07d27a3ebd03cc2f0ee22c5c79da06b6d1 (diff) |
release version
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | default.nix | 8 | ||||
-rw-r--r-- | kalauerbot/bot.py (renamed from kalauerbot.py) | 80 | ||||
-rw-r--r-- | nix/matrixbot.patch | 12 | ||||
-rw-r--r-- | nix/python-matrixbot.nix (renamed from python-matrixbot.nix) | 11 | ||||
-rw-r--r-- | nix/translate.patch | 17 |
6 files changed, 90 insertions, 42 deletions
diff --git a/README.md b/README.md new file mode 100644 index 0000000..54b5c22 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +Kalauerbot +========== + +Manage the list of kalauer königs diff --git a/default.nix b/default.nix index 5702f0a..c4564b6 100644 --- a/default.nix +++ b/default.nix @@ -1,10 +1,12 @@ with import <nixpkgs> {}; python3.pkgs.buildPythonPackage { - name = "env"; + name = "kalauerbot-env"; src = ./.; propagatedBuildInputs = with python3.pkgs;[ - (callPackage ./python-matrixbot.nix {}) - googletrans + (callPackage ./nix/python-matrixbot.nix {}) + (stdenv.lib.overrideDerivation googletrans (self: { + patches = [ ./nix/translate.patch ]; + })) ]; checkInputs = [ python3.pkgs.black ]; } diff --git a/kalauerbot.py b/kalauerbot/bot.py index 244456c..16dded0 100644 --- a/kalauerbot.py +++ b/kalauerbot/bot.py @@ -9,8 +9,8 @@ log = logging.getLogger("Kalauerbot") version = "1.0.0" class Kalauerbot(MatrixBot): - def __init__(self,host,display_name,token,user_id,dbpath = "kalauerdb.json",whitelist=None): - self.whitelist = whitelist + def __init__(self,host,display_name,token,user_id,dbpath = "kalauerdb.json",allowlist=None): + self.allowlist = allowlist self.dbpath=dbpath self.load_db() super().__init__(host,display_name,token,user_id) @@ -38,15 +38,15 @@ class Kalauerbot(MatrixBot): elif entry['active']: yield name,entry else: - log.debug(f"skipping {name} because user is inactive") + log.debug(f"skipping {name} because user is inactive") continue def process_event(self, room, event): # {'content': {'body': 'hello bot', 'msgtype': 'm.text'}, 'event_id': '$1597412611141040kQPEC:thales.citadel.team', 'origin_server_ts': 1597412611511, 'sender': '@frichter:thales.citadel.team', 'type': 'm.room.message', 'unsigned': {'age': 98}, 'room_id': '!eXmPERUmuMagsLVxar:thales.citadel.team'} if self.user_id == event['sender']: return # don't answer own message if event['type'] != 'm.room.message': return msg = event['content']['body'] - if self.whitelist and room.room_id not in self.whitelist: - log.debug("Ignoring message {msg} because {room.room_id} is not in whitelist") + if self.allowlist and room.room_id not in self.allowlist: + log.debug("Ignoring message {msg} because {room.room_id} is not in allowlist") say = partial(self.say,room) db = self.db chosen = db['__chosen__'] @@ -110,37 +110,43 @@ class Kalauerbot(MatrixBot): if chosen: 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") + say(f"Nobody was chosen to be the the kalauer könig, start the process with `!choose`") elif command in [ "!ok", "!approved", "!gut", "!passt", "!reicht" ]: - log.info("Increasing weight of all active persons") - for name,entity in self.active_db(): - db[name]['weight'] = db[name]['weight'] +1 - log.info("The kalauer könig gets his weight set to 0") - db[chosen]['weight'] = 0 - say(f"""{chosen}, your kalauer has been approved, your total number of approved kalauers is {c['total']} -{chosen}, please choose a new potential kalauer könig by typing in `!choose` or `!demand <name>` to force the choice""") - log.debug("Unsetting current chosen") - db["__chose__"] = None - self.save_db() + if not db['__chosen__']: + say("Nobody is nominated, cannot approve 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 + say(f"""Your kalauer has been approved, your total number of approved kalauers is {db[chosen]['total']}\n\n{chosen}, please choose a new potential kalauer könig by typing in `!choose` to let the kalauerbot choose or `!demand <name>` to force your choice""") + log.debug("Unsetting current chosen") + db["__chosen__"] = None + self.save_db() elif command in [ "!reject", "!zu-gut", "!zu-schlecht", "!französisch", "!french"]: - say(f"{chosen}, your kalauer privilege was revoked, nobody is chosen now. Choose another kalauer könig with !choose") - db["__chose__"] = None - self.save_db() + if not db['__chosen__']: + say("Nobody is nominated, cannot reject kalauer") + else: + say(f"{chosen}, your kalauer privilege was revoked, nobody is chosen now. Choose another kalauer könig with `!choose`") + db["__chosen__"] = None + self.save_db() elif command == "!demand": name = args.strip() if name in self.active_db(): say(f"The previous kalauer könig demands user {name} to be the next kalauer könig. The rules of the Kalauer are divine and shall be respected.") - db["__chose__"] = name + db["__choses__"] = name elif name in db: say(f"{name} is currently not active, cannot be chosen") else: say(f"{name} name does not exist in the database, add the user before demanding a kalauer from him") self.save_db() - pass elif command == "!list": - txt = "Kalauer list:\n" + txt = "Kalauer roster:\n\n" for name,entry in db.items(): - txt += "{name} - {entry['weight']} weight - {entry['total']} total{'(active)' if entry['active' else '(inactive)'}\n" + if name == '__chosen__': continue + txt += f"* {name} - {entry['weight']} weight - {entry['total']} total {'(active)' if entry['active'] else '(inactive)'}\n" say(txt) elif command == "!kill": name = args.strip() @@ -150,23 +156,29 @@ class Kalauerbot(MatrixBot): else: say(f"Cannot delete someone who was never in the kalauer db") self.save_db() - elif command == "!demand": - name = args.strip() elif command == "!kalauer": kalauer = "Wie nennt man einen dicken Vegetarier? Biotonne!" + #kalauer = "What do you call a fat vegetarian? Compost bin" translator = Translator() - ret = translator.translate(kalauer,src='de',dest=random.choice(list(LANGUAGES))) - say(ret.text) + lang = random.choice(list(LANGUAGES)) + ret = translator.translate(kalauer,src='de',dest=lang) + log.debug(ret) + say(f"Your emergency kalauer in {LANGUAGES[lang]}: {ret.text}" ) #needs a newer googletrans + if ret.pronunciation: + say(f"Pronounced: {ret.pronunciation}") else: log.debug(f"Receive message {msg}") -bot = Kalauerbot( - host = "ext01.citadel.team", - display_name = "Kalauerbot", - token = "", - user_id = "" -) +def main(): + bot = Kalauerbot( + host = "ext01.citadel.team", + display_name = "Kalauerbot", + token = os.environ['CITADEL_TOKEN'], + user_id = os.environ["CITADEL_ID"], + ) + bot.start() -bot.start() +if __name__ == "__main__": + main() diff --git a/nix/matrixbot.patch b/nix/matrixbot.patch new file mode 100644 index 0000000..2b9bbbe --- /dev/null +++ b/nix/matrixbot.patch @@ -0,0 +1,12 @@ +diff --git a/matrixbot/matrixbot.py b/matrixbot/matrixbot.py +index 8e5598c..d8c23d2 100644 +--- a/matrixbot/matrixbot.py ++++ b/matrixbot/matrixbot.py +@@ -51,7 +51,6 @@ class MatrixBot: + self.user = None + if self.token is not None and self.user_id is not None: + self.user = self.client.get_user(self.user_id) +- self.user.set_display_name(self.display_name) + self.init_rooms(self.client.rooms) + self.invite_listener = self.client.add_invite_listener(self.handle_invite) + self.cache = Cache(dbfile=cache_db) diff --git a/python-matrixbot.nix b/nix/python-matrixbot.nix index d134e03..7bc5aa7 100644 --- a/python-matrixbot.nix +++ b/nix/python-matrixbot.nix @@ -10,11 +10,12 @@ buildPythonPackage rec { version = "0.0.7"; CI_COMMIT_TAG = version; - src = ./python-matrixbot; - #src = fetchPypi { - # inherit pname version; - # sha256 = "9412981b14ff3ab7ffbb1bfc1691758113ab8d71f731b3093d8808c286b69c71"; - #}; + #src = ./python-matrixbot; + src = fetchPypi { + inherit pname version; + sha256 = "9412981b14ff3ab7ffbb1bfc1691758113ab8d71f731b3093d8808c286b69c71"; + }; + patches = [ ./matrixbot.patch ]; propagatedBuildInputs = [ markdown diff --git a/nix/translate.patch b/nix/translate.patch new file mode 100644 index 0000000..6ff8207 --- /dev/null +++ b/nix/translate.patch @@ -0,0 +1,17 @@ +diff --git a/googletrans/client.py b/googletrans/client.py +index 89c2237..c203b44 100644 +--- a/googletrans/client.py ++++ b/googletrans/client.py +@@ -190,6 +190,13 @@ class Translator(object): + pass + if not PY3 and isinstance(pron, unicode) and isinstance(origin, str): # pragma: nocover + origin = origin.decode('utf-8') ++ ++ if pron is None: ++ try: ++ pron = data[0][2][2] ++ except: # pragma: nocover ++ pass ++ + if dest in EXCLUDES and pron == origin: + pron = translated |