Skip to content
Open
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
18 changes: 18 additions & 0 deletions matrix/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,21 @@ async def kick_user(self, user_id: str, reason: str | None = None) -> None:
)
except Exception as e:
raise MatrixError(f"Failed to kick user: {e}")

async def leave(self) -> None:

@PenguinBoi12 PenguinBoi12 Jul 2, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new way to handle matrix calls has been added. Basically, the whole body of the function would become:

await matrix_call( 
    self.client.room_leave(room_id=self.room_id),
    error_message="Failed to leave room"
)

"""Leave the room.

The bot will leave the room and will no longer receive events from it.
This is useful for bots that join rooms temporarily or need to decline
invitations after inspection.

## Example

```python
await room.leave()
```
"""
try:
await self.client.room_leave(room_id=self.room_id)
except Exception as e:
raise MatrixError(f"Failed to leave room: {e}")
20 changes: 20 additions & 0 deletions tests/test_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,26 @@ async def test_kick_user_with_error__expect_matrix_error(room, client):
await room.kick_user("@troublemaker:example.com")


# LEAVE


@pytest.mark.asyncio
async def test_leave__expect_room_leave_called(room, client):
client.room_leave = AsyncMock()

await room.leave()

client.room_leave.assert_awaited_once_with(room_id="!room:example.com")


@pytest.mark.asyncio
async def test_leave_with_error__expect_matrix_error(room, client):
client.room_leave = AsyncMock(side_effect=Exception("Not in room"))

with pytest.raises(MatrixError, match="Failed to leave room"):
await room.leave()


def test_room_properties__expect_correct_delegation_to_matrix_room(room, matrix_room):
assert room.room_id == "!room:example.com"
assert room.name == "Test Room"
Expand Down
Loading