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
5 # This command relies on the priviledged DB-Cleaner maint script.
8 from inspect import cleandoc
11 logger = logging.getLogger(__name__)
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>]",
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."
24 def __init__(self, team_id, datadir):
25 super().__init__(team_id)
26 self.datadir = datadir
27 self.course_stats = dict()
30 def on_POST(self, request, data):
31 msg_text = data['text'].strip()
32 c = self.bot.api.get_channel(data["channel_id"])
36 if c["name"] in ["town-square", "off-topic"] or c["display_name"][0] == "=" or c["type"] != "O":
37 request.respond_cmd_err("``/"+self.TRIGGER+"`` must be used in a course channel.")
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.")
45 self.meh_courses_inc(data["channel_name"])
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```")
49 request.respond_cmd_temp("## :white_check_mark: Success! :)")
52 def on_shutdown(self):
53 self.meh_courses_dumpstats()
55 def on_SIGUSR1(self, sigusr1_cnt):
56 self.meh_courses_dumpstats()
59 def meh_courses_inc(self, course, amount=1):
60 if course in self.course_stats:
61 self.course_stats[course] += amount
63 self.course_stats[course] = amount
66 def meh_courses_dumpstats(self):
67 stats = self.course_stats.copy()
68 self.course_stats = dict()
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 )