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.
This commit is contained in:
parent
8f637ac5e3
commit
9b2981de90
2 changed files with 34 additions and 2 deletions
|
@ -1,4 +1,5 @@
|
||||||
homeserver: https://matrix.example.com
|
homeserver: https://matrix.example.com
|
||||||
user_id: "@example:example.com"
|
user_id: "@example:example.com"
|
||||||
access_token: syt_1234567890
|
access_token: syt_1234567890
|
||||||
check_room: # If you want to check for permissions in a specific room instead of the current room
|
check_room: # If you want to check for permissions in a specific room instead of the current room
|
||||||
|
reminder_cooldown: 30
|
|
@ -1,4 +1,5 @@
|
||||||
import re
|
import re
|
||||||
|
import datetime
|
||||||
import asyncio
|
import asyncio
|
||||||
import yaml
|
import yaml
|
||||||
import nio
|
import nio
|
||||||
|
@ -10,14 +11,23 @@ url_regex = re.compile(r"(https?://\S+)|(www\.\S+)")
|
||||||
|
|
||||||
class RedactorBot:
|
class RedactorBot:
|
||||||
def __init__(
|
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 = AsyncClient(homeserver, user_id)
|
||||||
self.client.access_token = access_token
|
self.client.access_token = access_token
|
||||||
self.sync_timeout = sync_timeout
|
self.sync_timeout = sync_timeout
|
||||||
self.client.add_event_callback(self.message_callback, RoomMessage)
|
self.client.add_event_callback(self.message_callback, RoomMessage)
|
||||||
|
self.reminder_cooldown = reminder_cooldown
|
||||||
self.check_room = check_room
|
self.check_room = check_room
|
||||||
|
|
||||||
|
self.cooldown_log = {}
|
||||||
|
|
||||||
async def message_callback(self, room: MatrixRoom, event: RoomMessage):
|
async def message_callback(self, room: MatrixRoom, event: RoomMessage):
|
||||||
if isinstance(event, (RoomMessageText, RoomMessageMedia)):
|
if isinstance(event, (RoomMessageText, RoomMessageMedia)):
|
||||||
check_room = (
|
check_room = (
|
||||||
|
@ -42,6 +52,26 @@ class RedactorBot:
|
||||||
print(
|
print(
|
||||||
f"Redacted message from {event.sender} in {room.display_name}"
|
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:
|
else:
|
||||||
print(f"Failed to redact message: {response}")
|
print(f"Failed to redact message: {response}")
|
||||||
|
|
||||||
|
@ -62,6 +92,7 @@ bot = RedactorBot(
|
||||||
user_id=config["user_id"],
|
user_id=config["user_id"],
|
||||||
access_token=config["access_token"],
|
access_token=config["access_token"],
|
||||||
sync_timeout=config.get("sync_timeout", 30000),
|
sync_timeout=config.get("sync_timeout", 30000),
|
||||||
|
reminder_cooldown=config.get("reminder_cooldown", 30),
|
||||||
check_room=config.get("check_room", None),
|
check_room=config.get("check_room", None),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue