berconpy.client.RCONClient

class berconpy.client.RCONClient(*, cache, dispatch)

Bases: ABC

The base class for all client implementations of the RCON protocol.

Methods

add_listener(event, func)

A shorthand for the EventDispatcher.add_listener() method.

ban(addr[, duration, reason])

Bans a given player ID, GUID, or IP address (without port).

check_disallowed_command(response)

Raises RCONCommandError if the server responded with "Disallowed command".

close()

Closes the connection.

connect(ip, port, password)

Returns a context manager that connects and logs into an RCON server.

fetch_admins()

Requests a list of RCON admins connected to the server, ordered by admin ID and IP address with port.

fetch_bans()

Requests a list of bans on the server.

fetch_missions()

Requests a list of mission files on the server.

fetch_players()

Requests a list of players from the server.

get_player(player_id)

A shorthand for RCONClientCache.get_player().

is_connected()

Indicates if the client has a currently active connection with the server.

is_logged_in()

Indicates if the client is currently authenticated with the server.

is_running()

Indicates if the client is running.

kick(player_id[, reason])

Kicks a player with the given ID from the server with an optional reason.

listen([event])

A shorthand for the EventDispatcher.listen() decorator.

on_message(message)

Parses a message sent from the server into various events.

parse_admins(response)

Parses an "admins" command response into a list of (IP, port) tuples.

parse_bans(response, *, cls)

Parses a "bans" command response into a list of Ban objects.

parse_missions(response)

Parses a "missions" command response into a list of mission files.

remove_listener(event, func)

A shorthand for the EventDispatcher.remove_listener() method.

send(message)

Sends a message to all players in the server.

send_command(command)

Sends a command to the server and waits for a response.

unban(ban_id)

Removes the ban with the given ID from the server.

whisper(player_id, message)

Sends a message to the player with the given ID.

Attributes

admin_id

A shorthand for RCONClientCache.admin_id.

cache

The cache used by the client.

dispatch

The event dispatcher used by the client.

players

A shorthand for RCONClientCache.players.

add_listener(event, func)

A shorthand for the EventDispatcher.add_listener() method.

See the EventDispatcher for a list of supported events.

Parameters:
  • event (str) – The event to listen for.

  • func (Callable) – The function to dispatch when the event is received.

Return type:

None

ban(addr, duration=None, reason='')

Bans a given player ID, GUID, or IP address (without port).

Note that the player ID cannot be used to ban players that are no longer in the server; a GUID or IP address must be provided.

Player.ban_ip() and Player.ban_guid() should be shorthands for calling this method.

Parameters:
  • addr (int | str) – The ID, GUID, or IP address to ban.

  • duration (Optional[int]) – The duration of the ban in minutes. If None, the ban will be permanent.

  • reason (str) – An optional reason to include with the ban.

Return type:

Union[str, Awaitable[str]]

Returns:

The response from the server, if any.

Raises:
  • RCONCommandError – The server has either disabled this command or failed to respond to our command.

  • RuntimeError – The client is either not connected or the server could/would not respond to the command.

check_disallowed_command(response)

Raises RCONCommandError if the server responded with “Disallowed command”.

This method should be used when implementing send_command().

Return type:

None

abstract close()

Closes the connection.

This method should be idempotent, i.e. having no effect when called multiple times consecutively.

Return type:

Optional[Awaitable[None]]

abstract connect(ip, port, password)

Returns a context manager that connects and logs into an RCON server.

Raises:
  • LoginFailure – The password given to the server was denied.

  • RuntimeError – This method was called while the client is already connected.

Return type:

Union[ContextManager[Self], AsyncContextManager[Self]]

abstract fetch_admins()

Requests a list of RCON admins connected to the server, ordered by admin ID and IP address with port.

Return type:

Union[Sequence[tuple[int, str]], Awaitable[Sequence[tuple[int, str]]]]

abstract fetch_bans()

Requests a list of bans on the server.

Return type:

Union[Sequence[Ban], Awaitable[Sequence[Ban]]]

abstract fetch_missions()

Requests a list of mission files on the server.

Return type:

Union[Sequence[str], Awaitable[Sequence[str]]]

abstract fetch_players()

Requests a list of players from the server.

This method also updates the player cache.

Return type:

