]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/CACommandModDelPostText.py
requirements.txt
[pub/jan/mattermost-bot.git] / modules / CACommandModDelPostText.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
6
7 # pylint: disable=wrong-import-position
8 from AbstractCommand import AbstractCommand
9 class CACommandModDelPostText(AbstractCommand):
10     TRIGGER = "mod-del-post-text"
11     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
12               "auto_complete_hint": "<permalink> <reason>",
13              }
14     CONFIG["auto_complete_desc"] = CONFIG["description"] = AbstractCommand.ICON_PUBLIC+"Remove post-text, but dont delete post. Run in same channel. Cannot be undone! [CHANNEL_ADMIN]"
15
16
17     def on_POST(self, request, data):
18         self._require_channel_admin(data) # will throw an exception if not. (Dont try-except: Its handled up the stack.)
19
20         msg_text = data['text'].strip().split(" ", 1)
21
22         if len(msg_text) == 1:
23             request.respond_cmd_err("``/"+self.TRIGGER+"`` It seems like you did not supply any reason-text.")
24             return
25
26         if len(msg_text[1]) > 15000:
27             request.respond_cmd_err("``/"+self.TRIGGER+"`` reason-text must be smaller than 15000 characters :(\nYour text is: "+str(len(msg_text[1]))+" characters long.")
28             return
29
30         try:
31             splitpath = msg_text[0].strip().strip("/").split("/")
32             if splitpath[4] == "pl":
33                 post = self.bot.api.get_post(splitpath[5])
34         except:
35             request.respond_cmd_err("``/"+self.TRIGGER+"`` The first parameter is not a valid post-permalink or the permalinked post has been deleted.")
36             return
37
38         if post["channel_id"] != data["channel_id"]:
39             request.respond_cmd_err("``/"+self.TRIGGER+"`` Must be executed in the same channel as the permalinked post.")
40             return
41
42         if "cmd" in post["props"] and post["props"]["cmd"] != self.TRIGGER:
43             request.respond_cmd_err("``/"+self.TRIGGER+"`` The command **``/"+post["props"]["cmd"]+"``** already affects this post. Undo said command before applying this one.")
44             return
45
46         if post["message"].strip() == "":
47             request.respond_cmd_err("``/"+self.TRIGGER+"`` doesnt work on empty messages and cant be undone.\nIf you are an admin_a you can use ``/mod-get-post-text`` to view the original text. :(")
48             return
49
50         # do actual action
51         att = [{"color":"#f35912", "text": "**Post-text removed. Reason:**\n"+msg_text[1]}]
52         post["props"].update(dict({"attachments":att, "somebot_removed_post_text":post["message"], "cmd":self.TRIGGER}))
53
54         self.bot.api.update_post(splitpath[5], message=None, is_pinned=post["is_pinned"], has_reactions=post["has_reactions"], props=post["props"])
55         self.bot.debug_chan("``/"+self.TRIGGER+" "+data["text"].strip()+"`` used by ``@"+data["user_name"]+"`` in ``"+data["team_domain"]+"::"+data["channel_name"]+"``")
56         request.respond_cmd_temp("## :white_check_mark: Success! :)")