From 540e2f90ff1e914a58356309634012d51fe1b0f7 Mon Sep 17 00:00:00 2001 From: Someone Date: Fri, 19 Jun 2020 01:32:13 +0200 Subject: [PATCH] [somebot] /ta-sync-admins --- somebot/modules/TACommandSyncAdmins.py | 44 ++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 somebot/modules/TACommandSyncAdmins.py diff --git a/somebot/modules/TACommandSyncAdmins.py b/somebot/modules/TACommandSyncAdmins.py new file mode 100644 index 0000000..6d3d27c --- /dev/null +++ b/somebot/modules/TACommandSyncAdmins.py @@ -0,0 +1,44 @@ +# Mattermost Bot. +# Copyright (c) 2016-2020 by Someone (aka. Jan Vales ) +# published under MIT-License + + + +from AbstractCommand import * +class TACommandSyncAdmins(AbstractCommand): + TRIGGER = "ta-sync-admins" + CONFIG = {"display_name": "somebot-command", "auto_complete": True, + } + CONFIG["auto_complete_desc"] = CONFIG["description"] = "Sync TAs to other managed teams. [TEAM_ADMIN]" + + + def __init__(self, team_id, managed_teams, active_admins_channel_id, additional_user_ids=[]): + super().__init__(team_id) + + self.managed_teams = managed_teams + self.active_admins_channel_id = active_admins_channel_id + self.additional_user_ids=additional_user_ids + + + def on_POST(self, request, data): + user = self.bot.api.get_team_member(data["team_id"], data["user_id"]) + if "team_admin" not in user["roles"]: + request.cmd_respond_text_temp("### You are not a Team Admin. :(") + return + + all_user_ids = [u["id"] for u in self.bot.api.get_users()] + users_ids = [u["user_id"] for u in self.bot.api.get_channel_members(self.active_admins_channel_id)] + users_ids += self.additional_user_ids + + for _, t in self.managed_teams.items(): + for u in users_ids: + if t[1]: + self.bot.api.add_user_to_team(t[0], u) + self.bot.api.update_team_members_scheme_roles(t[0], u, {"scheme_user": True, "scheme_admin": True}) + + diff_uids = set(all_user_ids) - set(users_ids) + for u in diff_uids: + self.bot.api.update_team_members_scheme_roles(t[0], u, {"scheme_user": True, "scheme_admin": False}) + + request.cmd_respond_text_temp("### ``Done.`` :)") + -- 2.43.0