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

352 lines
15 KiB
TypeScript
Raw Normal View History

2021-06-13 21:47:04 +05:00
import { Client, Collection, Guild, Snowflake, User, VoiceState } from "discord.js";
2021-06-11 15:32:22 +05:00
import { TypedEmitter as EventEmitter } from "tiny-typed-emitter";
import { Queue } from "./Structures/Queue";
2021-06-11 16:50:43 +05:00
import { VoiceUtils } from "./VoiceInterface/VoiceUtils";
2021-06-13 17:03:20 +05:00
import { PlayerEvents, PlayerOptions, QueryType, SearchOptions } from "./types/types";
2021-06-11 23:19:52 +05:00
import Track from "./Structures/Track";
import { QueryResolver } from "./utils/QueryResolver";
import YouTube from "youtube-sr";
2021-06-13 17:03:20 +05:00
import { Util } from "./utils/Util";
2021-06-13 18:09:25 +05:00
import Spotify from "spotify-url-info";
2021-06-13 17:03:20 +05:00
// @ts-ignore
import { Client as SoundCloud } from "soundcloud-scraper";
2021-06-13 19:31:27 +05:00
import { Playlist } from "./Structures/Playlist";
2021-06-13 17:03:20 +05:00
const soundcloud = new SoundCloud();
2021-06-11 15:32:22 +05:00
2021-06-11 23:19:52 +05:00
class DiscordPlayer extends EventEmitter<PlayerEvents> {
2021-06-11 15:32:22 +05:00
public readonly client: Client;
public readonly queues = new Collection<Snowflake, Queue>();
2021-06-11 16:50:43 +05:00
public readonly voiceUtils = new VoiceUtils();
2021-06-11 15:32:22 +05:00
2021-06-13 15:40:41 +05:00
/**
* Creates new Discord Player
* @param {Discord.Client} client The Discord Client
*/
2021-06-11 15:32:22 +05:00
constructor(client: Client) {
super();
2021-06-13 15:40:41 +05:00
/**
* The discord.js client
* @type {Discord.Client}
*/
2021-06-11 15:32:22 +05:00
this.client = client;
2021-06-13 21:47:04 +05:00
this.client.on("voiceStateUpdate", this._handleVoiceState.bind(this));
}
private _handleVoiceState(oldState: VoiceState, newState: VoiceState): void {
const queue = this.getQueue(oldState.guild.id);
if (!queue) return;
if (oldState.member.id === this.client.user.id && !newState.channelID) {
queue.destroy();
return void this.emit("botDisconnect", queue);
}
if (!queue.options.leaveOnEmpty || !queue.connection || !queue.connection.channel) return;
if (!oldState.channelID || newState.channelID) {
const emptyTimeout = queue._cooldownsTimeout.get(`empty_${oldState.guild.id}`);
const channelEmpty = Util.isVoiceEmpty(queue.connection.channel);
if (!channelEmpty && emptyTimeout) {
clearTimeout(emptyTimeout);
queue._cooldownsTimeout.delete(`empty_${oldState.guild.id}`);
}
} else {
if (!Util.isVoiceEmpty(queue.connection.channel)) return;
const timeout = setTimeout(() => {
if (!Util.isVoiceEmpty(queue.connection.channel)) return;
if (!this.queues.has(queue.guild.id)) return;
queue.destroy();
this.emit("channelEmpty", queue);
}, queue.options.leaveOnEmptyCooldown || 0);
queue._cooldownsTimeout.set(`empty_${oldState.guild.id}`, timeout);
}
2021-06-11 15:32:22 +05:00
}
2021-06-13 15:40:41 +05:00
/**
* Creates a queue for a guild if not available, else returns existing queue
* @param {Discord.Guild} guild The guild
* @param {PlayerOptions} queueInitOptions Queue init options
* @returns {Queue}
*/
2021-06-13 17:43:36 +05:00
createQueue<T = unknown>(guild: Guild, queueInitOptions?: PlayerOptions & { metadata?: T }): Queue<T> {
2021-06-12 11:37:41 +05:00
if (this.queues.has(guild.id)) return this.queues.get(guild.id) as Queue<T>;
2021-06-13 15:40:41 +05:00
const _meta = queueInitOptions.metadata;
delete queueInitOptions["metadata"];
2021-06-11 15:32:22 +05:00
const queue = new Queue(this, guild, queueInitOptions);
2021-06-13 15:40:41 +05:00
queue.metadata = _meta;
2021-06-11 15:32:22 +05:00
this.queues.set(guild.id, queue);
2021-06-12 11:37:41 +05:00
return queue as Queue<T>;
2021-06-11 15:32:22 +05:00
}
2021-06-13 15:40:41 +05:00
/**
* Returns the queue if available
* @param {Discord.Snowflake} guild The guild id
* @returns {Queue}
*/
2021-06-12 11:37:41 +05:00
getQueue<T = unknown>(guild: Snowflake) {
return this.queues.get(guild) as Queue<T>;
2021-06-11 15:32:22 +05:00
}
2021-06-11 19:57:49 +05:00
2021-06-13 15:40:41 +05:00
/**
* Deletes a queue and returns deleted queue object
* @param {Discord.Snowflake} guild The guild id to remove
* @returns {Queue}
*/
2021-06-13 13:06:19 +05:00
deleteQueue<T = unknown>(guild: Snowflake) {
const prev = this.getQueue<T>(guild);
2021-06-13 13:11:54 +05:00
try {
prev.destroy();
} catch {}
2021-06-13 13:06:19 +05:00
this.queues.delete(guild);
2021-06-13 13:11:54 +05:00
2021-06-13 13:06:19 +05:00
return prev;
}
2021-06-11 23:19:52 +05:00
/**
* Search tracks
* @param {string|Track} query The search query
2021-06-13 15:40:41 +05:00
* @param {Discord.User} requestedBy The person who requested track search
2021-06-14 11:44:32 +05:00
* @returns {Promise<{playlist?: Playlist; tracks: Track[]}>}
2021-06-11 23:19:52 +05:00
*/
2021-06-13 17:03:20 +05:00
async search(query: string | Track, options: SearchOptions) {
2021-06-14 11:44:32 +05:00
if (query instanceof Track) return { playlist: null, tracks: [query] };
2021-06-13 17:03:20 +05:00
if (!options) throw new Error("DiscordPlayer#search needs search options!");
if (!("searchEngine" in options)) options.searchEngine = QueryType.AUTO;
2021-06-11 23:19:52 +05:00
// @todo: add extractors
2021-06-13 17:03:20 +05:00
const qt = options.searchEngine === QueryType.AUTO ? QueryResolver.resolve(query) : options.searchEngine;
2021-06-11 23:19:52 +05:00
switch (qt) {
2021-06-11 23:39:21 +05:00
case QueryType.YOUTUBE_SEARCH: {
const videos = await YouTube.search(query, {
2021-06-11 23:19:52 +05:00
type: "video"
2021-06-13 19:31:27 +05:00
}).catch(() => {});
2021-06-14 11:44:32 +05:00
if (!videos) return { playlist: null, tracks: [] };
2021-06-11 23:19:52 +05:00
2021-06-13 19:31:27 +05:00
const tracks = videos.map((m) => {
2021-06-12 00:18:53 +05:00
(m as any).source = "youtube";
return new Track(this, {
title: m.title,
description: m.description,
author: m.channel?.name,
url: m.url,
2021-06-13 17:03:20 +05:00
requestedBy: options.requestedBy,
2021-06-12 00:18:53 +05:00
thumbnail: m.thumbnail?.displayThumbnailURL("maxresdefault"),
views: m.views,
duration: m.durationFormatted,
raw: m
2021-06-11 23:39:21 +05:00
});
2021-06-12 00:18:53 +05:00
});
2021-06-13 19:31:27 +05:00
2021-06-14 11:44:32 +05:00
return { playlist: null, tracks };
2021-06-11 23:19:52 +05:00
}
2021-06-13 17:03:20 +05:00
case QueryType.SOUNDCLOUD_TRACK:
case QueryType.SOUNDCLOUD_SEARCH: {
const result: any[] = QueryResolver.resolve(query) === QueryType.SOUNDCLOUD_TRACK ? [{ url: query }] : await soundcloud.search(query, "track").catch(() => {});
2021-06-14 11:44:32 +05:00
if (!result || !result.length) return { playlist: null, tracks: [] };
2021-06-13 17:03:20 +05:00
const res: Track[] = [];
for (const r of result) {
const trackInfo = await soundcloud.getSongInfo(r.url).catch(() => {});
if (!trackInfo) continue;
const track = new Track(this, {
title: trackInfo.title,
url: trackInfo.url,
duration: Util.buildTimeCode(Util.parseMS(trackInfo.duration)),
description: trackInfo.description,
thumbnail: trackInfo.thumbnail,
views: trackInfo.playCount,
author: trackInfo.author.name,
requestedBy: options.requestedBy,
source: "soundcloud",
engine: trackInfo
});
res.push(track);
}
2021-06-14 11:44:32 +05:00
return { playlist: null, tracks: res };
2021-06-13 17:03:20 +05:00
}
2021-06-13 18:09:25 +05:00
case QueryType.SPOTIFY_SONG: {
const spotifyData = await Spotify.getData(query).catch(() => {});
2021-06-14 11:44:32 +05:00
if (!spotifyData) return { playlist: null, tracks: [] };
2021-06-13 18:09:25 +05:00
const spotifyTrack = new Track(this, {
title: spotifyData.name,
description: spotifyData.description ?? "",
author: spotifyData.artists[0]?.name ?? "Unknown Artist",
url: spotifyData.external_urls?.spotify ?? query,
thumbnail:
spotifyData.album?.images[0]?.url ?? spotifyData.preview_url?.length
? `https://i.scdn.co/image/${spotifyData.preview_url?.split("?cid=")[1]}`
: "https://www.scdn.co/i/_global/twitter_card-default.jpg",
duration: Util.buildTimeCode(Util.parseMS(spotifyData.duration_ms)),
views: 0,
requestedBy: options.requestedBy,
source: "spotify"
});
2021-06-14 11:44:32 +05:00
return { playlist: null, tracks: [spotifyTrack] };
2021-06-13 19:31:27 +05:00
}
case QueryType.SPOTIFY_PLAYLIST:
case QueryType.SPOTIFY_ALBUM: {
const spotifyPlaylist = await Spotify.getData(query).catch(() => {});
2021-06-14 11:44:32 +05:00
if (!spotifyPlaylist) return { playlist: null, tracks: [] };
2021-06-13 19:31:27 +05:00
const playlist = new Playlist(this, {
title: spotifyPlaylist.name ?? spotifyPlaylist.title,
description: spotifyPlaylist.description ?? "",
thumbnail: spotifyPlaylist.images[0]?.url ?? "https://www.scdn.co/i/_global/twitter_card-default.jpg",
type: spotifyPlaylist.type,
source: "spotify",
author:
spotifyPlaylist.type !== "playlist"
? {
name: spotifyPlaylist.artists[0]?.name ?? "Unknown Artist",
url: spotifyPlaylist.artists[0]?.external_urls?.spotify ?? null
}
: {
name: spotifyPlaylist.owner?.display_name ?? spotifyPlaylist.owner?.id ?? "Unknown Artist",
url: spotifyPlaylist.owner?.external_urls?.spotify ?? null
},
tracks: [],
id: spotifyPlaylist.id,
2021-06-14 11:44:32 +05:00
url: spotifyPlaylist.external_urls?.spotify ?? query,
rawPlaylist: spotifyPlaylist
2021-06-13 19:31:27 +05:00
});
if (spotifyPlaylist.type !== "playlist") {
playlist.tracks = spotifyPlaylist.tracks.items.map((m: any) => {
const data = new Track(this, {
title: m.name ?? "",
description: m.description ?? "",
author: m.artists[0]?.name ?? "Unknown Artist",
url: m.external_urls?.spotify ?? query,
thumbnail: spotifyPlaylist.images[0]?.url ?? "https://www.scdn.co/i/_global/twitter_card-default.jpg",
duration: Util.buildTimeCode(Util.parseMS(m.duration_ms)),
views: 0,
requestedBy: options.requestedBy,
playlist,
source: "spotify"
});
return data;
}) as Track[];
} else {
playlist.tracks = spotifyPlaylist.tracks.items.map((m: any) => {
const data = new Track(this, {
title: m.track.name ?? "",
description: m.track.description ?? "",
author: m.track.artists[0]?.name ?? "Unknown Artist",
url: m.track.external_urls?.spotify ?? query,
thumbnail: m.track.album?.images[0]?.url ?? "https://www.scdn.co/i/_global/twitter_card-default.jpg",
duration: Util.buildTimeCode(Util.parseMS(m.track.duration_ms)),
views: 0,
requestedBy: options.requestedBy,
playlist,
source: "spotify"
});
return data;
}) as Track[];
}
2021-06-14 11:44:32 +05:00
return { playlist: playlist, tracks: playlist.tracks };
}
case QueryType.SOUNDCLOUD_PLAYLIST: {
2021-06-14 11:45:03 +05:00
const data = await SoundCloud.getPlaylist(query).catch(() => {});
2021-06-14 11:44:32 +05:00
if (!data) return { playlist: null, tracks: [] };
const res = new Playlist(this, {
title: data.title,
description: data.description ?? "",
thumbnail: data.thumbnail ?? "https://soundcloud.com/pwa-icon-192.png",
type: "playlist",
source: "soundcloud",
author: {
name: data.author?.name ?? data.author?.username ?? "Unknown Artist",
url: data.author?.profile
},
tracks: [],
id: `${data.id}`, // stringified
url: data.url,
rawPlaylist: data
});
for (const song of data) {
const track = new Track(this, {
title: song.title,
description: song.description ?? "",
author: song.author?.username ?? song.author?.name ?? "Unknown Artist",
url: song.url,
thumbnail: song.thumbnail,
duration: Util.buildTimeCode(Util.parseMS(song.duration)),
views: song.playCount ?? 0,
requestedBy: options.requestedBy,
playlist: res,
source: "soundcloud",
engine: song
});
res.tracks.push(track);
}
return { playlist: res, tracks: res.tracks };
}
case QueryType.YOUTUBE_PLAYLIST: {
const ytpl = await YouTube.getPlaylist(query).catch(() => {});
if (!ytpl) return { playlist: null, tracks: [] };
// @todo: better way of handling large playlists
await ytpl.fetch().catch(() => {});
const playlist = new Playlist(this, {
title: ytpl.title,
thumbnail: ytpl.thumbnail?.displayThumbnailURL("maxresdefault"),
description: "",
type: "playlist",
source: "youtube",
author: {
name: ytpl.channel.name,
url: ytpl.channel.url
},
tracks: [],
id: ytpl.id,
url: ytpl.url,
rawPlaylist: ytpl
});
for (const video of ytpl) {
2021-06-14 11:45:03 +05:00
playlist.tracks.push(
new Track(this, {
title: video.title,
description: video.description,
author: video.channel?.name,
url: video.url,
requestedBy: options.requestedBy,
thumbnail: video.thumbnail?.displayThumbnailURL("maxresdefault"),
views: video.views,
duration: video.durationFormatted,
raw: video,
playlist: playlist
})
);
2021-06-14 11:44:32 +05:00
}
2021-06-13 18:09:25 +05:00
}
2021-06-11 23:39:21 +05:00
default:
2021-06-14 11:44:32 +05:00
return { playlist: null, tracks: [] };
2021-06-11 23:19:52 +05:00
}
}
2021-06-11 19:57:49 +05:00
*[Symbol.iterator]() {
yield* Array.from(this.queues.values());
}
2021-06-11 15:32:22 +05:00
}
2021-06-13 23:28:37 +05:00
export { DiscordPlayer as Player };