PSConnection class
-
class
psclient.PSConnection(username, password, onParsedMessage=None, onOpenThread=None, url='ws://sim.smogon.com:8000/showdown/websocket', chatlogger=None, loglevel=1) Bases:
objectRepresents 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
Messageobject - onOpenThread (function, optional) – a function that will run in its own thread once the socket is open,
with the
PSConnectionas an argument. Defaults toNone. - 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 toNone. - 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
Roomobjects - userList (dictionary) – a dictionary mapping all known
Userobjects 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
Userobject 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
Messageobject - onOpenThread (function) – a function that will run in its own thread once the socket is open,
with the
PSConnectionas an*argsargument - isLoggedIn (bool) –
Trueif the connection represents a logged-in user andFalseotherwise
-
getRoom(name) Gets the
Roomobject corresponding to an IDParameters: id (string in ID format) – the room ID (in ID format from toID())Returns: a Roomobject with the given IDReturn type: Room
-
getUser(userid) Gets the
Userobject for a given IDParameters: 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
MessageobjectParameters: 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:
-
userLeftRoom(user, room) Handles a user leaving a room
Parameters:
-
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