2021-06-11 15:32:22 +05:00
|
|
|
import { Guild, StageChannel, VoiceChannel } from "discord.js";
|
|
|
|
import { Player } from "../Player";
|
2021-06-11 23:19:52 +05:00
|
|
|
import { StreamDispatcher } from "../VoiceInterface/BasicStreamDispatcher";
|
2021-06-11 15:32:22 +05:00
|
|
|
import Track from "./Track";
|
2021-06-13 17:43:36 +05:00
|
|
|
import { PlayerOptions, PlayOptions, QueueRepeatMode } from "../types/types";
|
2021-06-11 23:19:52 +05:00
|
|
|
import ytdl from "discord-ytdl-core";
|
|
|
|
import { AudioResource, StreamType } from "@discordjs/voice";
|
2021-06-13 17:43:36 +05:00
|
|
|
import { Util } from "../utils/Util";
|
2021-06-13 18:09:25 +05:00
|
|
|
import YouTube from "youtube-sr";
|
2021-06-11 15:32:22 +05:00
|
|
|
|
2021-06-12 11:37:41 +05:00
|
|
|
class Queue<T = unknown> {
|
2021-06-11 15:32:22 +05:00
|
|
|
public readonly guild: Guild;
|
|
|
|
public readonly player: Player;
|
2021-06-11 23:19:52 +05:00
|
|
|
public connection: StreamDispatcher;
|
2021-06-11 15:32:22 +05:00
|
|
|
public tracks: Track[] = [];
|
2021-06-13 17:43:36 +05:00
|
|
|
public previousTracks: Track[] = [];
|
2021-06-11 15:32:22 +05:00
|
|
|
public options: PlayerOptions;
|
2021-06-11 23:19:52 +05:00
|
|
|
public playing = false;
|
2021-06-12 11:37:41 +05:00
|
|
|
public metadata?: T = null;
|
2021-06-13 17:43:36 +05:00
|
|
|
public repeatMode: QueueRepeatMode = 0;
|
2021-06-11 15:32:22 +05:00
|
|
|
|
|
|
|
constructor(player: Player, guild: Guild, options: PlayerOptions = {}) {
|
|
|
|
this.player = player;
|
|
|
|
this.guild = guild;
|
|
|
|
this.options = {};
|
|
|
|
|
|
|
|
Object.assign(
|
|
|
|
this.options,
|
|
|
|
{
|
|
|
|
leaveOnEnd: true,
|
|
|
|
leaveOnEndCooldown: 1000,
|
|
|
|
leaveOnStop: true,
|
|
|
|
leaveOnEmpty: true,
|
|
|
|
leaveOnEmptyCooldown: 1000,
|
|
|
|
autoSelfDeaf: true,
|
|
|
|
enableLive: false,
|
|
|
|
ytdlDownloadOptions: {},
|
|
|
|
useSafeSearch: false,
|
|
|
|
disableAutoRegister: false,
|
2021-06-11 23:19:52 +05:00
|
|
|
fetchBeforeQueued: false,
|
|
|
|
initialVolume: 100
|
2021-06-11 15:32:22 +05:00
|
|
|
} as PlayerOptions,
|
|
|
|
options
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-11 16:50:43 +05:00
|
|
|
get current() {
|
2021-06-11 23:19:52 +05:00
|
|
|
return this.connection.audioResource?.metadata ?? this.tracks[0];
|
2021-06-11 16:50:43 +05:00
|
|
|
}
|
|
|
|
|
2021-06-11 23:19:52 +05:00
|
|
|
async connect(channel: StageChannel | VoiceChannel) {
|
2021-06-13 13:06:19 +05:00
|
|
|
if (!["stage", "voice"].includes(channel?.type)) throw new TypeError(`Channel type must be voice or stage, got ${channel?.type}!`);
|
2021-06-11 16:50:43 +05:00
|
|
|
const connection = await this.player.voiceUtils.connect(channel);
|
2021-06-11 23:19:52 +05:00
|
|
|
this.connection = connection;
|
2021-06-11 15:32:22 +05:00
|
|
|
|
2021-06-13 14:22:45 +05:00
|
|
|
// it's ok to use this here since Queue listens to the events 1 time per play and destroys the listener
|
2021-06-13 14:20:05 +05:00
|
|
|
this.connection.setMaxListeners(Infinity);
|
|
|
|
|
2021-06-13 10:55:20 +05:00
|
|
|
if (channel.type === "stage") await channel.guild.me.voice.setRequestToSpeak(true).catch(() => {});
|
2021-06-12 11:37:41 +05:00
|
|
|
|
2021-06-13 17:03:20 +05:00
|
|
|
this.connection.on("error", (err) => this.player.emit("error", this, err));
|
|
|
|
this.connection.on("debug", (msg) => this.player.emit("debug", this, msg));
|
2021-06-13 14:22:45 +05:00
|
|
|
|
2021-06-11 15:32:22 +05:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
2021-06-11 23:19:52 +05:00
|
|
|
this.connection.end();
|
|
|
|
this.connection.disconnect();
|
2021-06-11 15:32:22 +05:00
|
|
|
this.player.queues.delete(this.guild.id);
|
|
|
|
}
|
2021-06-11 16:50:43 +05:00
|
|
|
|
2021-06-11 23:19:52 +05:00
|
|
|
skip() {
|
|
|
|
if (!this.connection) return false;
|
2021-06-12 00:18:53 +05:00
|
|
|
this.connection.end();
|
|
|
|
return true;
|
2021-06-11 23:19:52 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
addTrack(track: Track) {
|
2021-06-13 13:17:50 +05:00
|
|
|
this.tracks.push(track);
|
2021-06-13 13:06:19 +05:00
|
|
|
this.player.emit("trackAdd", this, track);
|
2021-06-11 23:19:52 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
addTracks(tracks: Track[]) {
|
|
|
|
this.tracks.push(...tracks);
|
2021-06-13 13:17:50 +05:00
|
|
|
this.player.emit("tracksAdd", this, tracks);
|
2021-06-11 23:19:52 +05:00
|
|
|
}
|
|
|
|
|
2021-06-12 00:18:53 +05:00
|
|
|
setPaused(paused?: boolean) {
|
|
|
|
if (!this.connection) return false;
|
|
|
|
return paused ? this.connection.pause() : this.connection.resume();
|
|
|
|
}
|
|
|
|
|
2021-06-12 11:37:41 +05:00
|
|
|
setBitrate(bitrate: number | "auto") {
|
|
|
|
if (!this.connection?.audioResource?.encoder) return;
|
|
|
|
if (bitrate === "auto") bitrate = this.connection.channel?.bitrate ?? 64000;
|
|
|
|
this.connection.audioResource.encoder.setBitrate(bitrate);
|
|
|
|
}
|
|
|
|
|
2021-06-13 13:06:19 +05:00
|
|
|
setVolume(amount: number) {
|
|
|
|
if (!this.connection) return false;
|
2021-06-13 14:06:01 +05:00
|
|
|
this.options.initialVolume = amount;
|
2021-06-13 13:06:19 +05:00
|
|
|
return this.connection.setVolume(amount);
|
|
|
|
}
|
|
|
|
|
2021-06-13 17:43:36 +05:00
|
|
|
setRepeatMode(mode: QueueRepeatMode) {
|
|
|
|
if (![QueueRepeatMode.OFF, QueueRepeatMode.QUEUE, QueueRepeatMode.TRACK].includes(mode)) throw new Error(`Unknown repeat mode "${mode}"!`);
|
|
|
|
const prev = this.repeatMode;
|
|
|
|
if (mode === prev) return false;
|
|
|
|
this.repeatMode = mode;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-13 13:06:19 +05:00
|
|
|
get volume() {
|
|
|
|
if (!this.connection) return 100;
|
|
|
|
return this.connection.volume;
|
|
|
|
}
|
|
|
|
|
2021-06-13 17:46:15 +05:00
|
|
|
async back() {
|
|
|
|
return await this.play(Util.last(this.previousTracks), { immediate: true });
|
|
|
|
}
|
|
|
|
|
2021-06-13 18:09:25 +05:00
|
|
|
async play(src?: Track, options: PlayOptions = {}): Promise<void> {
|
2021-06-13 13:06:19 +05:00
|
|
|
if (!this.connection || !this.connection.voiceConnection) throw new Error("Voice connection is not available, use <Queue>.connect()!");
|
|
|
|
if (src && (this.playing || this.tracks.length) && !options.immediate) return this.addTrack(src);
|
2021-06-12 11:37:41 +05:00
|
|
|
const track = options.filtersUpdate ? this.current : src ?? this.tracks.shift();
|
2021-06-11 23:19:52 +05:00
|
|
|
if (!track) return;
|
2021-06-13 17:43:36 +05:00
|
|
|
this.previousTracks = this.previousTracks.filter((x) => x._trackID !== track._trackID);
|
|
|
|
this.previousTracks.push(track);
|
2021-06-12 18:22:45 +05:00
|
|
|
let stream;
|
2021-06-11 23:19:52 +05:00
|
|
|
if (["youtube", "spotify"].includes(track.raw.source)) {
|
2021-06-13 18:09:25 +05:00
|
|
|
if (track.raw.source === "spotify" && !track.raw.engine) {
|
|
|
|
track.raw.engine = await YouTube.search(`${track.author} ${track.title}`, { type: "video" })
|
|
|
|
.then((x) => x[0].url)
|
|
|
|
.catch(() => null);
|
|
|
|
}
|
|
|
|
const link = track.raw.source === "spotify" ? track.raw.engine : track.url;
|
|
|
|
if (!link) return void this.play(this.tracks.shift(), { immediate: true });
|
|
|
|
|
|
|
|
stream = ytdl(link, {
|
2021-06-11 23:19:52 +05:00
|
|
|
// because we don't wanna decode opus into pcm again just for volume, let discord.js handle that
|
|
|
|
opusEncoded: false,
|
2021-06-12 11:37:41 +05:00
|
|
|
fmt: "s16le",
|
|
|
|
encoderArgs: options.encoderArgs ?? [],
|
|
|
|
seek: options.seek
|
2021-06-11 23:19:52 +05:00
|
|
|
});
|
|
|
|
} else {
|
2021-06-13 18:09:25 +05:00
|
|
|
stream = ytdl.arbitraryStream(
|
|
|
|
track.raw.source === "soundcloud" ? await track.raw.engine.downloadProgressive() : typeof track.raw.engine === "function" ? await track.raw.engine() : track.raw.engine,
|
|
|
|
{
|
|
|
|
// because we don't wanna decode opus into pcm again just for volume, let discord.js handle that
|
|
|
|
opusEncoded: false,
|
|
|
|
fmt: "s16le",
|
|
|
|
encoderArgs: options.encoderArgs ?? [],
|
|
|
|
seek: options.seek
|
|
|
|
}
|
|
|
|
);
|
2021-06-11 23:19:52 +05:00
|
|
|
}
|
|
|
|
|
2021-06-12 18:22:45 +05:00
|
|
|
const resource: AudioResource<Track> = this.connection.createStream(stream, {
|
|
|
|
type: StreamType.Raw,
|
|
|
|
data: track
|
|
|
|
});
|
|
|
|
|
2021-06-12 00:18:53 +05:00
|
|
|
const dispatcher = await this.connection.playStream(resource);
|
2021-06-11 23:19:52 +05:00
|
|
|
dispatcher.setVolume(this.options.initialVolume);
|
|
|
|
|
2021-06-13 13:06:19 +05:00
|
|
|
dispatcher.once("start", () => {
|
2021-06-11 23:19:52 +05:00
|
|
|
this.playing = true;
|
2021-06-12 11:37:41 +05:00
|
|
|
if (!options.filtersUpdate) this.player.emit("trackStart", this, this.current);
|
2021-06-11 23:19:52 +05:00
|
|
|
});
|
|
|
|
|
2021-06-13 13:06:19 +05:00
|
|
|
dispatcher.once("finish", () => {
|
2021-06-11 23:19:52 +05:00
|
|
|
this.playing = false;
|
2021-06-12 11:37:41 +05:00
|
|
|
if (options.filtersUpdate) return;
|
|
|
|
|
2021-06-13 17:43:36 +05:00
|
|
|
if (!this.tracks.length && this.repeatMode === QueueRepeatMode.OFF) {
|
2021-06-11 23:19:52 +05:00
|
|
|
this.destroy();
|
|
|
|
this.player.emit("queueEnd", this);
|
|
|
|
} else {
|
2021-06-13 17:43:36 +05:00
|
|
|
if (this.repeatMode === QueueRepeatMode.TRACK) return void this.play(Util.last(this.previousTracks), { immediate: true });
|
|
|
|
if (this.repeatMode === QueueRepeatMode.QUEUE) this.tracks.push(Util.last(this.previousTracks));
|
2021-06-11 23:19:52 +05:00
|
|
|
const nextTrack = this.tracks.shift();
|
2021-06-13 13:06:19 +05:00
|
|
|
this.play(nextTrack, { immediate: true });
|
2021-06-11 23:19:52 +05:00
|
|
|
}
|
|
|
|
});
|
2021-06-11 16:50:43 +05:00
|
|
|
}
|
2021-06-11 19:57:49 +05:00
|
|
|
|
|
|
|
*[Symbol.iterator]() {
|
|
|
|
yield* this.tracks;
|
|
|
|
}
|
2021-06-11 23:19:52 +05:00
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
guild: this.guild.id,
|
|
|
|
options: this.options,
|
|
|
|
tracks: this.tracks.map((m) => m.toJSON())
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
if (!this.tracks.length) return "No songs available to display!";
|
|
|
|
return `**Upcoming Songs:**\n${this.tracks.map((m, i) => `${i + 1}. **${m.title}**`).join("\n")}`;
|
|
|
|
}
|
2021-06-11 15:32:22 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
export { Queue };
|