1 # Mattermost Bot module.
2 # Copyright (c) 2016-2021 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
3 # published under MIT-License
7 from AbstractCommand import AbstractCommand
8 class CACommandModInform(AbstractCommand):
10 CONFIG = {"display_name": "somebot-command", "auto_complete": True,
11 "auto_complete_hint": "<permalink> <reason> 📢",
13 CONFIG["auto_complete_desc"] = CONFIG["description"] = "Add Mod-Info to permalinked post. Run in same channel. Use <reason> = '--clear' to remove Info. [CHANNEL_ADMIN]"
16 def on_POST(self, request, data):
17 self._require_channel_admin(data) # will throw an exception if not. (Dont try-except: Its handled up the stack.)
19 msg_text = data['text'].strip().split(" ", 1)
21 if len(msg_text) == 1:
22 request.respond_cmd_err("``/"+self.TRIGGER+"`` It seems like you did not supply any reason-text. (use --clear to remove the information)")
25 if len(msg_text[1]) > 15000:
26 request.respond_cmd_err("``/"+self.TRIGGER+"`` reason-text must be smaller than 15000 characters :(\nYour text is: "+str(len(msg_text[1]))+" characters long.")
30 splitpath = msg_text[0].strip().strip("/").split("/")
31 if splitpath[4] == "pl":
32 post = self.bot.api.get_post(splitpath[5])
34 request.respond_cmd_err("``/"+self.TRIGGER+"`` The first parameter is not a valid post-permalink or the permalinked post has been deleted.")
37 if post["channel_id"] != data["channel_id"]:
38 request.respond_cmd_err("``/"+self.TRIGGER+"`` Must be executed in the same channel as the permalinked post.")
41 if "cmd" in post["props"] and post["props"]["cmd"] != self.TRIGGER:
42 request.respond_cmd_err("``/"+self.TRIGGER+"`` The command **``/"+post["props"]["cmd"]+"``** already affects this post. Undo said command before applying this one.")
46 att = [{"color":"#74bbfb", "text": "**Mod-Information**\n"+msg_text[1]}]
47 post["props"].update(dict({"attachments":att, "cmd":self.TRIGGER}))
49 if msg_text[1] in ["--abort", "--undo", "--remove", "--clear"]:
50 del post["props"]["cmd"]
51 del post["props"]["attachments"]
53 self.bot.api.update_post(splitpath[5], message=post["message"], is_pinned=post["is_pinned"], has_reactions=post["has_reactions"], props=post["props"])
54 self.bot.debug_chan("``/"+self.TRIGGER+" "+data["text"].strip()+"`` used by ``@"+data["user_name"]+"`` in ``"+data["team_domain"]+"::"+data["channel_name"]+"``")
55 request.respond_cmd_temp("## :white_check_mark: Success! :)")