From 9b2981de9054254cba84ddc2d7d78a8cc8d97447 Mon Sep 17 00:00:00 2001 From: Kumi Date: Thu, 26 Dec 2024 12:51:06 +0100 Subject: [PATCH] feat: Add reminder cooldown for room notices Introduces a customizable cooldown period for sending room notices after message redaction to prevent flooding users with notifications. Configurable via `reminder_cooldown` setting in the configuration file. --- config.dist.yaml | 3 ++- src/matrix_redactorbot/main.py | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/config.dist.yaml b/config.dist.yaml index 774f50b..84f3933 100644 --- a/config.dist.yaml +++ b/config.dist.yaml @@ -1,4 +1,5 @@ homeserver: https://matrix.example.com user_id: "@example:example.com" access_token: syt_1234567890 -check_room: # If you want to check for permissions in a specific room instead of the current room \ No newline at end of file +check_room: # If you want to check for permissions in a specific room instead of the current room +reminder_cooldown: 30 \ No newline at end of file diff --git a/src/matrix_redactorbot/main.py b/src/matrix_redactorbot/main.py index b93f675..83d3dca 100644 --- a/src/matrix_redactorbot/main.py +++ b/src/matrix_redactorbot/main.py @@ -1,4 +1,5 @@ import re +import datetime import asyncio import yaml import nio @@ -10,14 +11,23 @@ url_regex = re.compile(r"(https?://\S+)|(www\.\S+)") class RedactorBot: def __init__( - self, homeserver, user_id, access_token, sync_timeout, check_room=None + self, + homeserver, + user_id, + access_token, + sync_timeout=30000, + reminder_cooldown=30, + check_room=None, ): self.client = AsyncClient(homeserver, user_id) self.client.access_token = access_token self.sync_timeout = sync_timeout self.client.add_event_callback(self.message_callback, RoomMessage) + self.reminder_cooldown = reminder_cooldown self.check_room = check_room + self.cooldown_log = {} + async def message_callback(self, room: MatrixRoom, event: RoomMessage): if isinstance(event, (RoomMessageText, RoomMessageMedia)): check_room = ( @@ -42,6 +52,26 @@ class RedactorBot: print( f"Redacted message from {event.sender} in {room.display_name}" ) + + # Send a notice to the room to inform the user + if ( + not self.cooldown_log.get(room.room_id) + or ( + datetime.datetime.now() + - self.cooldown_log[room.room_id] + ).seconds + > self.reminder_cooldown + ): + self.cooldown_log[room.room_id] = datetime.datetime.now() + await self.client.room_send( + room_id=room.room_id, + message_type="m.room.message", + content={ + "msgtype": "m.notice", + "body": "New users cannot send links or uploads until powerlevel raised above zero.", + }, + ) + else: print(f"Failed to redact message: {response}") @@ -62,6 +92,7 @@ bot = RedactorBot( user_id=config["user_id"], access_token=config["access_token"], sync_timeout=config.get("sync_timeout", 30000), + reminder_cooldown=config.get("reminder_cooldown", 30), check_room=config.get("check_room", None), )