discord-player-play-dl/src/Player.ts

291 lines
9.5 KiB
TypeScript
Raw Normal View History

2021-04-06 17:58:46 +05:00
import { EventEmitter } from 'events';
2021-05-01 12:59:42 +05:00
import { Client, Collection, Snowflake, Collector, Message } from 'discord.js';
import {
PlayerOptions as PlayerOptionsType,
} from './types/types';
2021-04-06 17:58:46 +05:00
import Util from './utils/Util';
import AudioFilters from './utils/AudioFilters';
2021-04-06 20:38:17 +05:00
import { Queue } from './Structures/Queue';
2021-04-08 19:42:08 +05:00
import { ExtractorModel } from './Structures/ExtractorModel';
2021-04-04 22:36:40 +05:00
2021-04-21 14:14:04 +05:00
/**
* The Player class
* @extends {EventEmitter}
*/
2021-04-06 20:38:17 +05:00
export class Player extends EventEmitter {
2021-04-21 12:09:16 +05:00
public client: Client;
public options: PlayerOptionsType;
2021-04-06 17:55:29 +05:00
public filters: typeof AudioFilters;
2021-04-09 17:59:14 +05:00
/**
* The collection of queues in this player
2021-04-21 14:10:44 +05:00
* @type {DiscordCollection<Queue>}
2021-04-09 17:59:14 +05:00
*/
2021-04-09 15:04:36 +05:00
public queues = new Collection<Snowflake, Queue>();
2021-04-09 17:59:14 +05:00
/**
* The extractor model collection
2021-04-21 14:10:44 +05:00
* @type {DiscordCollection<ExtractorModel>}
2021-04-09 17:59:14 +05:00
*/
2021-04-08 18:03:04 +05:00
public Extractors = new Collection<string, ExtractorModel>();
2021-04-04 22:36:40 +05:00
2021-04-09 17:59:14 +05:00
/**
* Creates new Player instance
2021-04-21 12:09:16 +05:00
* @param {DiscordClient} client The discord.js client
2021-04-21 12:15:09 +05:00
* @param {PlayerOptions} options Player options
2021-04-09 17:59:14 +05:00
*/
constructor(client: Client, options?: PlayerOptionsType) {
2021-04-04 22:36:40 +05:00
super();
2021-04-21 14:10:44 +05:00
/**
* The discord client that instantiated this player
* @name Player#client
* @type {DiscordClient}
* @readonly
*/
2021-04-06 17:58:46 +05:00
Object.defineProperty(this, 'client', {
2021-04-04 22:36:40 +05:00
value: client,
enumerable: false
});
// check FFmpeg
void Util.alertFFmpeg();
2021-04-23 23:27:51 +05:00
}
2021-05-01 12:59:42 +05:00
2021-04-04 22:44:45 +05:00
}
2021-04-06 20:38:17 +05:00
export default Player;
2021-04-19 18:32:10 +05:00
/**
* Emitted when a track starts
* @event Player#trackStart
2021-04-21 12:09:16 +05:00
* @param {DiscordMessage} message The message
2021-04-21 11:35:57 +05:00
* @param {Track} track The track
* @param {Queue} queue The queue
2021-04-19 18:32:10 +05:00
*/
/**
* Emitted when a playlist is started
* @event Player#queueCreate
2021-04-21 12:09:16 +05:00
* @param {DiscordMessage} message The message
2021-04-21 11:35:57 +05:00
* @param {Queue} queue The queue
2021-04-19 18:32:10 +05:00
*/
/**
* Emitted when the bot is awaiting search results
* @event Player#searchResults
2021-04-21 12:09:16 +05:00
* @param {DiscordMessage} message The message
2021-04-21 11:52:59 +05:00
* @param {String} query The query
2021-04-21 11:35:57 +05:00
* @param {Track[]} tracks The tracks
2021-04-21 15:48:07 +05:00
* @param {DiscordCollector} collector The collector
2021-04-19 18:32:10 +05:00
*/
/**
* Emitted when the user has sent an invalid response for search results
* @event Player#searchInvalidResponse
2021-04-21 12:09:16 +05:00
* @param {DiscordMessage} message The message
2021-04-21 11:52:59 +05:00
* @param {String} query The query
2021-04-21 11:35:57 +05:00
* @param {Track[]} tracks The tracks
2021-04-21 11:52:59 +05:00
* @param {String} invalidResponse The `invalidResponse` string
2021-04-21 15:48:07 +05:00
* @param {DiscordCollector} collector The collector
2021-04-19 18:32:10 +05:00
*/
/**
* Emitted when the bot has stopped awaiting search results (timeout)
* @event Player#searchCancel
2021-04-21 12:09:16 +05:00
* @param {DiscordMessage} message The message
2021-04-21 11:52:59 +05:00
* @param {String} query The query
2021-04-21 11:35:57 +05:00
* @param {Track[]} tracks The tracks
2021-04-19 18:32:10 +05:00
*/
/**
* Emitted when the bot can't find related results to the query
* @event Player#noResults
2021-04-21 12:09:16 +05:00
* @param {DiscordMessage} message The message
2021-04-21 11:52:59 +05:00
* @param {String} query The query
2021-04-19 18:32:10 +05:00
*/
/**
* Emitted when the bot is disconnected from the channel
* @event Player#botDisconnect
2021-04-21 12:09:16 +05:00
* @param {DiscordMessage} message The message
2021-04-19 18:32:10 +05:00
*/
/**
* Emitted when the channel of the bot is empty
* @event Player#channelEmpty
2021-04-21 12:09:16 +05:00
* @param {DiscordMessage} message The message
2021-04-21 11:35:57 +05:00
* @param {Queue} queue The queue
2021-04-19 18:32:10 +05:00
*/
/**
* Emitted when the queue of the server is ended
* @event Player#queueEnd
2021-04-21 12:09:16 +05:00
* @param {DiscordMessage} message The message
2021-04-21 11:35:57 +05:00
* @param {Queue} queue The queue
2021-04-19 18:32:10 +05:00
*/
/**
* Emitted when a track is added to the queue
* @event Player#trackAdd
2021-04-21 12:09:16 +05:00
* @param {DiscordMessage} message The message
2021-04-21 11:35:57 +05:00
* @param {Queue} queue The queue
* @param {Track} track The track
2021-04-19 18:32:10 +05:00
*/
/**
* Emitted when a playlist is added to the queue
* @event Player#playlistAdd
2021-04-21 12:09:16 +05:00
* @param {DiscordMessage} message The message
2021-04-21 11:35:57 +05:00
* @param {Queue} queue The queue
* @param {Object} playlist The playlist
2021-04-19 18:32:10 +05:00
*/
/**
* Emitted when an error is triggered
* @event Player#error
2021-04-21 11:52:59 +05:00
* @param {String} error It can be `NotConnected`, `UnableToJoin`, `NotPlaying`, `ParseError`, `LiveVideo` or `VideoUnavailable`.
2021-04-21 12:09:16 +05:00
* @param {DiscordMessage} message The message
2021-04-19 18:32:10 +05:00
*/
/**
* Emitted when discord-player attempts to parse playlist contents (mostly soundcloud playlists)
* @event Player#playlistParseStart
* @param {Object} playlist Raw playlist (unparsed)
2021-04-21 12:09:16 +05:00
* @param {DiscordMessage} message The message
2021-04-19 18:32:10 +05:00
*/
/**
* Emitted when discord-player finishes parsing playlist contents (mostly soundcloud playlists)
* @event Player#playlistParseEnd
* @param {Object} playlist The playlist data (parsed)
2021-04-21 12:09:16 +05:00
* @param {DiscordMessage} message The message
2021-04-19 18:32:28 +05:00
*/
2021-04-21 11:52:59 +05:00
/**
* @typedef {Object} PlayerOptions
* @property {Boolean} [leaveOnEnd=false] If it should leave on queue end
* @property {Number} [leaveOnEndCooldown=0] Time in ms to wait before executing `leaveOnEnd`
* @property {Boolean} [leaveOnStop=false] If it should leave on stop command
* @property {Boolean} [leaveOnEmpty=false] If it should leave on empty voice channel
* @property {Number} [leaveOnEmptyCooldown=0] Time in ms to wait before executing `leaveOnEmpty`
* @property {Boolean} [autoSelfDeaf=false] If it should set the client to `self deaf` mode on joining
* @property {Boolean} [enableLive=false] If it should enable live videos support
* @property {YTDLDownloadOptions} [ytdlDownloadOptions={}] The download options passed to `ytdl-core`
* @property {Boolean} [useSafeSearch=false] If it should use `safe search` method for youtube searches
2021-04-21 17:17:43 +05:00
* @property {Boolean} [disableAutoRegister=false] If it should disable auto-registeration of `@discord-player/extractor`
2021-04-21 11:52:59 +05:00
*/
/**
2021-04-21 17:08:24 +05:00
* The type of Track source, either:
* * `soundcloud` - a stream from SoundCloud
* * `youtube` - a stream from YouTube
* * `arbitrary` - arbitrary stream
* @typedef {String} TrackSource
2021-04-21 11:52:59 +05:00
*/
/**
* @typedef {Object} TrackData
* @property {String} title The title
* @property {String} description The description
* @property {String} author The author
* @property {String} url The url
* @property {String} duration The duration
* @property {Number} views The view count
2021-04-21 12:09:16 +05:00
* @property {DiscordUser} requestedBy The user who requested this track
2021-04-21 11:52:59 +05:00
* @property {Boolean} fromPlaylist If this track came from a playlist
* @property {TrackSource} [source] The track source
* @property {string|Readable} [engine] The stream engine
* @property {Boolean} [live=false] If this track is livestream instance
*/
/**
* @typedef {Object} QueueFilters
* The FFmpeg Filters
*/
/**
2021-04-21 17:08:24 +05:00
* The query type, either:
* * `soundcloud_track` - a SoundCloud Track
* * `soundcloud_playlist` - a SoundCloud Playlist
* * `spotify_song` - a Spotify Song
* * `spotify_album` - a Spotify album
* * `spotify_playlist` - a Spotify playlist
* * `youtube_video` - a YouTube video
* * `youtube_playlist` - a YouTube playlist
* * `vimeo` - a Vimeo link
* * `facebook` - a Facebook link
* * `reverbnation` - a Reverbnation link
* * `attachment` - an attachment link
* * `youtube_search` - a YouTube search keyword
* @typedef {String} QueryType The query type
2021-04-21 11:52:59 +05:00
*/
/**
* @typedef {Object} ExtractorModelData
* @property {String} title The title
* @property {Number} duration The duration in ms
* @property {String} thumbnail The thumbnail url
* @property {string|Readable} engine The audio engine
* @property {Number} views The views count of this stream
* @property {String} author The author
* @property {String} description The description
* @property {String} url The url
* @property {String} [version='0.0.0'] The extractor version
* @property {Boolean} [important=false] Mark as important
*/
/**
* @typedef {Object} PlayerProgressbarOptions
* @property {Boolean} [timecodes] If it should return progres bar with time codes
* @property {Boolean} [queue] if it should return the progress bar of the whole queue
* @property {Number} [length] The length of progress bar to build
*/
/**
* @typedef {Object} LyricsData
* @property {String} title The title of the lyrics
* @property {Number} id The song id
* @property {String} thumbnail The thumbnail
* @property {String} image The image
* @property {String} url The url
* @property {Object} artist The artust info
* @property {String} [artist.name] The name of the artist
* @property {Number} [artist.id] The ID of the artist
* @property {String} [artist.url] The profile link of the artist
* @property {String} [artist.image] The artist image url
* @property {String?} lyrics The lyrics
*/
/**
* @typedef {Object} PlayerStats
* @property {Number} uptime The uptime in ms
* @property {Number} connections The number of connections
* @property {Number} users The number of users
* @property {Number} queues The number of queues
* @property {Number} extractors The number of custom extractors registered
* @property {Object} versions The versions metadata
* @property {String} [versions.ffmpeg] The ffmpeg version
* @property {String} [versions.node] The node version
* @property {String} [versions.v8] The v8 JavaScript engine version
* @property {Object} system The system data
* @property {String} [system.arch] The system arch
2021-04-21 17:08:24 +05:00
* @property {String} [system.platform] The system platform
2021-04-21 11:52:59 +05:00
* @property {Number} [system.cpu] The cpu count
* @property {Object} [system.memory] The memory info
* @property {String} [system.memory.total] The total memory
* @property {String} [system.memory.usage] The memory usage
* @property {String} [system.memory.rss] The memory usage in RSS
* @property {String} [system.memory.arrayBuffers] The memory usage in ArrayBuffers
* @property {Number} [system.uptime] The system uptime
*/
/**
* @typedef {Object} TimeData
* @property {Number} days The time in days
* @property {Number} hours The time in hours
* @property {Number} minutes The time in minutes
* @property {Number} seconds The time in seconds
2021-04-21 13:30:54 +05:00
*/