UNPKG

synthra-client

Version:

A user-friendly Lavalink client designed for NodeJS.

372 lines (371 loc) 15.4 kB
import { Filters } from "./Filters"; import { Synthra, SearchQuery, SearchResult } from "./Manager"; import { Lyrics, Node, SponsorBlockSegment } from "./Node"; import { Queue } from "./Queue"; import { Sizes, StateTypes, TrackSourceName, VoiceState } from "./Utils"; import { ClientUser, Message, User } from "discord.js"; export declare class Player { options: PlayerOptions; /** The Queue for the Player. */ readonly queue: Queue; /** The filters applied to the audio. */ filters: Filters; /** Whether the queue repeats the track. */ trackRepeat: boolean; /** Whether the queue repeats the queue. */ queueRepeat: boolean; /**Whether the queue repeats and shuffles after each song. */ dynamicRepeat: boolean; /** The time the player is in the track. */ position: number; /** Whether the player is playing. */ playing: boolean; /** Whether the player is paused. */ paused: boolean; /** The volume for the player */ volume: number; /** The Node for the Player. */ node: Node; /** The guild ID for the player. */ guildId: string; /** The voice channel for the player. */ voiceChannelId: string | null; /** The text channel for the player. */ textChannelId: string | null; /**The now playing message. */ nowPlayingMessage?: Message; /** The current state of the player. */ state: StateTypes; /** The equalizer bands array. */ bands: number[]; /** The voice state object from Discord. */ voiceState: VoiceState; /** The Manager. */ manager: Synthra; /** The autoplay state of the player. */ isAutoplay: boolean; /** The number of times to try autoplay before emitting queueEnd. */ autoplayTries: number | null; private static _manager; private readonly data; private dynamicLoopInterval; private dynamicRepeatIntervalMs; /** * Creates a new player, returns one if it already exists. * @param options The player options. * @see https://docs.magmastream.com/main/introduction/getting-started */ constructor(options: PlayerOptions); /** * Set custom data. * @param key - The key to set the data for. * @param value - The value to set the data to. */ set(key: string, value: unknown): void; /** * Retrieves custom data associated with a given key. * @template T - The expected type of the data. * @param {string} key - The key to retrieve the data for. * @returns {T} - The data associated with the key, cast to the specified type. */ get<T>(key: string): T; /** * Initializes the static properties of the Player class. * @hidden * @param manager The Manager to use. */ static init(manager: Synthra): void; /** * Same as Manager#search() but a shortcut on the player itself. * @param query * @param requester */ search<T extends User | ClientUser = User | ClientUser>(query: string | SearchQuery, requester?: T): Promise<SearchResult>; /** * Connects the player to the voice channel. * @throws {RangeError} If no voice channel has been set. * @returns {void} */ connect(): void; /** * Disconnects the player from the voice channel. * @throws {TypeError} If the player is not connected. * @returns {this} - The current instance of the Player class for method chaining. */ disconnect(): Promise<this>; /** * Destroys the player and clears the queue. * @param {boolean} disconnect - Whether to disconnect the player from the voice channel. * @returns {Promise<boolean>} - Whether the player was successfully destroyed. * @emits {PlayerDestroy} - Emitted when the player is destroyed. * @emits {PlayerStateUpdate} - Emitted when the player state is updated. */ destroy(disconnect?: boolean): Promise<boolean>; /** * Sets the player voice channel. * @param {string} channel - The new voice channel ID. * @returns {this} - The player instance. * @throws {TypeError} If the channel parameter is not a string. */ setVoiceChannelId(channel: string): this; /** * Sets the player text channel. * * This method updates the text channel associated with the player. It also * emits a player state update event indicating the change in the channel. * * @param {string} channel - The new text channel ID. * @returns {this} - The player instance for method chaining. * @throws {TypeError} If the channel parameter is not a string. */ setTextChannelId(channel: string): this; /** * Sets the now playing message. * * @param message - The message of the now playing message. * @returns The now playing message. */ setNowPlayingMessage<T = Message>(message: T): Message; /** * Plays the next track. * * If a track is provided, it will be played. Otherwise, the next track in the queue will be played. * If the queue is not empty, but the current track has not finished yet, it will be replaced with the provided track. * * @param {object} [optionsOrTrack] - The track to play or the options to play with. * @param {object} [playOptions] - The options to play with. * * @returns {Promise<void>} */ play(): Promise<Player>; play(track: Track): Promise<Player>; play(options: PlayOptions): Promise<Player>; play(track: Track, options: PlayOptions): Promise<Player>; /** * Sets the autoplay-state of the player. * * Autoplay is a feature that makes the player play a recommended * track when the current track ends. * * @param {boolean} autoplayState - Whether or not autoplay should be enabled. * @param {object} botUser - The user-object that should be used as the bot-user. * @param {number} [tries=3] - The number of times the player should try to find a * recommended track if the first one doesn't work. * @returns {this} - The player instance. */ setAutoplay(autoplayState: boolean, botUser?: object, tries?: number): this; /** * Gets recommended tracks and returns an array of tracks. * @param {Track} track - The track to find recommendations for. * @returns {Promise<Track[]>} - Array of recommended tracks. */ getRecommendedTracks(track: Track): Promise<Track[]>; /** * Handles YouTube-based recommendations. * @param {Track} track - The track to find recommendations for. * @returns {Promise<Track[]>} - Array of recommended tracks. */ private handleYouTubeRecommendations; /** * Handles Last.fm-based autoplay (or other platforms). * @param {Track} track - The track to find recommendations for. * @param {SearchPlatform} source - The selected search platform. * @param {string} apiKey - The Last.fm API key. * @returns {Promise<Track[]>} - Array of recommended tracks. */ private handlePlatformAutoplay; /** * Sets the volume of the player. * @param {number} volume - The new volume. Must be between 0 and 1000. * @returns {Promise<Player>} - The updated player. * @throws {TypeError} If the volume is not a number. * @throws {RangeError} If the volume is not between 0 and 1000. * @emits {PlayerStateUpdate} - Emitted when the volume is changed. * @example * player.setVolume(50); */ setVolume(volume: number): Promise<this>; /** * Sets the sponsorblock for the player. This will set the sponsorblock segments for the player to the given segments. * @param {SponsorBlockSegment[]} segments - The sponsorblock segments to set. Defaults to `[SponsorBlockSegment.Sponsor, SponsorBlockSegment.SelfPromo]` if not provided. * @returns {Promise<void>} The promise is resolved when the operation is complete. */ setSponsorBlock(segments?: SponsorBlockSegment[]): Promise<void>; /** * Gets the sponsorblock for the player. * @returns {Promise<SponsorBlockSegment[]>} The sponsorblock segments. */ getSponsorBlock(): Promise<SponsorBlockSegment[]>; /** * Deletes the sponsorblock for the player. This will remove all sponsorblock segments that have been set for the player. * @returns {Promise<void>} */ deleteSponsorBlock(): Promise<void>; /** * Sets the track repeat mode. * When track repeat is enabled, the current track will replay after it ends. * Disables queueRepeat and dynamicRepeat modes if enabled. * * @param repeat - A boolean indicating whether to enable track repeat. * @returns {this} - The player instance. * @throws {TypeError} If the repeat parameter is not a boolean. */ setTrackRepeat(repeat: boolean): this; /** * Sets the queue repeat. * @param repeat Whether to repeat the queue or not * @returns {this} - The player instance. * @throws {TypeError} If the repeat parameter is not a boolean */ setQueueRepeat(repeat: boolean): this; /** * Sets the queue to repeat and shuffles the queue after each song. * @param repeat "true" or "false". * @param ms After how many milliseconds to trigger dynamic repeat. * @returns {this} - The player instance. * @throws {TypeError} If the repeat parameter is not a boolean. * @throws {RangeError} If the queue size is less than or equal to 1. */ setDynamicRepeat(repeat: boolean, ms: number): this; /** * Restarts the currently playing track from the beginning. * If there is no track playing, it will play the next track in the queue. * @returns {Promise<Player>} The current instance of the Player class for method chaining. */ restart(): Promise<Player>; /** * Stops the player and optionally removes tracks from the queue. * @param {number} [amount] The amount of tracks to remove from the queue. If not provided, removes the current track if it exists. * @returns {Promise<this>} - The player instance. * @throws {RangeError} If the amount is greater than the queue length. */ stop(amount?: number): Promise<this>; /** * Skips the current track. * @returns {this} - The player instance. * @throws {Error} If there are no tracks in the queue. * @emits {PlayerStateUpdate} - With {@link PlayerStateEventTypes.TrackChange} as the change type. */ pause(pause: boolean): Promise<this>; /** * Skips to the previous track in the queue. * @returns {this} - The player instance. * @throws {Error} If there are no previous tracks in the queue. * @emits {PlayerStateUpdate} - With {@link PlayerStateEventTypes.TrackChange} as the change type. */ previous(): Promise<this>; /** * Seeks to a given position in the currently playing track. * @param position - The position in milliseconds to seek to. * @returns {this} - The player instance. * @throws {Error} If the position is invalid. * @emits {PlayerStateUpdate} - With {@link PlayerStateEventTypes.TrackChange} as the change type. */ seek(position: number): Promise<this>; /** * Returns the current repeat state of the player. * @param player The player to get the repeat state from. * @returns The repeat state of the player, or null if it is not repeating. */ private getRepeatState; /** * Automatically moves the player to a usable node. * @returns {Promise<Player | void>} - The player instance or void if not moved. */ autoMoveNode(): Promise<Player | void>; /** * Moves the player to another node. * @param {string} identifier - The identifier of the node to move to. * @returns {Promise<Player>} - The player instance after being moved. */ moveNode(identifier: string): Promise<Player>; /** * Transfers the player to a new server. If the player already exists on the new server * and force is false, this method will return the existing player. Otherwise, a new player * will be created and the current player will be destroyed. * @param {PlayerOptions} newOptions - The new options for the player. * @param {boolean} force - Whether to force the creation of a new player. * @returns {Promise<Player>} - The new player instance. */ switchGuild(newOptions: PlayerOptions, force?: boolean): Promise<Player>; /** * Retrieves the current lyrics for the playing track. * @param skipTrackSource - Indicates whether to skip the track source when fetching lyrics. * @returns {Promise<Lyrics>} - The lyrics of the current track. * @throws {RangeError} - If the 'lavalyrics-plugin' is not available on the Lavalink node. */ getCurrentLyrics(skipTrackSource?: boolean): Promise<Lyrics>; } export interface PlayerOptions { /** The guild ID the Player belongs to. */ guildId: string; /** The text channel the Player belongs to. */ textChannelId: string; /** The voice channel the Player belongs to. */ voiceChannelId?: string; /** The node the Player uses. */ node?: string; /** The initial volume the Player will use. */ volume?: number; /** If the player should mute itself. */ selfMute?: boolean; /** If the player should deaf itself. */ selfDeafen?: boolean; } /** If track partials are set some of these will be `undefined` as they were removed. */ export interface Track { /** The base64 encoded track. */ readonly track: string; /** The artwork url of the track. */ readonly artworkUrl: string; /** The track source name. */ readonly sourceName: TrackSourceName; /** The title of the track. */ title: string; /** The identifier of the track. */ readonly identifier: string; /** The author of the track. */ author: string; /** The duration of the track. */ readonly duration: number; /** The ISRC of the track. */ readonly isrc: string; /** If the track is seekable. */ readonly isSeekable: boolean; /** If the track is a stream.. */ readonly isStream: boolean; /** The uri of the track. */ readonly uri: string; /** The thumbnail of the track or null if it's a unsupported source. */ readonly thumbnail: string | null; /** The user that requested the track. */ readonly requester?: User | ClientUser; /** Displays the track thumbnail with optional size or null if it's a unsupported source. */ displayThumbnail(size?: Sizes): string; /** Additional track info provided by plugins. */ pluginInfo: TrackPluginInfo; /** Add your own data to the track. */ customData: Record<string, unknown>; } export interface TrackPluginInfo { albumName?: string; albumUrl?: string; artistArtworkUrl?: string; artistUrl?: string; isPreview?: string; previewUrl?: string; } export interface PlayOptions { /** The position to start the track. */ readonly startTime?: number; /** The position to end the track. */ readonly endTime?: number; /** Whether to not replace the track if a play payload is sent. */ readonly noReplace?: boolean; } export interface EqualizerBand { /** The band number being 0 to 14. */ band: number; /** The gain amount being -0.25 to 1.00, 0.25 being double. */ gain: number; }