From 937901cb5f077e53f6f323e1a17080887f385c7a Mon Sep 17 00:00:00 2001 From: Someone Date: Wed, 8 Dec 2021 19:38:38 +0100 Subject: [PATCH] [somebot] WS:AlliterationAssociation --- modules/WSAlliterationAssociation.py | 48 ++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 modules/WSAlliterationAssociation.py diff --git a/modules/WSAlliterationAssociation.py b/modules/WSAlliterationAssociation.py new file mode 100644 index 0000000..b7a23e1 --- /dev/null +++ b/modules/WSAlliterationAssociation.py @@ -0,0 +1,48 @@ +# Mattermost Bot module. +# Copyright (c) 2016-2021 by Someone (aka. Jan Vales ) +# published under MIT-License + +import json +import re + +from AbstractWSHandler import * +class WSAlliterationAssociation(AbstractWSHandler): + NAME = "alliterationassociation" + + def __init__(self, channel_ids): + super().__init__() + + self.channel_ids = channel_ids + + + def on_WS_EVENT(self, data): + post = json.loads(data["data"]["post"]) + + if post["channel_id"] not in self.channel_ids: + return False + + if post["user_id"] == self.bot.api._my_user_id: + return False + +# if post["message"][0][0] in ["#", "`"]: +# return True + + message = re.sub(r"\s+", " ", re.sub(r"[^\w\.\:;\!\?~@\n ]+", " ", post["message"])) + + sentences = [s.strip() for s in re.split(r"\.|\:|;|\!|\?|~|@|\n", message)] + for sentence in sentences: + words = sentence.lower().split() + for word in words: + if word[0] != words[0][0] and post["type"] in ["", "me"]: + if "root_id" not in post or post["root_id"] == "": + self.bot.api.create_post(post["channel_id"], "``"+sentence+"`` is not an alliteration! Kicking...", root_id=post["id"]) + else: + self.bot.api.create_post(post["channel_id"], "``"+sentence+"`` is not an alliteration! Kicking...", root_id=post["root_id"]) + self.bot.api.remove_user_from_channel(post["channel_id"], post["user_id"]) + return True + + + # make user to channel admin, once they manage to say one alliteration. + self.bot.api.update_channel_members_scheme_roles(post["channel_id"], post["user_id"], {"scheme_user": True, "scheme_admin": True}) + + return True -- 2.43.0