UNPKG

@persian-caesar/discord-player

Version:

Type definitions and helpers for a Discord music player (MusicPlayer) without external dependencies

237 lines (236 loc) 7.05 kB
import { VoiceConnection } from "@discordjs/voice"; import { VoiceChannel, TypedEmitter, TrackMetadata, MusicPlayerOptions } from "./types"; import EventEmitter from "events"; /** * Manages voice connection, playback, queue, history, and loop modes. * Emits events defined in MusicPlayerEvent. */ export declare class MusicPlayer extends EventEmitter<TypedEmitter> { channel: VoiceChannel; private previousQueueOrder; private connection; private player; private volume; private queue; private history; private loopQueue; private loopTrack; private playing; private autoLeaveOnEmptyQueue; private autoLeaveOnIdleMs; private idleTimer; private shuffield; private radioMode; private radioUrls; private connectionData; /** * Initializes a new MusicPlayer instance. * @param channel - Discord voice channel to connect to. * @param initialVolume - Initial volume (0–100), defaults to 100. * @param options - Options for auto-leave and idle timeout. */ constructor(channel: VoiceChannel, initialVolume?: number, options?: MusicPlayerOptions); /** * Sets the connection data for the voice channel. * @param data - Connection settings. * @returns The current MusicPlayer instance. */ setData(data: { channelId: string; guildId: string; adapterCreator: any; selfDeaf?: boolean; selfMute?: boolean; group?: string; debug?: boolean; }): this; /** * Joins the voice channel using provided or stored connection data. * @param data - Optional connection settings; uses stored data if not provided. * @returns The VoiceConnection instance. */ join(data?: { channelId: string; guildId: string; adapterCreator: any; selfDeaf?: boolean; selfMute?: boolean; group?: string; debug?: boolean; }): VoiceConnection; /** * Establishes a connection to the voice channel if not already connected. */ private ensureConnection; /** * Starts a timer to disconnect after inactivity. */ private startIdleTimer; /** * Clears the idle timer if it exists. */ private clearIdleTimer; /** * Randomly shuffles an array. * @param array - Array to shuffle. * @returns New shuffled array. */ private shuffleArray; /** * Searches Google for song lyrics and returns them, or null if not found. * @param title - Song title. * @param artist - Optional artist name for better accuracy. */ searchLyrics(title: string, artist?: string): Promise<string | null>; /** * Resolves a query to a track URL or returns the URL if provided. * @param query - Search query or URL. * @returns Resolved URL. */ search(query: string): Promise<string>; /** * Starts radio mode with a list of URLs. * @param urls - Array of URLs to play. */ startRadio(urls: string[]): Promise<void>; /** * Creates a stream from a SoundCloud URL. * @param url - SoundCloud URL. * @returns Readable stream. */ private createStreamFromScdl; /** * Creates a stream from a YouTube URL using various ytdl libraries. * @param url - YouTube URL. * @returns Readable stream or null if failed. */ private createStreamFromYtdl; /** * Creates a stream from a URL using play-dl. * @param url - URL to stream. * @returns Readable stream or null if failed. */ private createStreamFromPlayDl; /** * Fetches metadata for a track URL. * @param url - Track URL. * @returns Track metadata. */ private fetchMetadata; /** * Plays a track URL with its metadata. * @param url - Track URL. * @param metadata - Track metadata. */ private playUrl; /** * Plays a song by query or URL, adding to queue if already playing. * @param input - Search query or URL. */ play(input: string): Promise<void>; /** * Pauses the current playback. */ pause(): void; /** * Resumes the current playback. */ resume(): void; /** * Sets the playback volume. * @param percent - Volume percentage (0–200). */ setVolume(percent: number): number; /** * Handles idle state, looping or playing next track as needed. */ private onIdle; /** * Skips the current track and moves to the next. */ skip(): void; /** * Plays the previous track from history. */ previous(): Promise<void>; /** * Shuffles the queue and saves the previous order. */ shuffle(): void; /** * Restores the queue to its pre-shuffle order, excluding played tracks. */ undoShuffle(): void; /** * Toggles queue looping on or off. */ toggleLoopQueue(): boolean; /** * Checks if queue looping is enabled. * @returns True if enabled, false otherwise. */ isLoopQueue(): boolean; /** * Toggles track looping on or off. */ toggleLoopTrack(): boolean; /** * Checks if track looping is enabled. * @returns True if enabled, false otherwise. */ isLoopTrack(): boolean; /** * Disconnects from the voice channel and cleans up resources. */ disconnect(): void; /** * Stops playback and optionally disconnects. * @param noLeave - If true, stays connected to the channel. */ stop(noLeave?: boolean): void; /** * Returns a copy of the current queue. * @returns Array of track metadata. */ getQueue(): TrackMetadata[]; /** * Gets the current volume percentage. * @returns Volume (0–200). */ getVolume(): number; /** * Checks if the player is currently playing. * @returns True if playing, false otherwise. */ isPlaying(): boolean; /** * Checks if the player is paused. * @returns True if paused, false otherwise. */ isPaused(): boolean; /** * Checks if the queue is shuffled. * @returns True if shuffled, false otherwise. */ isShuffiled(): boolean; /** * Checks if the bot is actively connected to a voice channel. * @param guildId - Optional guild ID; uses stored guild ID if not provided. * @returns True if actively connected, false otherwise. */ isConnected(guildId?: string): boolean; /** * Creates a custom error for the music player. * @param message - Error message. * @returns Custom error instance. */ private createError; } /** * @copyright * Code by Sobhan-SRZA (mr.sinre) | https://github.com/Sobhan-SRZA * Developed for Persian Caesar | https://github.com/Persian-Caesar | https://dsc.gg/persian-caesar * * If you encounter any issues or need assistance with this code, * please make sure to credit "Persian Caesar" in your documentation or communications. */