Skip to content

Context

For each command invoked, the first argument is always a Context instance, which holds a lot of metadata, and a few utility functions to help you write commands.

A lot of the time, these are the three main attributes you'll be using:

  • Context.room (nio.MatrixRoom) - the room the command was invoked in.
  • Context.event (nio.RoomMessageText) - the message that invoked this command.
  • Context.respond - a utility class to help you respond to the command.

Command Context

Event-based context for a command callback

room property

room: MatrixRoom

The room that the event was dispatched in

client property

client: NioBot

The current instance of the client

command property

command: Command

The current command being invoked

args property

args: list[str]

Each argument given to this command

message property

message: RoomMessageText

The current message

original_response property

original_response: Optional[RoomSendResponse]

The result of Context.reply(), if it exists.

latency property

latency: float

Returns the current event's latency in milliseconds.

respond async

respond(
    content: Optional[str] = None,
    file: Optional[BaseAttachment] = None,
) -> ContextualResponse

Responds to the current event.

Parameters:

Name Type Description Default
content Optional[str]

The text to reply with

None
file Optional[BaseAttachment]

A file to reply with

None

Returns:

Type Description
ContextualResponse

Contextual Response

Context class for managing replies.

Usage of this function is not required, however it is a useful utility.

message property

message: Optional[RoomMessageText]

Fetches the current message for this response

reply async

reply(*args) -> ContextualResponse

Replies to the current response.

This does NOT reply to the original invoking message.

Parameters:

Name Type Description Default
args

args to pass to send_message

()

Returns:

Type Description
ContextualResponse

a new ContextualResponse object.

edit async

edit(content: str, **kwargs) -> ContextualResponse

Edits the current response.

Parameters:

Name Type Description Default
content str

The new content to edit with

required
kwargs

Any extra arguments to pass to Client.edit_message

{}

Returns:

Type Description
ContextualResponse

self

delete async

delete(reason: Optional[str] = None) -> None

Redacts the current response.

Parameters:

Name Type Description Default
reason Optional[str]

An optional reason for the redaction

None

Returns:

Type Description
None

None, as there will be no more response.