Union[Sequence[Player], Awaitable[Sequence[Player]]]

get_player(player_id)

A shorthand for RCONClientCache.get_player().

Return type:

Optional[Player]

abstract is_connected()

Indicates if the client has a currently active connection with the server.

Return type:

bool

abstract is_logged_in()

Indicates if the client is currently authenticated with the server.

Return type:

Optional[bool]

Returns:

True if authenticated or None if no response has been received from the server.

Raises:

LoginFailure – The password given to the server was denied.

abstract is_running()

Indicates if the client is running. This may not necessarily mean that the client is connected.

Return type:

bool

kick(player_id, reason='')

Kicks a player with the given ID from the server with an optional reason.

Player.kick() is a shorthand for calling this method.

Parameters:
  • player_id (int) – The ID of the player.

  • reason (str) – An optional reason to show when kicking.

Return type:

Union[str, Awaitable[str]]

Returns:

The response from the server, if any.

Raises:
  • RCONCommandError – The server has either disabled this command or failed to respond to our command.

  • RuntimeError – The client is either not connected or the server could/would not respond to the command.

listen(event=None)

A shorthand for the EventDispatcher.listen() decorator.

Example usage:

>>> client = AsyncRCONClient()
>>> @client.listen()
... async def on_login():
...     print("We have logged in!")
Parameters:

event (Optional[str]) – The event to listen for. If None, the function name is used as the event name.

Return type:

Callable[[TypeVar(T)], TypeVar(T)]

on_message(message)

Parses a message sent from the server into various events.

This method is automatically added as a listener on initialization.

parse_admins(response)

Parses an “admins” command response into a list of (IP, port) tuples.

This method should be used when implementing fetch_admins().

Return type:

list[tuple[int, str]]

parse_bans(response, *, cls)

Parses a “bans” command response into a list of Ban objects.

This method should be used when implementing fetch_bans().

Parameters:
  • response (str) – The server response to parse.

  • cls (Type[TypeVar(BanT, bound= Ban)]) – The Ban subclass to use.

Return type:

list[TypeVar(BanT, bound= Ban)]

parse_missions(response)

Parses a “missions” command response into a list of mission files.

This method should be used when implementing fetch_missions().

Return type:

list[str]

remove_listener(event, func)

A shorthand for the EventDispatcher.remove_listener() method.

This method should be a no-op if the given event and function does not match any registered listener.

Parameters:
  • event (str) – The event used by the listener.

  • func (Callable) – The function used by the listener.

Return type:

None

send(message)

Sends a message to all players in the server.

Parameters:

message (str) – The message to send.

Return type:

Union[str, Awaitable[str]]

Returns:

The response from the server, if any.

Raises:
  • RCONCommandError – The server has either disabled this command or failed to respond to our command.

  • RuntimeError – The client is either not connected or the server could/would not respond to the command.

abstract send_command(command)

Sends a command to the server and waits for a response.

Parameters:

command (str) – The command to send.

Return type:

Union[str, Awaitable[str]]

Returns:

The server’s response as a string.

Raises:
  • RCONCommandError – The server has either disabled this command or failed to respond to our command.

  • RuntimeError – The client is either not connected or the server could/would not respond to the command.

unban(ban_id)

Removes the ban with the given ID from the server.

Parameters:

ban_id (int) – The ID of the ban to remove.

Return type:

Union[str, Awaitable[str]]

Returns:

The response from the server, if any.

Raises:
  • RCONCommandError – The server has either disabled this command or failed to respond to our command.

  • RuntimeError – The client is either not connected or the server could/would not respond to the command.

whisper(player_id, message)

Sends a message to the player with the given ID.

Player.send() is a shorthand for calling this method.

Parameters:
  • player_id (int) – The ID of the player to send to.

  • message (str) – The message to send.

Return type:

Union[str, Awaitable[str]]

Returns:

The response from the server, if any.

Raises:
  • RCONCommandError – The server has either disabled this command or failed to respond to our command.

  • RuntimeError – The client is either not connected or the server could/would not respond to the command.

property admin_id: int | None

A shorthand for RCONClientCache.admin_id.

property cache: RCONClientCache

The cache used by the client.

property dispatch: EventDispatcher

The event dispatcher used by the client.

property players: Sequence[Player]

A shorthand for RCONClientCache.players.