]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/CommandCourseIsMeh.py
config.py.example
[pub/jan/mattermost-bot.git] / modules / CommandCourseIsMeh.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 # This command relies on the priviledged DB-Cleaner maint script.
6
7
8 from inspect import cleandoc
9
10 import logging
11 logger = logging.getLogger(__name__)
12
13
14 # pylint: disable=wrong-import-position
15 from AbstractCommand import AbstractCommand
16 class CommandCourseIsMeh(AbstractCommand):
17     TRIGGER = "course-is-meh"
18     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
19               "auto_complete_hint": "[<reason>]",
20              }
21     CONFIG["auto_complete_desc"] = CONFIG["description"] = AbstractCommand.ICON_DM+"Use in a course channel. Increments the 'course is meh atm' counter. The optional reason is sent to fsinf to look into."
22
23
24     def __init__(self, team_id, datadir):
25         super().__init__(team_id)
26         self.datadir = datadir
27         self.course_stats = {}
28
29
30     def on_POST(self, request, data):
31         msg_text = data['text'].strip()
32         chan = self.bot.api.get_channel(data["channel_id"])
33         import pprint
34         pprint.pprint(chan)
35
36         if chan["name"] in ["town-square", "off-topic"] or chan["display_name"][0] == "=" or chan["type"] != "O":
37             request.respond_cmd_err("``/"+self.TRIGGER+"`` must be used in a course channel.")
38             return
39
40         if len(msg_text) > 5000:
41             request.respond_cmd_err("``/"+self.TRIGGER+"`` failed: reason-text must be smaller than 5000 characters :(\nYour text is: "+str(len(msg_text[1]))+" characters long.")
42             return
43
44         # do actual action
45         self.meh_courses_inc(data["channel_name"])
46         if len(msg_text) > 0:
47             self.bot.api.create_post("cmr1s9d6678e7y94iw3jgwte3c", "``AUTODELETE-DAY`` ``/"+self.TRIGGER+"`` used by ``@"+data["user_name"]+"`` in ``"+data["team_domain"]+"::"+data["channel_name"]+"``\n```\n"+data["text"].strip()+"\n```")
48
49         request.respond_cmd_temp("## :white_check_mark: Success! :)")
50
51
52     def on_shutdown(self):
53         self.meh_courses_dumpstats()
54
55     def on_SIGUSR1(self, sigusr1_cnt):
56         self.meh_courses_dumpstats()
57
58
59     def meh_courses_inc(self, course, amount=1):
60         if course in self.course_stats:
61             self.course_stats[course] += amount
62         else:
63             self.course_stats[course] = amount
64
65
66     def meh_courses_dumpstats(self):
67         stats = self.course_stats.copy()
68         self.course_stats = {}
69         self.bot.dump_stats_json(stats, self.datadir+"course_stats-"+self.TEAM_ID+".json", cleandoc("""
70             This week's ``/course-is-meh`` -stats #mmstats.
71             Use this command in a course channel to increment the counter if the course is currently suboptimal for you. This command produces no output in that channel.
72             It doesnt really matter if its because you forgot your laptop-charger or the course just changed its modalities and you turned negative without any way to become positive again.
73             If you supply a reason it will be forwarded to fsinf to take a look at. (We want to know when you forgot your laptop chargers! XD )
74
75             |course|meh-ness|
76             |---|---:|
77         """))