From 362128007e2f92470c7daadbb81ab860db00a9a6 Mon Sep 17 00:00:00 2001 From: Someone Date: Sun, 9 Jan 2022 00:25:41 +0100 Subject: [PATCH] core/MMBot.py --- core/MMBot.py | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/core/MMBot.py b/core/MMBot.py index 8673be8..b0279cd 100644 --- a/core/MMBot.py +++ b/core/MMBot.py @@ -72,7 +72,6 @@ class MMBot(): threading.Thread.rename_current_thread = _rename_current_thread - # Core-Command: /use-data self.USETOPICS = {"bot":cleandoc("""##### Here I am, brain the size of a planet, and they ask me to fill in some missing MM-features and be fun ... It gives me a headache. Written by **``@someone``** in python3. Big thanks to contributors: **``@ju``**, **``@gittenburg``** @@ -91,7 +90,6 @@ class MMBot(): self.api.login(bearer=api_bearer) - # Register a module with the bot. def register(self, module): if module.TEAM_ID not in self.modules: @@ -105,7 +103,6 @@ class MMBot(): raise Exception("Multiple registration attempts for module: "+module.TRIGGER+" and team: "+module.TEAM_ID) - # Register a websocket handling module with the bot. # There is no useful way to discriminate WS-events by originating teams atm. :( def register_ws(self, module, eventlist): @@ -121,7 +118,6 @@ class MMBot(): raise Exception("Multiple registration attempts for module: "+module.NAME+" and evtype: "+evtype) - def start(self): logger.info("Starting: Almost there.") logger.info(pprint.pformat(self.modules)) @@ -134,7 +130,6 @@ class MMBot(): self.start_webserver() - def on_shutdown(self): logger.info("Shutting down ...") @@ -154,7 +149,6 @@ class MMBot(): logger.info("BYE.") - ######## # misc # ######## @@ -177,7 +171,6 @@ class MMBot(): self.wsmodules[evtype][module]._on_SIGUSR1(self.sigusr1_cnt) - def debug_chan(self, message): if self.debug_chan_id is None: logger.error("debug_chan() called, but debug_chan_id is unspecified.") @@ -185,7 +178,6 @@ class MMBot(): self.api.create_post(self.debug_chan_id, "``AUTODELETE-DAY``\n"+message) - def command_stats_inc(self, command, amount=1): if command in self.command_stats: self.command_stats[command] += amount @@ -193,13 +185,11 @@ class MMBot(): self.command_stats[command] = amount - def command_stats_dump(self): self.dump_stats_json(self.command_stats, "/tmp/somebot_command_stats.json", "#command_usage #mmstats") self.command_stats = {} - def dump_stats_json(self, stats_data, file_path, header="", footer="", no_data_text=""): do_write = False if stats_data: @@ -227,7 +217,6 @@ class MMBot(): json.dump({"header":header, "footer":footer, "no_data_text":no_data_text, "data":dict(sorted(stats_data.items()))}, file, ensure_ascii=False, indent=2) - ########################## # Bot's websocket client # ########################## @@ -243,7 +232,6 @@ class MMBot(): logger.error(exctxt) - ################### # Bot's webserver # ################### @@ -271,12 +259,10 @@ class MMBot(): self.respond(415) return - # store responseURL if "response_url" in data: self.responseURL = data["response_url"] - # handle call logger.debug("do_POST(): request incomming.") try: @@ -325,7 +311,6 @@ class MMBot(): self.respond(400, if_nonzero_secondary='ignore') - # Send a response to the channel. def respond_cmd_chan(self, message, props=None, att=None, http_code=200): data = {"skip_slack_parsing":True, "response_type":"in_channel", "text":message} @@ -339,7 +324,6 @@ class MMBot(): self.respond(http_code, data) - # Send a ephemeral response to the user. def respond_cmd_temp(self, message, props=None, att=None, http_code=200): data = {"skip_slack_parsing":True, "response_type":"ephemeral", "text":message} @@ -358,10 +342,19 @@ class MMBot(): self.respond(http_code, {"ephemeral_text":message}) + # Use to send a failure to the user. Use only the first time during a request. Should retain input on clientside. + def respond_cmd_ok(self, message, props=None): + data = {"skip_slack_parsing":True, "response_type":"ephemeral", "text": "## :x: Failure! :(\n#### "+message} + + if props: + data.update({"props": props}) + + self.respond(200, data + # Use to send a failure to the user. Use only the first time during a request. Should retain input on clientside. def respond_cmd_err(self, message, props=None): - data = {"skip_slack_parsing":True, "response_type":"ephemeral", "text": "## :x: Failure! :(\n### "+message} + data = {"skip_slack_parsing":True, "response_type":"ephemeral", "text": "## :x: Failure! :(\n#### "+message} if props: data.update({"props": props}) @@ -371,7 +364,6 @@ class MMBot(): self.respond(000, data) - def respond(self, http_code=200, data=None, if_nonzero_secondary='exc'): """ First response call must have a valid http code. @@ -396,7 +388,6 @@ class MMBot(): self.wfile.write(bytes(json.dumps(data), "utf8")) logger.debug("respond(): Primary response send.") - # Secondary responses else: if http_code != 0 and if_nonzero_secondary == "ignore": @@ -414,7 +405,6 @@ class MMBot(): if not self.responseURL: raise Exception("respond(): Secondary response attempt without response url.") - logger.debug("respond(): Secondary response. Using responseURL: %s", self.responseURL) req = Request(self.responseURL, data=bytes(json.dumps(data), "utf8"), method='POST') req.add_header("Content-Type", "application/json") @@ -422,7 +412,6 @@ class MMBot(): logger.debug("respond(): Secondary response send. Status: %s", conn.status) - class MyHTTPServer(ThreadingMixIn, HTTPServer): def serve_forever(self, bot): self.RequestHandlerClass.bot = bot -- 2.43.0