Connection class

class psclient.Connection(username, password, websocket, chatlogger, loglevel)

Bases: object

Represents a connection to Pokemon Showdown

You should NOT be constructing this class yourself. Instead, use psclient.connect().

Parameters:
  • username (string) – the username to log in to PS! with
  • password (string) – the password for the PS! username provided
  • websocket (object) – the websocket of the server to connect to.
  • chatlogger (object) – a chatlogger, whose handleMessage() method will be called on each message.
  • loglevel (int) – the level of logging (to stdout / stderr). Higher means more verbose.
Variables:
  • roomList (set) – a set of all the known Room objects
  • userList (dictionary) – a dictionary mapping all known User objects to lists of room IDs
  • password (string) – the password to use to log into PS
  • loglevel (int) – the level of logging
  • lastSentTime (int) – the timestamp at which the most recent message was sent
  • this (User) – an User object referring to the user who’s logged in
  • isLoggedIn (bool) – True if the connection represents a logged-in user and False otherwise
getMessage() → psclient.Message

Gets a message from the websocket and parses it as needed

This is a blocking method if awaited, but you can use asyncio.wait_for() safely.

Returns:the message, or None if no message was received
Return type:Message or None
getRoom(name)

Gets the Room object corresponding to an ID

Parameters:id (string in ID format) – the room ID (in ID format from toID())
Returns:a Room object with the given ID
Return type:Room
getUser(userid)

Gets the User object for a given ID

Parameters:userid (string that is an ID) – the ID of the user to search for
Returns:the user with the given ID
Return type:User or None
getUserRooms(user)

Gets a set of the IDs (not objects) of the rooms that the user is in.

Parameters:user (User) – the user
Returns:
Return type:set or None
handleMessage(rawMessage: Union[str, bytes]) → psclient.Message

Handles incoming raw messages

Parameters:rawMessage (Union[str, bytes]) – the raw message from the websocket
Returns:the parsed message
Return type:Message
log(message)

Logs a message to the console according to LOGLEVEL

Parameters:message (string) – the message to be logged, beginning with E:, W:, I:, or DEBUG:
login(challstr)

Logs into Pokemon Showdown

Parameters:challstr (string) – the challstr to use to log in
messages() → AsyncGenerator[psclient.Message, None]

An async generator yielding messages from the websocket

Yields:Message – a processed messages
sayIn(room, message)

Sends a message to a room.

Parameters:
  • room (Room) – the room to send the message to
  • message (string) – the message to send
send(message)

Sends a message

Parameters:message (string) – the message to send
userJoinedRoom(user, room)

Handles a user joining a room

Parameters:
  • user (User) – the user who joined
  • room (Room) – the room they joined
userLeftRoom(user, room)

Handles a user leaving a room

Parameters:
  • user (User) – the user who joined
  • room (Room) – the room they joined
waitForLogin()

Waits for a challstr and then logs in.

May block infinitely if the server never sends a |challstr|, so using asyncio.wait_for() is advised.

whisper(userid, message)

PMs a message to a user

Parameters:
  • userid (string in ID format) – the user to PM
  • message (string) – the message to PM
psclient.connect(username: str, password: str, url='wss://sim3.psim.us/showdown/websocket', chatlogger=None, loglevel=2) → psclient.Connection

Creates a new connection to a PS server, and logs in

Parameters:
  • username (str) – the username to use for the connection
  • password (str) – the password for the username
  • url (str, optional) – the URL of the server to connect to. Defaults to “wss://sim3.psim.us/showdown/websocket”.
  • chatlogger (Chatlogger, optional) – a ps-client compatible chatlogger to use. Defaults to None.
  • loglevel (int, optional) – the level of logging; higher is more verbose. Defaults to 2.
Returns:

an object representing the connection

Return type:

Connection