]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/WSAlliterationAssociation.py
core/MMBot.py
[pub/jan/mattermost-bot.git] / modules / WSAlliterationAssociation.py
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
4
5 import json
6 import re
7
8 from AbstractWSHandler import *
9 class WSAlliterationAssociation(AbstractWSHandler):
10     NAME = "alliterationassociation"
11
12     def __init__(self, channel_ids):
13         super().__init__()
14
15         self.channel_ids = channel_ids
16
17
18     def on_WS_EVENT(self, data):
19         post = json.loads(data["data"]["post"])
20
21         if post["channel_id"] not in self.channel_ids:
22             return False
23
24         if post["user_id"] == self.bot.api._my_user_id:
25             return False
26
27 #        if post["message"][0][0] in ["#", "`"]:
28 #            return True
29
30         message = re.sub(r"\s+", " ", re.sub(r"[^\w\.\:;\!\?~@\n ]+", " ", post["message"]))
31
32         sentences = [s.strip() for s in re.split(r"\.|\:|;|\!|\?|~|@|\n", message)]
33         for sentence in sentences:
34             words = sentence.lower().split()
35             for word in words:
36                 if word[0] != words[0][0] and post["type"] in ["", "me"]:
37                     if "root_id" not in post or post["root_id"] == "":
38                         self.bot.api.create_post(post["channel_id"], "``"+sentence+"`` is not an alliteration! Kicking...", root_id=post["id"])
39                     else:
40                         self.bot.api.create_post(post["channel_id"], "``"+sentence+"`` is not an alliteration! Kicking...", root_id=post["root_id"])
41                     self.bot.api.remove_user_from_channel(post["channel_id"], post["user_id"])
42                     return True
43
44
45         # make user to channel admin, once they manage to say one alliteration.
46         self.bot.api.update_channel_members_scheme_roles(post["channel_id"], post["user_id"], {"scheme_user": True, "scheme_admin": True})
47
48         return True