PSConnection class

class psclient.PSConnection(username, password, onParsedMessage=None, onOpenThread=None, url='ws://sim.smogon.com:8000/showdown/websocket', chatlogger=None, loglevel=1)

Bases: object

Represents a connection to Pokemon Showdown

Parameters:
  • username (string) – the username to log in to PS! with
  • password (string) – the password for the PS! username provided
  • onParsedMessage (function, optional) – a function that will be called each time a message is recieved the only argument passed in is the parsed Message object
  • onOpenThread (function, optional) – a function that will run in its own thread once the socket is open, with the PSConnection as an argument. Defaults to None.
  • url (str, optional) – the URL of the websocket of the server to connect to. Defaults to ws://sim.smogon.com:8000/showdown/websocket.
  • chatlogger (object, optional) – a chatlogger, whose handleMessage() method will be called on each message. Defaults to None.
  • loglevel (int, optional) – the level of logging (to stdout / stderr). Defaults to 1. 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
  • onParsedMessage (function) – a function that will be called each time a message is recieved the only argument passed in is the parsed Message object
  • onOpenThread (function) – a function that will run in its own thread once the socket is open, with the PSConnection as an *args argument
  • isLoggedIn (bool) – True if the connection represents a logged-in user and False otherwise
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
login(challstr)

Logs into Pokemon Showdown

Parameters:challstr (string) – the challstr to use to log in
onClose()

Logs when the connection closes

onError(error)

Handles errors on the websocket

Parameters:{string? probably} (error) – the error
onMessage(rawMessage)

Handles new messages from the websocket, creating a Message object

Parameters:rawMessage (string) – the raw message data
onOpen()

Logs when the websocket is opened

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
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