]> 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]) > 15000:
21             request.cmd_respond_text_temp("``/spoiler-adv`` failed: Spoiler-text must be smaller than 15000 characters :(\nYour text is: "+str(len(msg_text[1]))+" characters long.")
22             return
23
24         try:
25             splitpath = msg_text[0].strip().strip("/").split("/")
26             if splitpath[4] == "pl":
27                 pl_post_id = splitpath[5]
28         except:
29             request.cmd_respond_text_temp("``/spoiler-adv`` failed: The first parameter is not a valid post permalink. :(")
30             return
31
32         if len(msg_text) == 1:
33             request.cmd_respond_text_temp("``/spoiler-adv`` failed: It seems like you did not supply any spoiler-text. (use --remove to remove a spoiler text) :(")
34             return
35
36         post = self.bot.api.get_post(pl_post_id)
37         if post['user_id'] != data['user_id']:
38             request.cmd_respond_text_temp("``/spoiler-adv`` failed: Is the permalinked post yours? :(")
39             return
40
41         att = [{
42             "text": "This post contains spoilers.",
43             "actions": [{
44                 "name": ":ghost: show me the spoilers :ghost:",
45                 "integration": {"url": self.URL+"/interactive", "context": {"action": "show_spoiler", "spoiler":msg_text[1]}}
46                 }]
47             }]
48
49         post["props"].update(dict({"attachments":att}))
50
51         if msg_text[1] == "--remove":
52             del post["props"]["attachments"]
53
54         self.bot.api.patch_post(splitpath[5], props=post["props"])
55         request.cmd_respond_text_temp("### ``Done.`` :)")
56
57
58     def on_POST_interactive(self, request, data):
59         if data["context"]["action"] == "show_spoiler":
60             request.respond(200, {"skip_slack_parsing":True, "ephemeral_text": data["context"]["spoiler"]})