UNPKG

websockets-topgg

Version:

A real-time WebSocket client for Top.gg that converts webhooks into a persistent connection, enabling seamless vote tracking, reminders, and enhanced community engagement.

77 lines (76 loc) 3.11 kB
import { EventEmitter } from "events"; import { Close, Entity, Options, Ready, Reminder, User, Vote } from "./typings.js"; export declare class TopWebsocket extends EventEmitter { private socketToken; private options; private client; private pingTimeout; private _status; private userCache; private reconnectionAttempts; private reconnectionDelay; entityId: string | undefined; lastMessageTimestamp: number | undefined; private noReconnectStatusCodes; on(event: "ready", listener: (ready: Ready) => void): this; on(event: "error", listener: (error: Error) => void): this; on(event: "disconnected", listener: (data: Close) => void): this; on(event: "reminder", listener: (data: Reminder, messageTimestamp: number, lastMessageTimestamp: number | undefined) => void): this; on(event: "vote" | "test", listener: (data: Vote, messageTimestamp: number, lastMessageTimestamp: number | undefined) => void): this; /** * Create a new TopWebsocket instance * * @param {string} socketToken Socket token * @param {Options} [options] Websocket options and configuration of userCache */ constructor(socketToken: string, options?: Options); /** * Get the current status of the websocket * @returns {string} The current status of the websocket */ get status(): string; private heartbeat; /** * Connect to the websocket * @param {number} [lastMessageTimestamp] The timestamp of the last message received, used to resume a connection. IE all messages after the provided timestamp will be sent * @returns {void} */ connect(lastMessageTimestamp?: number | undefined): void; private setupEventHandlers; private handleClose; private handleMessage; private parseBufferToJson; /** * Get User * @param {string} userId The user's ID * @param {boolean} [ignoreCache] When true ignore the cache and fetch directly from the API * @returns {Promise<User>} The user */ getUser: (userId: string, ignoreCache?: boolean) => Promise<User>; /** * Get Entity * @returns {Promise<Entity>} and entity */ getEntity: () => Promise<Entity>; /** * User Voted within past 12 hours * @param {string} userId The user's ID * @param {boolean} ignoreCache When true ignore the cache and fetch directly from the API * @returns {Promise<boolean>} Whether the user has voted in the past 12 hours */ userVoted: (userId: string, ignoreCache?: boolean) => Promise<boolean>; /** * Set user reminders * @param {string} userId The user's ID * @param {boolean} enable Whether to enable or disable reminders * @returns {Promise<User | undefined>} */ setReminders: (userId: string, enable: boolean) => Promise<User | undefined>; /** * Function send a fetch request * @param {string} path The URL to send the request to * @param {string} method The method of the request * @param {object} body The body of the request */ private fetchRequest; }