From 1aed62d0c1a1b8a9ea40de1beb828d0ba6c32c89 Mon Sep 17 00:00:00 2001 From: Ishita Tyagi Date: Wed, 1 Jul 2026 12:31:51 +0530 Subject: [PATCH] feat: add reason parameter to Message.delete() Closes #76 --- matrix/message.py | 8 +++++++- tests/test_message.py | 15 ++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/matrix/message.py b/matrix/message.py index f221dea..186ab36 100644 --- a/matrix/message.py +++ b/matrix/message.py @@ -149,9 +149,11 @@ async def typo(ctx: Context): except Exception as e: raise MatrixError(f"Failed to edit message: {e}") - async def delete(self) -> None: + async def delete(self, reason: str | None = None) -> None: """Removes the message content from the room. This action cannot be undone. + Optionally provide a reason that will be visible to room moderators. + ## Example ```python @@ -159,12 +161,16 @@ async def delete(self) -> None: async def oops(ctx: Context): msg = await ctx.reply("Secret info!") await msg.delete() + + # Delete with a reason + await message.delete(reason="Violated room rules") ``` """ try: await self.client.room_redact( room_id=self.room.room_id, event_id=self.event_id, + reason=reason, ) except Exception as e: raise MatrixError(f"Failed to delete message: {e}") diff --git a/tests/test_message.py b/tests/test_message.py index b22baad..4014d9f 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -121,7 +121,20 @@ async def test_delete__expect_message_redacted(message, client): await message.delete() client.room_redact.assert_awaited_once_with( - room_id="!room:example.com", event_id="$event123" + room_id="!room:example.com", event_id="$event123", reason=None + ) + + +@pytest.mark.asyncio +async def test_delete_with_reason__expect_reason_passed(message, client): + client.room_redact = AsyncMock() + + await message.delete(reason="Violated room rules") + + client.room_redact.assert_awaited_once_with( + room_id="!room:example.com", + event_id="$event123", + reason="Violated room rules", )