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"] = "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.cmd_respond_text_temp("``/"+self.TRIGGER+"`` failed: Command must be used in a course channel.")
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.")
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 #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.`` :)")
53 def on_shutdown(self):
54 self.meh_courses_dumpstats()
56 def on_SIGUSR1(self, sigusr1_cnt):
57 self.meh_courses_dumpstats()
60 def meh_courses_inc(self, course, amount=1):
61 if course in self.course_stats:
62 self.course_stats[course] += amount
64 self.course_stats[course] = amount
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 )