2020-03-15 20:36:59 +01:00
|
|
|
import {VoiceConnection, VoiceChannel, StreamDispatcher} from 'discord.js';
|
2020-03-14 02:36:42 +01:00
|
|
|
import {promises as fs, createWriteStream} from 'fs';
|
2020-03-15 03:48:08 +01:00
|
|
|
import {Readable, PassThrough} from 'stream';
|
2020-03-14 02:36:42 +01:00
|
|
|
import path from 'path';
|
|
|
|
import hasha from 'hasha';
|
|
|
|
import ytdl from 'ytdl-core';
|
|
|
|
import {WriteStream} from 'fs-capacitor';
|
2020-03-15 03:48:08 +01:00
|
|
|
import ffmpeg from 'fluent-ffmpeg';
|
2020-03-14 02:36:42 +01:00
|
|
|
import Queue, {QueuedSong} from './queue';
|
2020-03-13 04:41:26 +01:00
|
|
|
|
2020-03-15 20:36:59 +01:00
|
|
|
export enum STATUS {
|
|
|
|
PLAYING,
|
2020-03-17 23:59:26 +01:00
|
|
|
PAUSED
|
2020-03-13 04:41:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class {
|
2020-03-17 23:59:26 +01:00
|
|
|
public status = STATUS.PAUSED;
|
2020-03-17 02:14:15 +01:00
|
|
|
public voiceConnection: VoiceConnection | null = null;
|
2020-03-13 04:41:26 +01:00
|
|
|
private readonly queue: Queue;
|
2020-03-14 02:36:42 +01:00
|
|
|
private readonly cacheDir: string;
|
2020-03-15 20:36:59 +01:00
|
|
|
private dispatcher: StreamDispatcher | null = null;
|
2020-03-18 01:42:28 +01:00
|
|
|
private nowPlaying: QueuedSong | null = null;
|
2020-03-16 01:30:07 +01:00
|
|
|
private playPositionInterval: NodeJS.Timeout | undefined;
|
2020-03-13 04:41:26 +01:00
|
|
|
|
2020-03-15 21:35:34 +01:00
|
|
|
private positionInSeconds = 0;
|
|
|
|
|
2020-03-15 20:36:59 +01:00
|
|
|
constructor(queue: Queue, cacheDir: string) {
|
2020-03-13 04:41:26 +01:00
|
|
|
this.queue = queue;
|
2020-03-14 02:36:42 +01:00
|
|
|
this.cacheDir = cacheDir;
|
2020-03-13 04:41:26 +01:00
|
|
|
}
|
|
|
|
|
2020-03-15 20:36:59 +01:00
|
|
|
async connect(channel: VoiceChannel): Promise<void> {
|
2020-03-13 04:41:26 +01:00
|
|
|
const conn = await channel.join();
|
|
|
|
|
2020-03-15 20:36:59 +01:00
|
|
|
this.voiceConnection = conn;
|
2020-03-13 04:41:26 +01:00
|
|
|
}
|
|
|
|
|
2020-03-17 23:59:26 +01:00
|
|
|
disconnect(breakConnection = true): void {
|
2020-03-15 20:36:59 +01:00
|
|
|
if (this.voiceConnection) {
|
2020-03-16 01:30:07 +01:00
|
|
|
if (this.status === STATUS.PLAYING) {
|
|
|
|
this.pause();
|
|
|
|
}
|
|
|
|
|
2020-03-17 23:59:26 +01:00
|
|
|
if (breakConnection) {
|
|
|
|
this.voiceConnection.disconnect();
|
|
|
|
}
|
|
|
|
|
2020-03-18 01:42:28 +01:00
|
|
|
this.positionInSeconds = 0;
|
2020-03-17 23:59:26 +01:00
|
|
|
this.voiceConnection = null;
|
|
|
|
this.dispatcher = null;
|
2020-03-13 04:41:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 20:36:59 +01:00
|
|
|
async seek(positionSeconds: number): Promise<void> {
|
2020-03-17 23:59:26 +01:00
|
|
|
this.status = STATUS.PAUSED;
|
|
|
|
|
2020-03-15 20:36:59 +01:00
|
|
|
if (this.voiceConnection === null) {
|
2020-03-14 02:36:42 +01:00
|
|
|
throw new Error('Not connected to a voice channel.');
|
|
|
|
}
|
|
|
|
|
2020-03-18 01:42:28 +01:00
|
|
|
const currentSong = this.queue.getCurrent();
|
2020-03-14 02:36:42 +01:00
|
|
|
|
|
|
|
if (!currentSong) {
|
|
|
|
throw new Error('No song currently playing');
|
|
|
|
}
|
|
|
|
|
2020-03-18 18:40:31 +01:00
|
|
|
if (positionSeconds > currentSong.length) {
|
|
|
|
throw new Error('Seek position is outside the range of the song.');
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:36:48 +01:00
|
|
|
if (await this.isCached(currentSong.url)) {
|
|
|
|
this.dispatcher = this.voiceConnection.play(this.getCachedPath(currentSong.url), {seek: positionSeconds});
|
|
|
|
} else {
|
|
|
|
const stream = await this.getStream(currentSong.url, {seek: positionSeconds});
|
|
|
|
this.dispatcher = this.voiceConnection.play(stream, {type: 'webm/opus'});
|
|
|
|
}
|
2020-03-16 01:30:07 +01:00
|
|
|
|
|
|
|
this.attachListeners();
|
|
|
|
this.startTrackingPosition(positionSeconds);
|
2020-03-15 21:35:34 +01:00
|
|
|
|
2020-03-16 01:30:07 +01:00
|
|
|
this.status = STATUS.PLAYING;
|
2020-03-15 21:35:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async forwardSeek(positionSeconds: number): Promise<void> {
|
|
|
|
return this.seek(this.positionInSeconds + positionSeconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
getPosition(): number {
|
|
|
|
return this.positionInSeconds;
|
2020-03-14 02:36:42 +01:00
|
|
|
}
|
|
|
|
|
2020-03-15 20:36:59 +01:00
|
|
|
async play(): Promise<void> {
|
|
|
|
if (this.voiceConnection === null) {
|
2020-03-13 04:41:26 +01:00
|
|
|
throw new Error('Not connected to a voice channel.');
|
|
|
|
}
|
|
|
|
|
2020-03-18 01:42:28 +01:00
|
|
|
const currentSong = this.queue.getCurrent();
|
2020-03-17 23:59:26 +01:00
|
|
|
|
|
|
|
if (!currentSong) {
|
|
|
|
throw new Error('Queue empty.');
|
|
|
|
}
|
|
|
|
|
2020-03-15 20:36:59 +01:00
|
|
|
// Resume from paused state
|
2020-03-18 01:42:28 +01:00
|
|
|
if (this.status === STATUS.PAUSED && this.getPosition() !== 0 && this.dispatcher && currentSong.url === this.nowPlaying?.url) {
|
|
|
|
this.dispatcher.resume();
|
|
|
|
this.status = STATUS.PLAYING;
|
|
|
|
return;
|
2020-03-13 04:41:26 +01:00
|
|
|
}
|
|
|
|
|
2020-03-14 02:36:42 +01:00
|
|
|
if (await this.isCached(currentSong.url)) {
|
2020-03-16 01:30:07 +01:00
|
|
|
this.dispatcher = this.voiceConnection.play(this.getCachedPath(currentSong.url));
|
2020-03-14 02:36:42 +01:00
|
|
|
} else {
|
|
|
|
const stream = await this.getStream(currentSong.url);
|
2020-03-16 01:30:07 +01:00
|
|
|
this.dispatcher = this.voiceConnection.play(stream, {type: 'webm/opus'});
|
2020-03-14 02:36:42 +01:00
|
|
|
}
|
2020-03-13 04:41:26 +01:00
|
|
|
|
2020-03-16 01:30:07 +01:00
|
|
|
this.attachListeners();
|
2020-03-13 04:41:26 +01:00
|
|
|
|
2020-03-15 20:36:59 +01:00
|
|
|
this.status = STATUS.PLAYING;
|
2020-03-18 01:42:28 +01:00
|
|
|
this.nowPlaying = currentSong;
|
2020-03-16 01:30:07 +01:00
|
|
|
|
|
|
|
this.startTrackingPosition();
|
2020-03-13 04:41:26 +01:00
|
|
|
}
|
|
|
|
|
2020-03-15 20:36:59 +01:00
|
|
|
pause(): void {
|
2020-03-16 01:30:07 +01:00
|
|
|
if (this.status !== STATUS.PLAYING) {
|
2020-03-15 20:36:59 +01:00
|
|
|
throw new Error('Not currently playing.');
|
|
|
|
}
|
2020-03-13 04:41:26 +01:00
|
|
|
|
2020-03-16 01:30:07 +01:00
|
|
|
this.status = STATUS.PAUSED;
|
|
|
|
|
|
|
|
if (this.dispatcher) {
|
|
|
|
this.dispatcher.pause();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.stopTrackingPosition();
|
2020-03-13 04:41:26 +01:00
|
|
|
}
|
|
|
|
|
2020-03-14 02:36:42 +01:00
|
|
|
private getCachedPath(url: string): string {
|
2020-03-15 21:13:12 +01:00
|
|
|
return path.join(this.cacheDir, hasha(url));
|
2020-03-14 02:36:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private getCachedPathTemp(url: string): string {
|
2020-03-17 18:30:27 +01:00
|
|
|
return path.join(this.cacheDir, 'tmp', hasha(url));
|
2020-03-14 02:36:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private async isCached(url: string): Promise<boolean> {
|
|
|
|
try {
|
|
|
|
await fs.access(this.getCachedPath(url));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-18 03:36:48 +01:00
|
|
|
private async getStream(url: string, options: {seek?: number} = {}): Promise<Readable|string> {
|
2020-03-14 02:36:42 +01:00
|
|
|
const cachedPath = this.getCachedPath(url);
|
|
|
|
|
|
|
|
if (await this.isCached(url)) {
|
|
|
|
return cachedPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not yet cached, must download
|
|
|
|
const info = await ytdl.getInfo(url);
|
|
|
|
|
|
|
|
const {formats} = info;
|
|
|
|
|
|
|
|
const filter = (format: ytdl.videoFormat): boolean => format.codecs === 'opus' && format.container === 'webm' && format.audioSampleRate !== undefined && parseInt(format.audioSampleRate, 10) === 48000;
|
|
|
|
|
|
|
|
let format = formats.find(filter);
|
|
|
|
|
2020-03-15 03:48:08 +01:00
|
|
|
const nextBestFormat = (formats: ytdl.videoFormat[]): ytdl.videoFormat | undefined => {
|
|
|
|
if (formats[0].live) {
|
|
|
|
formats = formats.sort((a, b) => (b as any).audioBitrate - (a as any).audioBitrate); // Bad typings
|
|
|
|
|
|
|
|
return formats.find(format => [128, 127, 120, 96, 95, 94, 93].includes(parseInt(format.itag as unknown as string, 10))); // Bad typings
|
|
|
|
}
|
|
|
|
|
2020-03-14 02:36:42 +01:00
|
|
|
formats = formats
|
|
|
|
.filter(format => format.averageBitrate)
|
|
|
|
.sort((a, b) => b.averageBitrate - a.averageBitrate);
|
|
|
|
return formats.find(format => !format.bitrate) ?? formats[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!format) {
|
|
|
|
format = nextBestFormat(info.formats);
|
2020-03-15 03:48:08 +01:00
|
|
|
|
|
|
|
if (!format) {
|
|
|
|
// If still no format is found, throw
|
|
|
|
throw new Error('Can\'t find suitable format.');
|
|
|
|
}
|
2020-03-14 02:36:42 +01:00
|
|
|
}
|
|
|
|
|
2020-03-18 03:36:48 +01:00
|
|
|
const inputOptions = [
|
|
|
|
'-reconnect',
|
|
|
|
'1',
|
|
|
|
'-reconnect_streamed',
|
|
|
|
'1',
|
|
|
|
'-reconnect_delay_max',
|
|
|
|
'5'
|
|
|
|
];
|
|
|
|
|
|
|
|
if (options.seek) {
|
|
|
|
inputOptions.push('-ss', options.seek.toString());
|
2020-03-14 02:36:42 +01:00
|
|
|
}
|
|
|
|
|
2020-03-18 03:36:48 +01:00
|
|
|
const youtubeStream = ffmpeg(format.url).inputOptions(inputOptions).noVideo().audioCodec('libopus').outputFormat('webm').pipe() as PassThrough;
|
|
|
|
|
2020-03-14 02:36:42 +01:00
|
|
|
const capacitor = new WriteStream();
|
|
|
|
|
|
|
|
youtubeStream.pipe(capacitor);
|
|
|
|
|
2020-03-15 21:13:12 +01:00
|
|
|
// Don't cache livestreams
|
|
|
|
if (!info.player_response.videoDetails.isLiveContent) {
|
|
|
|
const cacheTempPath = this.getCachedPathTemp(url);
|
|
|
|
const cacheStream = createWriteStream(cacheTempPath);
|
|
|
|
|
|
|
|
cacheStream.on('finish', async () => {
|
|
|
|
await fs.rename(cacheTempPath, cachedPath);
|
|
|
|
});
|
|
|
|
|
|
|
|
capacitor.createReadStream().pipe(cacheStream);
|
|
|
|
}
|
2020-03-14 02:36:42 +01:00
|
|
|
|
|
|
|
return capacitor.createReadStream();
|
|
|
|
}
|
2020-03-15 20:36:59 +01:00
|
|
|
|
2020-03-16 01:30:07 +01:00
|
|
|
private startTrackingPosition(initalPosition?: number): void {
|
|
|
|
if (initalPosition) {
|
|
|
|
this.positionInSeconds = initalPosition;
|
|
|
|
}
|
2020-03-15 21:35:34 +01:00
|
|
|
|
2020-03-16 01:30:07 +01:00
|
|
|
if (this.playPositionInterval) {
|
|
|
|
clearInterval(this.playPositionInterval);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.playPositionInterval = setInterval(() => {
|
|
|
|
this.positionInSeconds++;
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
private stopTrackingPosition(): void {
|
|
|
|
if (this.playPositionInterval) {
|
|
|
|
clearInterval(this.playPositionInterval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private attachListeners(): void {
|
|
|
|
if (!this.voiceConnection) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.voiceConnection.on('disconnect', () => {
|
2020-03-17 23:59:26 +01:00
|
|
|
this.disconnect(false);
|
2020-03-16 01:30:07 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!this.dispatcher) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.dispatcher.on('speaking', async isSpeaking => {
|
2020-03-15 20:36:59 +01:00
|
|
|
// Automatically advance queued song at end
|
|
|
|
if (!isSpeaking && this.status === STATUS.PLAYING) {
|
|
|
|
if (this.queue.get().length > 0) {
|
|
|
|
this.queue.forward();
|
|
|
|
await this.play();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-03-13 04:41:26 +01:00
|
|
|
}
|