From 01a19fe208539a55ca359980ade3fe1a3d6cb235 Mon Sep 17 00:00:00 2001 From: Someone Date: Fri, 19 Jun 2020 01:32:13 +0200 Subject: [PATCH] [somebot] /ta-wipe-channel "--really-all"| --- somebot/modules/TACommandWipeChannel.py | 35 +++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 somebot/modules/TACommandWipeChannel.py diff --git a/somebot/modules/TACommandWipeChannel.py b/somebot/modules/TACommandWipeChannel.py new file mode 100644 index 0000000..080d29a --- /dev/null +++ b/somebot/modules/TACommandWipeChannel.py @@ -0,0 +1,35 @@ +# Mattermost Bot. +# Copyright (c) 2016-2020 by Someone (aka. Jan Vales ) +# published under MIT-License + +import logging + + +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()) + if data["text"].strip() != "--really-all" and len(u) == 0: + request.cmd_respond_text_temp("### parameter must be '--really-all' 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() != "" and u[0]["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