]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/CommandSpoilerAdv.py
modules/CommandSpoilerAdv.py
[pub/jan/mattermost-bot.git] / modules / CommandSpoilerAdv.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 CommandSpoilerAdv(AbstractCommand):
10     TRIGGER = "spoiler-adv"
11     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
12               "auto_complete_hint": "<post permalink> <spoiler-text>",
13              }
14     USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Attach/replace spoiler-text on any of your messages. (--remove to remove) Must be able to edit said message."
15
16
17     def on_POST(self, request, data):
18         msg_text = data['text'].strip().split(" ", 1)
19
20         if len(msg_text) == 1:
21             request.respond_cmd_err("``/"+self.TRIGGER+"`` It seems like you did not supply any spoiler-text. (use --remove to remove)")
22             return
23
24         if len(msg_text[1]) > 15000:
25             request.respond_cmd_err("``/"+self.TRIGGER+"`` reason-text must be smaller than 15000 characters :(\nYour text is: "+str(len(msg_text[1]))+" characters long.")
26             return
27
28         try:
29             splitpath = msg_text[0].strip().strip("/").split("/")
30             if splitpath[4] == "pl":
31                 post = self.bot.api.get_post(splitpath[5])
32         except:
33             request.respond_cmd_err("``/"+self.TRIGGER+"`` The first parameter is not a valid post-permalink or the permalinked post has been deleted.")
34             return
35
36         if post['user_id'] != data['user_id']:
37             request.respond_cmd_err("``/"+self.TRIGGER+"`` Is the permalinked post yours?")
38             return
39
40         if "cmd" in post["props"] and post["props"]["cmd"] != self.TRIGGER:
41             request.respond_cmd_err("``/"+self.TRIGGER+"`` The command **``/"+post["props"]["cmd"]+"``** already affects this post. Undo said command before applying this one.")
42             return
43
44         att = [{
45             "text": "This post contains spoilers.",
46             "actions": [{
47                 "name": ":ghost: show me the spoilers :ghost:",
48                 "integration": {"url": self.URL+"/interactive", "context": {"action": "show_spoiler", "spoiler":msg_text[1]}}
49                 }]
50             }]
51
52         post["props"].update(dict({"attachments":att}))
53
54         if msg_text[1] in ["--abort", "--undo", "--remove", "--clear"]:
55             del post["props"]["cmd"]
56             del post["props"]["attachments"]
57
58 #        self.bot.api.patch_post(splitpath[5], props=post["props"])
59         self.bot.api.update_post(splitpath[5], message=post["message"], is_pinned=post["is_pinned"], has_reactions=post["has_reactions"], props=post["props"])
60         request.respond_cmd_temp("## :white_check_mark: Success! :)")
61
62
63     def on_POST_interactive(self, request, data):
64         if data["context"]["action"] == "show_spoiler":
65             if data["context"]["spoiler"] != "":
66                 request.respond_interactive_temp(data["context"]["spoiler"])
67             else:
68                 request.respond_interactive_err("Post has no stored spoiler text")
69