@jadestudios/discord-music-player
Version:
Complete framework to facilitate music commands using discord.js v13
370 lines (369 loc) • 16.2 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Utils = exports.ProviderList = void 0;
const __1 = require("..");
const isomorphic_unfetch_1 = __importDefault(require("isomorphic-unfetch"));
const ytsr_1 = __importDefault(require("@distube/ytsr"));
const MD_Apple_1 = require("./MD_Apple");
const youtubei_1 = require("youtubei");
const discord_js_1 = require("discord.js");
let YouTube = new youtubei_1.Client();
const { getData, getPreview } = require('spotify-url-info')(isomorphic_unfetch_1.default);
var ProviderList;
(function (ProviderList) {
ProviderList[ProviderList["YOUTUBE"] = 0] = "YOUTUBE";
ProviderList[ProviderList["SPOTIFY"] = 1] = "SPOTIFY";
ProviderList[ProviderList["APPLE"] = 2] = "APPLE";
ProviderList[ProviderList["NONE"] = 3] = "NONE";
})(ProviderList || (exports.ProviderList = ProviderList = {}));
class Utils {
/**
* Checks if url is valid and gets which provider it is
* @param url
* @returns {boolean, ProviderList}
*/
static isSongLink(url) {
if (this.regexList.Spotify.test(url))
return [true, ProviderList.SPOTIFY];
if (this.regexList.YouTubeVideo.test(url))
return [true, ProviderList.YOUTUBE];
if (this.regexList.Apple.test(url))
return [true, ProviderList.APPLE];
return [false, ProviderList.NONE];
}
static isListLink(url) {
if (this.regexList.SpotifyPlaylist.test(url))
return [true, ProviderList.SPOTIFY];
if (this.regexList.YouTubePlaylist.test(url))
return [true, ProviderList.YOUTUBE];
if (this.regexList.ApplePlaylist.test(url))
return [true, ProviderList.APPLE];
return [false, ProviderList.NONE];
}
/**
* Get ID from YouTube link
* @param {string} url
* @returns {?string}
*/
static parseVideo(url) {
const match = url.match(this.regexList.YouTubeVideoID);
return match ? match[7] : null;
}
/**
* Get timecode from YouTube link
* @param {string} url
* @returns {?string}
*/
static parseVideoTimecode(url) {
const match = url.match(this.regexList.YouTubeVideoTime);
return match ? match[3] : null;
}
/**
* Get ID from Playlist link
* @param {string} url
* @returns {?string}
*/
static parsePlaylist(url) {
const match = url.match(this.regexList.YouTubePlaylistID);
return match ? match[1] : null;
}
/**
* Search for Songs
* @param {string} Search
* @param {PlayOptions} [SOptions=DefaultPlayOptions]
* @param {Queue} Queue
* @param {number} [Limit=1]
* @return {Promise<Song[]>}
*/
static async search(Search, SOptions = __1.DefaultPlayOptions, Queue, Limit = 5) {
SOptions = Object.assign({}, __1.DefaultPlayOptions, SOptions);
try {
let Result = await (0, ytsr_1.default)(Search, {
limit: Limit,
});
let songs = Result.items.map(item => {
if (item?.type?.toLowerCase() !== 'video')
return null;
return new __1.Song({
name: item.name,
url: item.url,
duration: item.duration,
author: item.author ? item.author.name : "N/A",
isLive: item.isLive,
thumbnail: item.thumbnail,
}, Queue, SOptions.requestedBy);
}).filter(I => I);
return songs;
}
catch (e) {
throw __1.DMPErrors.SEARCH_NULL;
}
}
/**
* Search for Song via link
* @param {string} Search
* @param {PlayOptions} SOptions
* @param {Queue} Queue
* @return {Promise<Song>}
*/
static async link(Search, SOptions = __1.DefaultPlayOptions, Queue) {
const [isSong, provider] = this.isSongLink(Search);
if (!isSong)
return null;
switch (provider) {
case ProviderList.APPLE:
try {
let AppleResult = await (0, MD_Apple_1.getApple)(Search, MD_Apple_1.AppleLinkType.Song);
if (AppleResult) {
if (AppleResult instanceof MD_Apple_1.AppleTrack) {
let SearchResult = await this.search(`${AppleResult.artist} - ${AppleResult.title}`, SOptions, Queue);
return SearchResult[0];
}
}
}
catch (e) {
throw __1.DMPErrors.INVALID_APPLE;
}
break;
case ProviderList.SPOTIFY:
try {
let SpotifyResult = await getPreview(Search);
let SearchResult = await this.search(`${SpotifyResult.artist} - ${SpotifyResult.title}`, SOptions, Queue);
return SearchResult[0];
}
catch (e) {
throw __1.DMPErrors.INVALID_SPOTIFY;
}
break;
case ProviderList.YOUTUBE:
let VideoID = this.parseVideo(Search);
if (!VideoID)
throw __1.DMPErrors.SEARCH_NULL;
YouTube = new youtubei_1.Client();
let VideoResult = await YouTube.getVideo(VideoID);
if (!VideoResult)
throw __1.DMPErrors.SEARCH_NULL;
let VideoTimecode = this.parseVideoTimecode(Search);
return new __1.Song({
name: VideoResult.title,
url: Search,
duration: this.msToTime((VideoResult.duration ?? 0) * 1000),
author: VideoResult.channel.name,
isLive: VideoResult.isLiveContent,
thumbnail: VideoResult.thumbnails.best,
seekTime: SOptions.timecode && VideoTimecode ? Number(VideoTimecode) * 1000 : null,
}, Queue, SOptions.requestedBy);
break;
default:
return null;
}
}
/**
* Gets the best result of a Search
* @param {Song|string} Search
* @param {PlayOptions} SOptions
* @param {Queue} Queue
* @return {Promise<Song>}
*/
static async best(Search, SOptions = __1.DefaultPlayOptions, Queue) {
let _Song;
if (Search instanceof __1.Song)
return Search;
_Song = await this.link(Search, SOptions, Queue);
if (!_Song) {
const _Song_Array = (await this.search(Search, SOptions, Queue));
let i = 0;
_Song = _Song_Array[i];
while (!_Song && i < _Song_Array.length) { //Makes sure that a valid song is chosen from first 3 results
i++;
_Song = _Song_Array[i];
}
}
return _Song; //Possibly undefined still
}
/**
* Search for Playlist
* @param {string} Search
* @param {PlaylistOptions} SOptions
* @param {Queue} Queue
* @return {Promise<Playlist>}
*/
static async playlist(Search, SOptions = __1.DefaultPlaylistOptions, Queue) {
if (Search instanceof __1.Playlist)
return Search;
let Limit = SOptions.maxSongs ?? -1;
const [isList, Provider] = this.isListLink(Search);
if (!isList)
throw __1.DMPErrors.INVALID_PLAYLIST;
switch (Provider) {
case ProviderList.APPLE:
let AppleResultData = await (0, MD_Apple_1.getApple)(Search, MD_Apple_1.AppleLinkType.Album).catch(() => null);
if (!AppleResultData)
throw __1.DMPErrors.INVALID_PLAYLIST;
if (!(AppleResultData instanceof MD_Apple_1.AppleTrackList))
throw __1.DMPErrors.INVALID_PLAYLIST;
let AppleResult = {
name: 'Apple Playlist',
author: 'N/A',
url: Search,
songs: [],
type: 'playlist'
};
AppleResult.songs = (await Promise.all(AppleResultData.tracks.map(async (track, index) => {
if (Limit !== -1 && index >= Limit)
return null;
const Result = await this.search(`${track.artist} - ${track.title}`, SOptions, Queue).catch(() => null);
if (Result && Result[0]) {
Result[0].data = SOptions.data;
return Result[0];
}
else
return null;
})))
.filter((V) => V !== null);
if (AppleResult.songs.length === 0)
throw __1.DMPErrors.INVALID_PLAYLIST;
if (SOptions.shuffle)
AppleResult.songs = this.shuffle(AppleResult.songs);
return new __1.Playlist(AppleResult, Queue, SOptions.requestedBy);
break;
case ProviderList.SPOTIFY:
let SpotifyResultData = await getData(Search).catch(() => null);
if (!SpotifyResultData || !['playlist', 'album'].includes(SpotifyResultData.type))
throw __1.DMPErrors.INVALID_PLAYLIST;
let SpotifyResult = {
name: SpotifyResultData.name,
author: SpotifyResultData.subtitle,
url: Search,
songs: [],
type: SpotifyResultData.type
};
SpotifyResult.songs = (await Promise.all(SpotifyResultData.trackList.map(async (track, index) => {
if (Limit !== -1 && index >= Limit)
return null;
const Result = await this.search(`${track.subtitle} - ${track.title}`, SOptions, Queue).catch(() => null);
if (Result && Result[0]) {
Result[0].data = SOptions.data;
return Result[0];
}
else
return null;
})))
.filter((V) => V !== null);
if (SpotifyResult.songs.length === 0)
throw __1.DMPErrors.INVALID_PLAYLIST;
if (SOptions.shuffle)
SpotifyResult.songs = this.shuffle(SpotifyResult.songs);
return new __1.Playlist(SpotifyResult, Queue, SOptions.requestedBy);
break;
case ProviderList.YOUTUBE:
let PlaylistID = this.parsePlaylist(Search);
if (!PlaylistID)
throw __1.DMPErrors.INVALID_PLAYLIST;
YouTube = new youtubei_1.Client();
let YouTubeResultData = await YouTube.getPlaylist(PlaylistID);
if (!YouTubeResultData || Object.keys(YouTubeResultData).length === 0)
throw __1.DMPErrors.INVALID_PLAYLIST;
let YouTubeResult = {
name: YouTubeResultData.title,
author: YouTubeResultData instanceof youtubei_1.Playlist ? YouTubeResultData.channel?.name ?? 'YouTube Mix' : 'YouTube Mix',
url: Search,
songs: [],
type: 'playlist'
};
if (YouTubeResultData instanceof youtubei_1.Playlist && YouTubeResultData.videoCount > 100 && (Limit === -1 || Limit > 100))
await YouTubeResultData.videos.next(Math.floor((Limit === -1 || Limit > YouTubeResultData.videoCount ? YouTubeResultData.videoCount : Limit - 1) / 100));
if (YouTubeResultData.videos instanceof youtubei_1.PlaylistVideos) {
//Needs VideoCompact[] for map to work below
YouTubeResultData.videos = YouTubeResultData.videos.items;
}
YouTubeResult.songs = YouTubeResultData.videos.map((video, index) => {
if (Limit !== -1 && index >= Limit)
return null;
let song = new __1.Song({
name: video.title,
url: `https://youtube.com/watch?v=${video.id}`,
duration: this.msToTime((video.duration ?? 0) * 1000),
author: video.channel.name,
isLive: video.isLive,
thumbnail: video.thumbnails.best,
}, Queue, SOptions.requestedBy);
song.data = SOptions.data;
return song;
})
.filter((V) => V !== null);
if (YouTubeResult.songs.length === 0)
throw __1.DMPErrors.INVALID_PLAYLIST;
if (SOptions.shuffle)
YouTubeResult.songs = this.shuffle(YouTubeResult.songs);
return new __1.Playlist(YouTubeResult, Queue, SOptions.requestedBy);
break;
default:
throw __1.DMPErrors.INVALID_PLAYLIST;
}
}
/**
* Shuffles an array
* @param {any[]} array
* @returns {any[]}
*/
static shuffle(array) {
if (!Array.isArray(array))
return [];
const clone = [...array];
const shuffled = [];
while (clone.length > 0)
shuffled.push(clone.splice(Math.floor(Math.random() * clone.length), 1)[0]);
return shuffled;
}
/**
* Converts milliseconds to duration (HH:MM:SS)
* @returns {string}
*/
static msToTime(duration) {
const seconds = Math.floor(duration / 1000 % 60);
const minutes = Math.floor(duration / 60000 % 60);
const hours = Math.floor(duration / 3600000);
const secondsPad = `${seconds}`.padStart(2, '0');
const minutesPad = `${minutes}`.padStart(2, '0');
const hoursPad = `${hours}`.padStart(2, '0');
return `${hours ? `${hoursPad}:` : ''}${minutesPad}:${secondsPad}`;
}
/**
* Converts duration (HH:MM:SS) to milliseconds
* @returns {number}
*/
static timeToMs(duration) {
return duration.split(':')
.reduceRight((prev, curr, i, arr) => prev + parseInt(curr) * 60 ** (arr.length - 1 - i), 0) * 1000;
}
static isVoiceChannel(Channel) {
let type = Channel.type;
if (typeof type === 'string')
return ['GUILD_VOICE', 'GUILD_STAGE_VOICE'].includes(type);
else
return [discord_js_1.ChannelType.GuildVoice, discord_js_1.ChannelType.GuildStageVoice].includes(type);
}
static isStageVoiceChannel(Channel) {
let type = Channel.type;
if (typeof type === 'string')
return type === 'GUILD_STAGE_VOICE';
else
return type === discord_js_1.ChannelType.GuildStageVoice;
}
}
exports.Utils = Utils;
Utils.regexList = {
YouTubeVideo: /^((?:https?:)\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))((?!channel)(?!user)\/(?:[\w\-]+\?v=|embed\/|v\/)?)((?!channel)(?!user)[\w\-]+)/,
YouTubeVideoTime: /(([?]|[&])t=(\d+))/,
YouTubeVideoID: /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/,
YouTubePlaylist: /^((?:https?:)\/\/)?((?:www|m)\.)?((?:youtube\.com)).*(youtu.be\/|list=)([^#&?]*).*/,
YouTubePlaylistID: /[&?]list=([^&]+)/,
Spotify: /https?:\/\/(?:embed\.|open\.)(?:spotify\.com\/)(?:track\/|\?uri=spotify:track:)((\w|-)+)(?:(?=\?)(?:[?&]foo=(\d*)(?=[&#]|$)|(?![?&]foo=)[^#])+)?(?=#|$)/,
SpotifyPlaylist: /https?:\/\/(?:embed\.|open\.)(?:spotify\.com\/)(?:(album|playlist)\/|\?uri=spotify:playlist:)((\w|-)+)(?:(?=\?)(?:[?&]foo=(\d*)(?=[&#]|$)|(?![?&]foo=)[^#])+)?(?=#|$)/,
Apple: /https?:\/\/music\.apple\.com\/[a-z]{2}\/album\/[\S]+?\/\d+?\?i=([0-9]+)/,
ApplePlaylist: /https?:\/\/music\.apple\.com\/[a-z]{2}\/(playlist|album)\//,
};