]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/CommandCourseIsMeh.py
modules/CommandGWGRoll.py
[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"] = "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 = dict()
28
29
30     def on_POST(self, request, data):
31         msg_text = data['text'].strip()
32         c = self.bot.api.get_channel(data["channel_id"])
33         import pprint
34         pprint.pprint(c)
35
36         if c["name"] in ["town-square", "off-topic"] or c["display_name"][0] == "=" or c["type"] != "O":
37             request.cmd_respond_text_temp("``/"+self.TRIGGER+"`` failed: Command must be used in a course channel.")
38             return
39
40         if len(msg_text) > 5000:
41             request.cmd_respond_text_temp("``/"+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         #self.bot.debug_chan("``/"+self.TRIGGER+" "+data["text"].strip()+"`` used by ``@"+data["user_name"]+"`` in ``"+data["team_domain"]+"::"+data["channel_name"]+"``")
50         request.cmd_respond_text_temp("### ``Done.`` :)")
51
52
53     def on_shutdown(self):
54         self.meh_courses_dumpstats()
55
56     def on_SIGUSR1(self, sigusr1_cnt):
57         self.meh_courses_dumpstats()
58
59
60     def meh_courses_inc(self, course, amount=1):
61         if course in self.course_stats:
62             self.course_stats[course] += amount
63         else:
64             self.course_stats[course] = amount
65
66
67     def meh_courses_dumpstats(self):
68         stats = self.course_stats.copy()
69         self.course_stats = dict()
70         self.bot.dump_stats_json(stats, self.datadir+"course_stats-"+self.TEAM_ID+".json", cleandoc("""
71             This week's ``/course-is-meh`` -stats #mmstats.
72             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.
73             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.
74             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 )
75
76             |course|meh-ness|
77             |---|---:|
78         """))