Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion matrix/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,22 +149,28 @@ 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
@bot.command()
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}")
15 changes: 14 additions & 1 deletion tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)


Expand Down
Loading