1 # Mattermost Bot module.
2 # Copyright (c) 2016-2022 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
3 # published under MIT-License
8 # pylint: disable=wrong-import-position
9 from AbstractWSHandler import AbstractWSHandler
10 class WSAlliterationAssociation(AbstractWSHandler):
11 NAME = "alliterationassociation"
13 def __init__(self, channel_ids):
16 self.channel_ids = channel_ids
19 def on_WS_EVENT(self, data):
20 post = json.loads(data["data"]["post"])
22 if post["channel_id"] not in self.channel_ids:
25 if post["user_id"] == self.bot.api._my_user_id:
28 message = re.sub(r"\s+", " ", re.sub(r"[^\w\.\:;\!\?~@\n ]+", " ", post["message"]))
30 sentences = [s.strip() for s in re.split(r"\.|\:|;|\!|\?|~|@|\n", message)]
31 for sentence in sentences:
32 words = sentence.lower().split()
34 if word[0] != words[0][0] and post["type"] in ["", "me"]:
35 if "root_id" not in post or post["root_id"] == "":
36 self.bot.api.create_post(post["channel_id"], "``"+sentence+"`` is not an alliteration! Kicking...", root_id=post["id"])
38 self.bot.api.create_post(post["channel_id"], "``"+sentence+"`` is not an alliteration! Kicking...", root_id=post["root_id"])
39 self.bot.api.remove_user_from_channel(post["channel_id"], post["user_id"])
43 # make user to channel admin, once they manage to say one alliteration.
44 self.bot.api.update_channel_members_scheme_roles(post["channel_id"], post["user_id"], {"scheme_user": True, "scheme_admin": True}, exc=False)