From 735bc9d671e45223937ef8e96172aee0e76569b8 Mon Sep 17 00:00:00 2001 From: William Kray Date: Wed, 14 Aug 2024 10:21:24 -0700 Subject: [PATCH] include ban and unban functionality --- kickbot/bot.py | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++ maubot.yaml | 2 +- 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/kickbot/bot.py b/kickbot/bot.py index cdff91e..764c8cd 100644 --- a/kickbot/bot.py +++ b/kickbot/bot.py @@ -274,6 +274,98 @@ class KickBot(Plugin): await evt.reply("lol you don't have permission to do that") + @activity.subcommand("ban", help='kick and ban a specific user from the community and all rooms') + @command.argument("mxid", "full matrix ID", required=True) + async def ban_user(self, evt: MessageEvent, mxid: UserID) -> None: + await evt.mark_read() + if evt.sender in self.config["admins"]: + user = mxid + msg = await evt.respond("starting the ban...") + roomlist = await self.get_space_roomlist() + # don't forget to kick from the space itself + roomlist.append(self.config["master_room"]) + ban_list = {} + error_list = {} + + ban_list[user] = [] + for room in roomlist: + try: + roomname = None + roomnamestate = await self.client.get_state_event(room, 'm.room.name') + roomname = roomnamestate['name'] + + await self.client.get_state_event(room, EventType.ROOM_MEMBER, user) + await self.client.ban_user(room, user, reason='banned') + if roomname: + ban_list[user].append(roomname) + else: + ban_list[user].append(room) + time.sleep(0.5) + except MNotFound: + pass + except Exception as e: + self.log.warning(e) + error_list[user] = [] + error_list[user].append(roomname or room) + + + results = "the following users were kicked and banned:

{ban_list}

the following errors were \ + recorded:

{error_list}

".format(ban_list=ban_list, error_list=error_list) + await evt.respond(results, allow_html=True, edits=msg) + + # sync our database after we've made changes to room memberships + await self.do_sync() + + else: + await evt.reply("lol you don't have permission to do that") + + + @activity.subcommand("unban", help='unban a specific user from the community and all rooms') + @command.argument("mxid", "full matrix ID", required=True) + async def unban_user(self, evt: MessageEvent, mxid: UserID) -> None: + await evt.mark_read() + if evt.sender in self.config["admins"]: + user = mxid + msg = await evt.respond("starting the unban...") + roomlist = await self.get_space_roomlist() + # don't forget to kick from the space itself + roomlist.append(self.config["master_room"]) + unban_list = {} + error_list = {} + + unban_list[user] = [] + for room in roomlist: + try: + roomname = None + roomnamestate = await self.client.get_state_event(room, 'm.room.name') + roomname = roomnamestate['name'] + + await self.client.get_state_event(room, EventType.ROOM_MEMBER, user) + await self.client.unban_user(room, user, reason='unbanned') + if roomname: + unban_list[user].append(roomname) + else: + unban_list[user].append(room) + time.sleep(0.5) + except MNotFound: + pass + except Exception as e: + self.log.warning(e) + error_list[user] = [] + error_list[user].append(roomname or room) + + + results = "the following users were unbanned:

{unban_list}

the following errors were \ + recorded:

{error_list}

".format(unban_list=unban_list, error_list=error_list) + await evt.respond(results, allow_html=True, edits=msg) + + # sync our database after we've made changes to room memberships + await self.do_sync() + + else: + await evt.reply("lol you don't have permission to do that") + + #need to somehow regularly fetch and update the list of room ids that are associated with a given space diff --git a/maubot.yaml b/maubot.yaml index 4da95cc..21af9fd 100644 --- a/maubot.yaml +++ b/maubot.yaml @@ -1,6 +1,6 @@ maubot: 0.1.0 id: org.jobmachine.kickbot -version: 0.0.11 +version: 0.0.12 license: MIT modules: - kickbot