]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/CACommandModDelNowNotifyAuthor.py
modules/CACommandModDelNowNotifyAuthor.py
[pub/jan/mattermost-bot.git] / modules / CACommandModDelNowNotifyAuthor.py
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
4
5
6
7 from AbstractCommand import AbstractCommand
8 class CACommandModDelNowNotifyAuthor(AbstractCommand):
9     TRIGGER = "mod-del-now-notify-author"
10     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
11               "auto_complete_hint": "<permalink> <reason> ✉️",
12              }
13     CONFIG["auto_complete_desc"] = CONFIG["description"] = "Delete permalinked post immediately and notify author. Run in same channel. [CHANNEL_ADMIN]"
14
15
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.)
18
19         msg_text = data['text'].strip().split(" ", 1)
20
21         if len(msg_text) == 1:
22             request.respond_cmd_err("``/"+self.TRIGGER+"`` It seems like you did not supply any reason-text.")
23             return
24
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.")
27             return
28
29         try:
30             splitpath = msg_text[0].strip().strip("/").split("/")
31             if splitpath[4] == "pl":
32                 post = self.bot.api.get_post(splitpath[5])
33         except:
34             request.respond_cmd_err("``/"+self.TRIGGER+"`` The first parameter is not a valid post-permalink or the permalinked post has been deleted.")
35             return
36
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.")
39             return
40
41         # do actual action
42         self.bot.api.delete_post(post["id"])
43         channel = self.bot.api.create_dm_channel_with(post["user_id"])
44         self.bot.api.create_post(channel["id"], "Im sorry to inform you that your post in ``"+data["team_domain"]+"::"+data["channel_name"]+"`` has been deleted, for the following reason\n\n----\n\n"+msg_text[1]+"\n\n----\n\nYour post:\n```\n"+post["message"]+"\n```\n")
45         self.bot.debug_chan("``/"+self.TRIGGER+" "+data["text"].strip()+"`` used by ``@"+data["user_name"]+"`` in ``"+data["team_domain"]+"::"+data["channel_name"]+"``")
46         request.respond_cmd_temp("## :white_check_mark: Success! :)")