import { EventEmitter } from 'events'; import Message from './classes/message'; import Room from './classes/room'; import User from './classes/user'; import { HTMLopts } from './classes/common'; type UserDetails = { userid: string; [key: string]: any }; type ClientOpts = { /** * The username you wish to connect to. Required parameter. */ username: string; /** * The password for the username you're connecting to. Leave this blank if the account is unregistered. */ password?: string; /** * The avatar your Bot will have on connection. If not specified, PS will set one randomly. */ avatar?: string | number; /** * The status your Bot will have on connection. */ status?: string; /** * An array with the strings of the rooms you want the Bot to join. */ rooms: string[]; /** * The function you would like to run on debugs. If this is a falsy value, debug messages will not be displayed. * If a true value is given which is not a function, the Bot simply logs messages to the console. */ debug?: boolean | ((details: string) => void); /** * Handling for internal errors. If a function is provided, this will run it with an error / string. * To opt out of error handling (not recommended), set this to false. * @default console.log */ handle?: boolean | ((error: string | Error) => void); /** * Does not populate userdetails automatically. Use `Client#getUserDetails` or `User#update` to populate a user. * @warning * Users will not have any properties other than id, userid, name, and alts. * @warning * `User#sendHTML` and `User#pageHTML` will be disabled. Use `Room#pmHTML` or `Room#pageHTML` instead. */ sparse?: boolean; /** * Enables scrollback (messages that are received from before the bot joins, such as * chat history). Scrollback messages will be emitted with the field `isIntro: true`. * @default false */ scrollback?: boolean; /** * Dictates whether messages throw errors by default. Set to 'true' to enable messages throwing errors. * @default false */ noFailMessages?: boolean; /** * The throttle (in milliseconds) for every 'batch' of three messages. PS has a per-message throttle of * 25ms for public roombots, 100ms for trusted users, and 600ms for regular users. */ throttle?: number; // A custom HTML processor, applied on all HTML methods. Defaults to no-transform. See HTML options for more info on opts. transformHTML?: (input: any, opts: HTMLopts) => string; /** * The time, in milliseconds, that your Bot will wait before attempting to login again after failing. * If this is 0, it will not attempt to login again. * @default 4_000 */ retryLogin?: number; /** * The time, in milliseconds, that your Bot will wait before attempting to reconnect after a disconnect. * If this is 0, it will not attempt to reconnect. * @default 5_000 */ autoReconnect?: number; /** * The time, in milliseconds, after which your connection times out. * @default 20_000 */ connectionTimeout?: number; /** * The server to connect to. * @default 'sim3.psim.us' */ server?: string; serverid?: string; /** * The port on which you're connecting to. Can also be specified in server as `url:port`, in which case leave this field blank. */ port?: number; /** * The protocol used for the websocket connection. Defaults to wss, but can be changed to ws (insecure). * @default 'ws' */ serverProtocol?: 'ws' | 'wss'; /** * The login server. * @default 'https://play.pokemonshowdown.com/~~showdown/action.php' */ loginServer?: string; }; export interface Client { /** * Any incoming or outgoing line of content between the server and client. */ on(event: 'packet', listener: (direction: 'in' | 'out', data: string) => void): this; /** * Emitted whenever the client connects to the server. */ on(event: 'connect', listener: () => void): this; /** * Emitted on any chat/PM message from the server/client. */ on(event: 'message', listener: (message: Message<'chat' | 'pm'>) => void): this; /** * Emitted whenever a user joins a room. */ on(event: 'join', listener: (room: string, user: string, isIntro: boolean) => void): this; /** * Emitted whenever a user leaves a room. */ on(event: 'leave', listener: (room: string, user: string, isIntro: boolean) => void): this; /** * Emitted whenever a user renames. */ on(event: 'name', listener: (room: string, newName: string, oldName: string, isIntro: boolean) => void): this; /** * Emitted whenever the client joins a room. */ on(event: 'joinRoom', listener: (room: string) => void): this; /** * Emitted whenever the client leaves a room. */ on(event: 'leaveRoom', listener: (room: string) => void): this; /** * Emitted whenever an error is shown in chat. This does not necessarily have to be from the user's input. */ on(event: 'chatError', listener: (room: string, error: string, isIntro: boolean) => void): this; /** * Emitted for all other types of events. */ on(event: 'tour' | string, listener: (room: string, line: string, isIntro: boolean) => void): this; } export class Client extends EventEmitter { opts: Required> & ClientOpts; status: { /** * Whether the socket is connected. */ connected: boolean; /** * Whether the client is authenticated on PS. */ loggedIn: boolean; /** * Whether the client has inited with rooms and whatnot. */ inited: boolean; username?: string | null; userid?: string | null; /** * The backoff exponent for reconnects. * The reconnect delay doubles for each attempt. */ backoff: number; lastConnected: number | null; }; isTrusted?: boolean; closed: boolean; rooms: Map; users: Map; constructor(opts: ClientOpts); /** * Connects to the server * @param retry Indicates whether this is a reconnect attempt */ connect(retry?: boolean): void; /** * Disconnects from the server */ disconnect(): void; /** * Logs in * @param username The username to use to log in * @param password The password for the username. LEave blank if unregistered * @returns A promise that resolves when the login message is sent */ login(username: string, password?: string): Promise; /** * Sends a text message to the server. Unthrottled; use sendQueue for chat messages * @param text The text to send */ send(text: string): void; /** * Schedules a message to be sent, while being throttled * @param text The message to send * @param sent The resolve method for a promise * @param fail The reject method for a promise * @returns A promise that resolves when the message is sent successfully */ sendQueue(text: string, sent: (msg: Message) => any, fail: (msg: Message) => any): void; /** * Sends a string to a user (if the user is not already tracked, they are added) * @param user The user to send to * @param text The message to send * @returns A promise that resolves when the message is sent successfully */ sendUser(user: User | string, text: string): Promise; /** * Adds a user to the list of tracked users on the Bot. Starts fetching userdetails in the background * @param details The details of the user to add, or the full username of the user. * @returns The added User */ addUser(details: string | UserDetails): User; /** * Gets the specified user (or their current user, if they were seen on an alt) * @param user The user to find * @param deepSearch Whether to also look for direct alts * @returns The user if found, `undefined` if not tracked, or `null` for invalid input */ getUser(user: string | User, deepSearch?: boolean): User | null | undefined; /** * Queues a request to fetch userdetails * @param userid The user being queried * @returns A promise that resolves with the queried userdetails */ getUserDetails(userid: string): Promise; /** * Gets a (cached) room from its name (aliases not supported) * @param room The name of the room being fetched * @returns The room being fetched */ getRoom(room: string): Room; /** * Joins a room * @param room The room to join * @returns A promise that resolves when the room is joined */ joinRoom(room: string): Promise; } export { Message, Room, User }; export * as Tools from './tools'; export * as Data from './data';