Skip to content

Space

Space extends Room to represent a Matrix Space. It is returned by Bot.get_space() and Bot.get_spaces() instead of a plain Room whenever the room type is m.space.

from matrix import Bot

bot = Bot()

space = bot.get_space("!space123:matrix.org")
if space:
    print(space.name)

matrix.space.Space

Space(matrix_room, client)

Bases: Room

Source code in matrix/room.py
46
47
48
def __init__(self, matrix_room: MatrixRoom, client: AsyncClient) -> None:
    self._matrix_room: MatrixRoom = matrix_room
    self._client: AsyncClient = client

get_children

get_children(depth=1)

Return the child rooms and spaces of this space that the bot has joined.

Children the bot has not joined are silently omitted. Use depth to recursively collect children of sub-spaces. depth=1 returns direct children only (default).

Example
space = bot.get_space("!space123:matrix.org")

for child in space.get_children():
    print(child.name)

for child in space.get_children(depth=3):
    print(child.name)
Source code in matrix/space.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def get_children(self, depth: int = 1) -> list[Room | Self]:
    """Return the child rooms and spaces of this space that the bot has joined.

    Children the bot has not joined are silently omitted. Use `depth` to
    recursively collect children of sub-spaces. `depth=1` returns direct
    children only (default).

    ## Example

    ```python
    space = bot.get_space("!space123:matrix.org")

    for child in space.get_children():
        print(child.name)

    for child in space.get_children(depth=3):
        print(child.name)
    ```
    """
    children: list[Room | Self] = []

    if depth < 0:
        raise ValueError(f"depth must be a non-negative integer, got {depth}")

    if depth == 0:
        return []

    for room_id in self.children:
        matrix_room = self._client.rooms.get(room_id)

        if not matrix_room:
            continue

        child = make_room(matrix_room, self._client)
        children.append(child)

        if isinstance(child, Space) and depth > 1:
            children.extend(child.get_children(depth - 1))

    return children