From 1202b607f6e30d37afc7e7a7219e5192329be564 Mon Sep 17 00:00:00 2001 From: Someone Date: Wed, 8 Dec 2021 19:38:38 +0100 Subject: [PATCH] [somebot] /ta-wipe-channel "--really-all"| --- modules/TACommandWipeChannel.py | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 modules/TACommandWipeChannel.py diff --git a/modules/TACommandWipeChannel.py b/modules/TACommandWipeChannel.py new file mode 100644 index 0000000..9938ce6 --- /dev/null +++ b/modules/TACommandWipeChannel.py @@ -0,0 +1,34 @@ +# Mattermost Bot module. +# Copyright (c) 2016-2021 by Someone (aka. Jan Vales ) +# published under MIT-License + + + +from AbstractCommand import * +class TACommandWipeChannel(AbstractCommand): + TRIGGER = "ta-wipe-channel" + CONFIG = {"display_name": "somebot-command", "auto_complete": True, + "auto_complete_hint": "'--really-all'|", + } + CONFIG["auto_complete_desc"] = CONFIG["description"] = "Delete all not-pinned posts in channel (optionally: only by ). There is no undo. [TEAM_ADMIN]" + + + def on_POST(self, request, data): + self._require_team_admin(data) # will throw an exception if not. (Dont try-except: Its handled up the stack.) + + u = self.bot.api.get_user_by_username(data["text"].strip(), exc=False) + if data["text"].strip() in ["--really-all", "--system"] and not u: + request.cmd_respond_text_temp("### parameter must be '--really-all', '--system' or a valid username. :(") + return + + # needs indirection. see iterators. + posts = [p for p in self.bot.api.get_posts_for_channel(data["channel_id"])] + for p in posts: + if p["is_pinned"]: + continue + + if (data["text"].strip() == "--really-all") or (data["text"].strip() == "--system" and p["type"].startswith("system_")) or (data["text"].strip() != "" and u["id"] == p["user_id"]) or (data["text"].strip() == ""): + self.bot.api.delete_post(p["id"]) + + request.cmd_respond_text_temp("### ``Done.`` :)") + self.bot.debug_chan("``/ta-wipe-channel "+data["text"].strip()+"`` used by ``@"+data["user_name"]+"`` in ``"+data["team_domain"]+"::"+data["channel_name"]+"``") -- 2.43.0