Merge Player and Queue services

This commit is contained in:
Max Isom 2020-03-20 20:47:04 -05:00
parent 646f030781
commit 9c91ce1a13
21 changed files with 236 additions and 255 deletions

View file

@ -73,6 +73,7 @@
"dotenv": "^8.2.0", "dotenv": "^8.2.0",
"fluent-ffmpeg": "^2.1.2", "fluent-ffmpeg": "^2.1.2",
"fs-capacitor": "^6.1.0", "fs-capacitor": "^6.1.0",
"get-youtube-id": "^1.0.1",
"got": "^10.6.0", "got": "^10.6.0",
"hasha": "^5.2.0", "hasha": "^5.2.0",
"inversify": "^5.0.1", "inversify": "^5.0.1",

View file

@ -90,8 +90,8 @@ export default class {
await handler.execute(msg, args); await handler.execute(msg, args);
} catch (error) { } catch (error) {
console.error(error); debug(error);
await msg.channel.send(errorMsg('¯\\_(ツ)_/¯')); await msg.channel.send(errorMsg(error.message.toLowerCase()));
} }
}); });

View file

@ -1,7 +1,7 @@
import {Message} from 'discord.js'; import {Message} from 'discord.js';
import {TYPES} from '../types'; import {TYPES} from '../types';
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import QueueManager from '../managers/queue'; import PlayerManager from '../managers/player';
import Command from '.'; import Command from '.';
@injectable() @injectable()
@ -14,14 +14,14 @@ export default class implements Command {
public requiresVC = true; public requiresVC = true;
private readonly queueManager: QueueManager; private readonly playerManager: PlayerManager;
constructor(@inject(TYPES.Managers.Queue) queueManager: QueueManager) { constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) {
this.queueManager = queueManager; this.playerManager = playerManager;
} }
public async execute(msg: Message, _: string []): Promise<void> { public async execute(msg: Message, _: string []): Promise<void> {
this.queueManager.get(msg.guild!.id).clear(); this.playerManager.get(msg.guild!.id).clear();
await msg.channel.send('clearer than a field after a fresh harvest'); await msg.channel.send('clearer than a field after a fresh harvest');
} }

View file

@ -2,7 +2,6 @@ import {Message, TextChannel} from 'discord.js';
import {TYPES} from '../types'; import {TYPES} from '../types';
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import PlayerManager from '../managers/player'; import PlayerManager from '../managers/player';
import QueueManager from '../managers/queue';
import LoadingMessage from '../utils/loading-message'; import LoadingMessage from '../utils/loading-message';
import errorMsg from '../utils/error-msg'; import errorMsg from '../utils/error-msg';
import Command from '.'; import Command from '.';
@ -18,17 +17,15 @@ export default class implements Command {
public requiresVC = true; public requiresVC = true;
private readonly playerManager: PlayerManager; private readonly playerManager: PlayerManager;
private readonly queueManager: QueueManager;
constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager, @inject(TYPES.Managers.Queue) queueManager: QueueManager) { constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) {
this.playerManager = playerManager; this.playerManager = playerManager;
this.queueManager = queueManager;
} }
public async execute(msg: Message, args: string []): Promise<void> { public async execute(msg: Message, args: string []): Promise<void> {
const queue = this.queueManager.get(msg.guild!.id); const player = this.playerManager.get(msg.guild!.id);
const currentSong = queue.getCurrent(); const currentSong = player.getCurrent();
if (!currentSong) { if (!currentSong) {
await msg.channel.send(errorMsg('nothing is playing')); await msg.channel.send(errorMsg('nothing is playing'));
@ -42,7 +39,7 @@ export default class implements Command {
const seekTime = parseInt(args[0], 10); const seekTime = parseInt(args[0], 10);
if (seekTime + this.playerManager.get(msg.guild!.id).getPosition() > currentSong.length) { if (seekTime + player.getPosition() > currentSong.length) {
await msg.channel.send(errorMsg('can\'t seek past the end of the song')); await msg.channel.send(errorMsg('can\'t seek past the end of the song'));
return; return;
} }
@ -52,7 +49,7 @@ export default class implements Command {
await loading.start(); await loading.start();
try { try {
await this.playerManager.get(msg.guild!.id).forwardSeek(seekTime); await player.forwardSeek(seekTime);
await loading.stop(); await loading.stop();
} catch (error) { } catch (error) {

View file

@ -2,9 +2,8 @@ import {TextChannel, Message} from 'discord.js';
import {URL} from 'url'; import {URL} from 'url';
import {TYPES} from '../types'; import {TYPES} from '../types';
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import {QueuedSong} from '../services/queue'; import {QueuedSong} from '../services/player';
import {STATUS} from '../services/player'; import {STATUS} from '../services/player';
import QueueManager from '../managers/queue';
import PlayerManager from '../managers/player'; import PlayerManager from '../managers/player';
import {getMostPopularVoiceChannel} from '../utils/channels'; import {getMostPopularVoiceChannel} from '../utils/channels';
import LoadingMessage from '../utils/loading-message'; import LoadingMessage from '../utils/loading-message';
@ -28,12 +27,10 @@ export default class implements Command {
public requiresVC = true; public requiresVC = true;
private readonly queueManager: QueueManager;
private readonly playerManager: PlayerManager; private readonly playerManager: PlayerManager;
private readonly getSongs: GetSongs; private readonly getSongs: GetSongs;
constructor(@inject(TYPES.Managers.Queue) queueManager: QueueManager, @inject(TYPES.Managers.Player) playerManager: PlayerManager, @inject(TYPES.Services.GetSongs) getSongs: GetSongs) { constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager, @inject(TYPES.Services.GetSongs) getSongs: GetSongs) {
this.queueManager = queueManager;
this.playerManager = playerManager; this.playerManager = playerManager;
this.getSongs = getSongs; this.getSongs = getSongs;
} }
@ -44,11 +41,10 @@ export default class implements Command {
const res = new LoadingMessage(msg.channel as TextChannel); const res = new LoadingMessage(msg.channel as TextChannel);
await res.start(); await res.start();
const queue = this.queueManager.get(msg.guild!.id);
const player = this.playerManager.get(msg.guild!.id); const player = this.playerManager.get(msg.guild!.id);
const queueOldSize = queue.size(); const queueOldSize = player.queueSize();
const wasPlayingSong = queue.getCurrent() !== null; const wasPlayingSong = player.getCurrent() !== null;
if (args.length === 0) { if (args.length === 0) {
if (player.status === STATUS.PLAYING) { if (player.status === STATUS.PLAYING) {
@ -57,7 +53,7 @@ export default class implements Command {
} }
// Must be resuming play // Must be resuming play
if (queue.get().length === 0 && !queue.getCurrent()) { if (!wasPlayingSong) {
await res.stop(errorMsg('nothing to play')); await res.stop(errorMsg('nothing to play'));
return; return;
} }
@ -101,11 +97,15 @@ export default class implements Command {
extraMsg = 'a random sample of 50 songs was taken'; extraMsg = 'a random sample of 50 songs was taken';
} }
if (totalSongs > 50 && nSongsNotFound !== 0) {
extraMsg += ' and ';
}
if (nSongsNotFound !== 0) { if (nSongsNotFound !== 0) {
if (nSongsNotFound === 1) { if (nSongsNotFound === 1) {
extraMsg += 'and 1 song was not found'; extraMsg += '1 song was not found';
} else { } else {
extraMsg += `and ${nSongsNotFound.toString()} songs were not found`; extraMsg += `${nSongsNotFound.toString()} songs were not found`;
} }
} }
@ -130,7 +130,7 @@ export default class implements Command {
return; return;
} }
newSongs.forEach(song => queue.add(song)); newSongs.forEach(song => player.add(song));
const firstSong = newSongs[0]; const firstSong = newSongs[0];

View file

@ -1,13 +1,13 @@
import {Message, MessageEmbed} from 'discord.js'; import {Message, MessageEmbed} from 'discord.js';
import {TYPES} from '../types'; import {TYPES} from '../types';
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import QueueManager from '../managers/queue';
import PlayerManager from '../managers/player'; import PlayerManager from '../managers/player';
import {STATUS} from '../services/player'; import {STATUS} from '../services/player';
import Command from '.'; import Command from '.';
import getProgressBar from '../utils/get-progress-bar'; import getProgressBar from '../utils/get-progress-bar';
import errorMsg from '../utils/error-msg'; import errorMsg from '../utils/error-msg';
import {prettyTime} from '../utils/time'; import {prettyTime} from '../utils/time';
import getYouTubeID from 'get-youtube-id';
const PAGE_SIZE = 10; const PAGE_SIZE = 10;
@ -19,22 +19,19 @@ export default class implements Command {
['queue', 'shows current queue'] ['queue', 'shows current queue']
]; ];
private readonly queueManager: QueueManager;
private readonly playerManager: PlayerManager; private readonly playerManager: PlayerManager;
constructor(@inject(TYPES.Managers.Queue) queueManager: QueueManager, @inject(TYPES.Managers.Player) playerManager: PlayerManager) { constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) {
this.queueManager = queueManager;
this.playerManager = playerManager; this.playerManager = playerManager;
} }
public async execute(msg: Message, args: string []): Promise<void> { public async execute(msg: Message, args: string []): Promise<void> {
const queue = this.queueManager.get(msg.guild!.id);
const player = this.playerManager.get(msg.guild!.id); const player = this.playerManager.get(msg.guild!.id);
const currentlyPlaying = queue.getCurrent(); const currentlyPlaying = player.getCurrent();
if (currentlyPlaying) { if (currentlyPlaying) {
const queueSize = queue.size(); const queueSize = player.queueSize();
const queuePage = args[0] ? parseInt(args[0], 10) : 1; const queuePage = args[0] ? parseInt(args[0], 10) : 1;
if (queuePage * PAGE_SIZE > queueSize && queuePage > Math.ceil((queueSize + 1) / PAGE_SIZE)) { if (queuePage * PAGE_SIZE > queueSize && queuePage > Math.ceil((queueSize + 1) / PAGE_SIZE)) {
@ -45,23 +42,30 @@ export default class implements Command {
const embed = new MessageEmbed(); const embed = new MessageEmbed();
embed.setTitle(currentlyPlaying.title); embed.setTitle(currentlyPlaying.title);
embed.setURL(`https://www.youtube.com/watch?v=${currentlyPlaying.url}`); embed.setURL(`https://www.youtube.com/watch?v=${currentlyPlaying.url.length === 11 ? currentlyPlaying.url : getYouTubeID(currentlyPlaying.url) ?? ''}`);
embed.setFooter(`Source: ${currentlyPlaying.artist}`);
let description = player.status === STATUS.PLAYING ? '⏹️' : '▶️'; let description = player.status === STATUS.PLAYING ? '⏹️' : '▶️';
description += ' '; description += ' ';
description += getProgressBar(20, player.getPosition() / currentlyPlaying.length); description += getProgressBar(20, player.getPosition() / currentlyPlaying.length);
description += ' '; description += ' ';
description += `\`[${prettyTime(player.getPosition())}/${prettyTime(currentlyPlaying.length)}]\``; description += `\`[${prettyTime(player.getPosition())}/${currentlyPlaying.isLive ? 'live' : prettyTime(currentlyPlaying.length)}]\``;
description += ' 🔉'; description += ' 🔉';
description += queue.isEmpty() ? '' : '\n\n**Next up:**'; description += player.isQueueEmpty() ? '' : '\n\n**Next up:**';
embed.setDescription(description); embed.setDescription(description);
let footer = `Source: ${currentlyPlaying.artist}`;
if (currentlyPlaying.playlist) {
footer += ` (${currentlyPlaying.playlist.title})`;
}
embed.setFooter(footer);
const queuePageBegin = (queuePage - 1) * PAGE_SIZE; const queuePageBegin = (queuePage - 1) * PAGE_SIZE;
const queuePageEnd = queuePageBegin + PAGE_SIZE; const queuePageEnd = queuePageBegin + PAGE_SIZE;
queue.get().slice(queuePageBegin, queuePageEnd).forEach((song, i) => { player.getQueue().slice(queuePageBegin, queuePageEnd).forEach((song, i) => {
embed.addField(`${(i + 1 + queuePageBegin).toString()}/${queueSize.toString()}`, song.title, false); embed.addField(`${(i + 1 + queuePageBegin).toString()}/${queueSize.toString()}`, song.title, false);
}); });

View file

@ -2,7 +2,6 @@ import {Message, TextChannel} from 'discord.js';
import {TYPES} from '../types'; import {TYPES} from '../types';
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import PlayerManager from '../managers/player'; import PlayerManager from '../managers/player';
import QueueManager from '../managers/queue';
import LoadingMessage from '../utils/loading-message'; import LoadingMessage from '../utils/loading-message';
import errorMsg from '../utils/error-msg'; import errorMsg from '../utils/error-msg';
import Command from '.'; import Command from '.';
@ -20,17 +19,15 @@ export default class implements Command {
public requiresVC = true; public requiresVC = true;
private readonly playerManager: PlayerManager; private readonly playerManager: PlayerManager;
private readonly queueManager: QueueManager;
constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager, @inject(TYPES.Managers.Queue) queueManager: QueueManager) { constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) {
this.playerManager = playerManager; this.playerManager = playerManager;
this.queueManager = queueManager;
} }
public async execute(msg: Message, args: string []): Promise<void> { public async execute(msg: Message, args: string []): Promise<void> {
const queue = this.queueManager.get(msg.guild!.id); const player = this.playerManager.get(msg.guild!.id);
const currentSong = queue.getCurrent(); const currentSong = player.getCurrent();
if (!currentSong) { if (!currentSong) {
await msg.channel.send(errorMsg('nothing is playing')); await msg.channel.send(errorMsg('nothing is playing'));
@ -69,7 +66,7 @@ export default class implements Command {
await loading.start(); await loading.start();
try { try {
await this.playerManager.get(msg.guild!.id).seek(seekTime); await player.seek(seekTime);
await loading.stop(); await loading.stop();
} catch (error) { } catch (error) {

View file

@ -1,7 +1,7 @@
import {Message} from 'discord.js'; import {Message} from 'discord.js';
import {TYPES} from '../types'; import {TYPES} from '../types';
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import QueueManager from '../managers/queue'; import PlayerManager from '../managers/player';
import errorMsg from '../utils/error-msg'; import errorMsg from '../utils/error-msg';
import Command from '.'; import Command from '.';
@ -15,23 +15,22 @@ export default class implements Command {
public requiresVC = true; public requiresVC = true;
private readonly queueManager: QueueManager; private readonly playerManager: PlayerManager;
constructor(@inject(TYPES.Managers.Queue) queueManager: QueueManager) { constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) {
this.queueManager = queueManager; this.playerManager = playerManager;
} }
public async execute(msg: Message, _: string []): Promise<void> { public async execute(msg: Message, _: string []): Promise<void> {
const queue = this.queueManager.get(msg.guild!.id).get(); const player = this.playerManager.get(msg.guild!.id);
if (queue.length <= 2) { if (player.isQueueEmpty()) {
await msg.channel.send(errorMsg('not enough songs to shuffle')); await msg.channel.send(errorMsg('not enough songs to shuffle'));
return; return;
} }
this.queueManager.get(msg.guild!.id).shuffle(); player.shuffle();
// TODO: better response
await msg.channel.send('shuffled'); await msg.channel.send('shuffled');
} }
} }

View file

@ -1,9 +1,10 @@
import {Message} from 'discord.js'; import {Message, TextChannel} from 'discord.js';
import {TYPES} from '../types'; import {TYPES} from '../types';
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import PlayerManager from '../managers/player'; import PlayerManager from '../managers/player';
import QueueManager from '../managers/queue';
import Command from '.'; import Command from '.';
import LoadingMessage from '../utils/loading-message';
import errorMsg from '../utils/error-msg';
@injectable() @injectable()
export default class implements Command { export default class implements Command {
@ -15,29 +16,24 @@ export default class implements Command {
public requiresVC = true; public requiresVC = true;
private readonly queueManager: QueueManager;
private readonly playerManager: PlayerManager; private readonly playerManager: PlayerManager;
constructor(@inject(TYPES.Managers.Queue) queueManager: QueueManager, @inject(TYPES.Managers.Player) playerManager: PlayerManager) { constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) {
this.queueManager = queueManager;
this.playerManager = playerManager; this.playerManager = playerManager;
} }
public async execute(msg: Message, _: string []): Promise<void> { public async execute(msg: Message, _: string []): Promise<void> {
const queue = this.queueManager.get(msg.guild!.id);
const player = this.playerManager.get(msg.guild!.id); const player = this.playerManager.get(msg.guild!.id);
const loader = new LoadingMessage(msg.channel as TextChannel);
try { try {
queue.forward(); await loader.start();
player.resetPosition(); await player.forward();
if (queue.isEmpty() && !queue.getCurrent()) { await loader.stop('keep \'er movin\'');
player.disconnect();
}
await msg.channel.send('keep \'er movin\'');
} catch (_) { } catch (_) {
await msg.channel.send('no song to skip to'); await loader.stop(errorMsg('no song to skip to'));
} }
} }
} }

View file

@ -2,7 +2,6 @@ import {Message} from 'discord.js';
import {TYPES} from '../types'; import {TYPES} from '../types';
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import PlayerManager from '../managers/player'; import PlayerManager from '../managers/player';
import QueueManager from '../managers/queue';
import errorMsg from '../utils/error-msg'; import errorMsg from '../utils/error-msg';
import Command from '.'; import Command from '.';
@ -16,21 +15,17 @@ export default class implements Command {
public requiresVC = true; public requiresVC = true;
private readonly queueManager: QueueManager;
private readonly playerManager: PlayerManager; private readonly playerManager: PlayerManager;
constructor(@inject(TYPES.Managers.Queue) queueManager: QueueManager, @inject(TYPES.Managers.Player) playerManager: PlayerManager) { constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) {
this.queueManager = queueManager;
this.playerManager = playerManager; this.playerManager = playerManager;
} }
public async execute(msg: Message, _: string []): Promise<void> { public async execute(msg: Message, _: string []): Promise<void> {
const queue = this.queueManager.get(msg.guild!.id);
const player = this.playerManager.get(msg.guild!.id); const player = this.playerManager.get(msg.guild!.id);
try { try {
queue.back(); await player.back();
player.resetPosition();
await msg.channel.send('back \'er up\''); await msg.channel.send('back \'er up\'');
} catch (_) { } catch (_) {

View file

@ -17,7 +17,6 @@ import {
// Managers // Managers
import PlayerManager from './managers/player'; import PlayerManager from './managers/player';
import QueueManager from './managers/queue';
// Helpers // Helpers
import GetSongs from './services/get-songs'; import GetSongs from './services/get-songs';
@ -47,7 +46,6 @@ container.bind<Client>(TYPES.Client).toConstantValue(new Client());
// Managers // Managers
container.bind<PlayerManager>(TYPES.Managers.Player).to(PlayerManager).inSingletonScope(); container.bind<PlayerManager>(TYPES.Managers.Player).to(PlayerManager).inSingletonScope();
container.bind<QueueManager>(TYPES.Managers.Queue).to(QueueManager).inSingletonScope();
// Helpers // Helpers
container.bind<GetSongs>(TYPES.Services.GetSongs).to(GetSongs).inSingletonScope(); container.bind<GetSongs>(TYPES.Services.GetSongs).to(GetSongs).inSingletonScope();

View file

@ -1,25 +1,22 @@
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import {TYPES} from '../types'; import {TYPES} from '../types';
import Player from '../services/player'; import Player from '../services/player';
import QueueManager from './queue';
@injectable() @injectable()
export default class { export default class {
private readonly guildPlayers: Map<string, Player>; private readonly guildPlayers: Map<string, Player>;
private readonly cacheDir: string; private readonly cacheDir: string;
private readonly queueManager: QueueManager;
constructor(@inject(TYPES.Config.CACHE_DIR) cacheDir: string, @inject(TYPES.Managers.Queue) queueManager: QueueManager) { constructor(@inject(TYPES.Config.CACHE_DIR) cacheDir: string) {
this.guildPlayers = new Map(); this.guildPlayers = new Map();
this.cacheDir = cacheDir; this.cacheDir = cacheDir;
this.queueManager = queueManager;
} }
get(guildId: string): Player { get(guildId: string): Player {
let player = this.guildPlayers.get(guildId); let player = this.guildPlayers.get(guildId);
if (!player) { if (!player) {
player = new Player(this.queueManager.get(guildId), this.cacheDir); player = new Player(this.cacheDir);
this.guildPlayers.set(guildId, player); this.guildPlayers.set(guildId, player);
} }

View file

@ -1,23 +0,0 @@
import {injectable} from 'inversify';
import Queue from '../services/queue';
@injectable()
export default class {
private readonly guildQueues: Map<string, Queue>;
constructor() {
this.guildQueues = new Map();
}
get(guildId: string): Queue {
let queue = this.guildQueues.get(guildId);
if (!queue) {
queue = new Queue();
this.guildQueues.set(guildId, queue);
}
return queue;
}
}

View file

@ -8,8 +8,9 @@ import ytsr from 'ytsr';
import YouTube from 'youtube.ts'; import YouTube from 'youtube.ts';
import pLimit from 'p-limit'; import pLimit from 'p-limit';
import uniqueRandomArray from 'unique-random-array'; import uniqueRandomArray from 'unique-random-array';
import {QueuedSong, QueuedPlaylist} from '../services/queue'; import {QueuedSong, QueuedPlaylist} from '../services/player';
import {TYPES} from '../types'; import {TYPES} from '../types';
import {parseTime} from '../utils/time';
@injectable() @injectable()
export default class { export default class {
@ -186,7 +187,7 @@ export default class {
return { return {
title: video.title, title: video.title,
artist: track.artists[0].name, artist: track.artists[0].name,
length: track.duration_ms / 1000, length: parseTime(video.duration),
url: video.link, url: video.link,
playlist, playlist,
isLive: video.live isLive: video.live

View file

@ -2,17 +2,14 @@ import {inject, injectable} from 'inversify';
import {Message} from 'discord.js'; import {Message} from 'discord.js';
import {TYPES} from '../types'; import {TYPES} from '../types';
import PlayerManager from '../managers/player'; import PlayerManager from '../managers/player';
import QueueManager from '../managers/queue';
import {getMostPopularVoiceChannel} from '../utils/channels'; import {getMostPopularVoiceChannel} from '../utils/channels';
@injectable() @injectable()
export default class { export default class {
private readonly playerManager: PlayerManager; private readonly playerManager: PlayerManager;
private readonly queueManager: QueueManager;
constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager, @inject(TYPES.Managers.Queue) queueManager: QueueManager) { constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) {
this.playerManager = playerManager; this.playerManager = playerManager;
this.queueManager = queueManager;
} }
async execute(msg: Message): Promise<boolean> { async execute(msg: Message): Promise<boolean> {
@ -24,7 +21,6 @@ export default class {
} }
if (msg.content.includes('packers')) { if (msg.content.includes('packers')) {
const queue = this.queueManager.get(msg.guild!.id);
const player = this.playerManager.get(msg.guild!.id); const player = this.playerManager.get(msg.guild!.id);
const [channel, n] = getMostPopularVoiceChannel(msg.guild!); const [channel, n] = getMostPopularVoiceChannel(msg.guild!);
@ -39,14 +35,15 @@ export default class {
await player.connect(channel); await player.connect(channel);
} }
const isPlaying = queue.getCurrent() !== null; const isPlaying = player.getCurrent() !== null;
let oldPosition = 0; let oldPosition = 0;
queue.add({title: 'GO PACKERS!', artist: 'Unknown', url: 'https://www.youtube.com/watch?v=qkdtID7mY3E', length: 204, playlist: null, isLive: false}); player.add({title: 'GO PACKERS!', artist: 'Unknown', url: 'https://www.youtube.com/watch?v=qkdtID7mY3E', length: 204, playlist: null, isLive: false}, {immediate: true});
if (isPlaying) { if (isPlaying) {
oldPosition = player.getPosition(); oldPosition = player.getPosition();
queue.forward();
await player.forward();
} }
await player.seek(8); await player.seek(8);
@ -54,10 +51,10 @@ export default class {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
setTimeout(async () => { setTimeout(async () => {
queue.removeCurrent(); player.removeCurrent();
if (isPlaying) { if (isPlaying) {
queue.back(); await player.back();
await player.seek(oldPosition); await player.seek(oldPosition);
} else { } else {
player.disconnect(); player.disconnect();

View file

@ -6,7 +6,21 @@ import hasha from 'hasha';
import ytdl from 'ytdl-core'; import ytdl from 'ytdl-core';
import {WriteStream} from 'fs-capacitor'; import {WriteStream} from 'fs-capacitor';
import ffmpeg from 'fluent-ffmpeg'; import ffmpeg from 'fluent-ffmpeg';
import Queue, {QueuedSong} from './queue'; import shuffle from 'array-shuffle';
export interface QueuedPlaylist {
title: string;
source: string;
}
export interface QueuedSong {
title: string;
artist: string;
url: string;
length: number;
playlist: QueuedPlaylist | null;
isLive: boolean;
}
export enum STATUS { export enum STATUS {
PLAYING, PLAYING,
@ -16,7 +30,8 @@ export enum STATUS {
export default class { export default class {
public status = STATUS.PAUSED; public status = STATUS.PAUSED;
public voiceConnection: VoiceConnection | null = null; public voiceConnection: VoiceConnection | null = null;
private readonly queue: Queue; private queue: QueuedSong[] = [];
private queuePosition = 0;
private readonly cacheDir: string; private readonly cacheDir: string;
private dispatcher: StreamDispatcher | null = null; private dispatcher: StreamDispatcher | null = null;
private nowPlaying: QueuedSong | null = null; private nowPlaying: QueuedSong | null = null;
@ -25,8 +40,7 @@ export default class {
private positionInSeconds = 0; private positionInSeconds = 0;
constructor(queue: Queue, cacheDir: string) { constructor(cacheDir: string) {
this.queue = queue;
this.cacheDir = cacheDir; this.cacheDir = cacheDir;
} }
@ -58,7 +72,7 @@ export default class {
throw new Error('Not connected to a voice channel.'); throw new Error('Not connected to a voice channel.');
} }
const currentSong = this.queue.getCurrent(); const currentSong = this.getCurrent();
if (!currentSong) { if (!currentSong) {
throw new Error('No song currently playing'); throw new Error('No song currently playing');
@ -90,14 +104,14 @@ export default class {
throw new Error('Not connected to a voice channel.'); throw new Error('Not connected to a voice channel.');
} }
const currentSong = this.queue.getCurrent(); const currentSong = this.getCurrent();
if (!currentSong) { if (!currentSong) {
throw new Error('Queue empty.'); throw new Error('Queue empty.');
} }
// Resume from paused state // Resume from paused state
if (this.status === STATUS.PAUSED && this.getPosition() !== 0 && currentSong.url === this.nowPlaying?.url) { if (this.status === STATUS.PAUSED && currentSong.url === this.nowPlaying?.url) {
if (this.dispatcher) { if (this.dispatcher) {
this.dispatcher.resume(); this.dispatcher.resume();
this.status = STATUS.PLAYING; this.status = STATUS.PLAYING;
@ -109,6 +123,7 @@ export default class {
return this.seek(this.getPosition()); return this.seek(this.getPosition());
} }
try {
const stream = await this.getStream(currentSong.url); const stream = await this.getStream(currentSong.url);
this.dispatcher = this.voiceConnection.play(stream, {type: 'webm/opus'}); this.dispatcher = this.voiceConnection.play(stream, {type: 'webm/opus'});
@ -124,6 +139,10 @@ export default class {
this.startTrackingPosition(0); this.startTrackingPosition(0);
this.lastSongURL = currentSong.url; this.lastSongURL = currentSong.url;
} }
} catch (error) {
this.removeCurrent();
throw error;
}
} }
pause(): void { pause(): void {
@ -140,8 +159,103 @@ export default class {
this.stopTrackingPosition(); this.stopTrackingPosition();
} }
resetPosition(): void { async forward(): Promise<void> {
if (this.queuePosition < this.queueSize() + 1) {
this.queuePosition++;
try {
if (this.getCurrent() && this.status !== STATUS.PAUSED) {
await this.play();
} else {
this.status = STATUS.PAUSED;
this.disconnect();
}
} catch (error) {
this.queuePosition--;
throw error;
}
} else {
throw new Error('No songs in queue to forward to.');
}
}
async back(): Promise<void> {
if (this.queuePosition - 1 >= 0) {
this.queuePosition--;
this.positionInSeconds = 0; this.positionInSeconds = 0;
if (this.status !== STATUS.PAUSED) {
await this.play();
}
} else {
throw new Error('No songs in queue to go back to.');
}
}
getCurrent(): QueuedSong | null {
if (this.queue[this.queuePosition]) {
return this.queue[this.queuePosition];
}
return null;
}
getQueue(): QueuedSong[] {
return this.queue.slice(this.queuePosition + 1);
}
add(song: QueuedSong, {immediate = false} = {}): void {
if (song.playlist) {
// Add to end of queue
this.queue.push(song);
} else {
// Not from playlist, add immediately
let insertAt = this.queuePosition + 1;
if (!immediate) {
// Loop until playlist song
this.queue.some(song => {
if (song.playlist) {
return true;
}
insertAt++;
return false;
});
}
this.queue = [...this.queue.slice(0, insertAt), song, ...this.queue.slice(insertAt)];
}
}
shuffle(): void {
this.queue = [...this.queue.slice(0, this.queuePosition + 1), ...shuffle(this.queue.slice(this.queuePosition + 1))];
}
clear(): void {
const newQueue = [];
// Don't clear curently playing song
const current = this.getCurrent();
if (current) {
newQueue.push(current);
}
this.queuePosition = 0;
this.queue = newQueue;
}
removeCurrent(): void {
this.queue = [...this.queue.slice(0, this.queuePosition), ...this.queue.slice(this.queuePosition + 1)];
}
queueSize(): number {
return this.getQueue().length;
}
isQueueEmpty(): boolean {
return this.queueSize() === 0;
} }
private getCachedPath(url: string): string { private getCachedPath(url: string): string {
@ -271,30 +385,23 @@ export default class {
return; return;
} }
this.voiceConnection.on('disconnect', () => { this.voiceConnection.on('disconnect', this.onVoiceConnectionDisconnect.bind(this));
this.disconnect(false);
});
if (!this.dispatcher) { if (!this.dispatcher) {
return; return;
} }
this.dispatcher.on('speaking', async isSpeaking => { this.dispatcher.on('speaking', this.onVoiceConnectionSpeaking.bind(this));
}
private onVoiceConnectionDisconnect(): void {
this.disconnect(false);
}
private async onVoiceConnectionSpeaking(isSpeaking: boolean): Promise<void> {
// Automatically advance queued song at end // Automatically advance queued song at end
if (!isSpeaking && this.status === STATUS.PLAYING) { if (!isSpeaking && this.status === STATUS.PLAYING) {
if (this.queue.size() >= 0) { await this.forward();
this.queue.forward();
this.positionInSeconds = 0;
if (this.queue.getCurrent()) {
await this.play();
} else {
this.status = STATUS.PAUSED;
this.disconnect();
} }
} }
}
});
}
} }

View file

@ -1,97 +0,0 @@
import shuffle from 'array-shuffle';
export interface QueuedPlaylist {
title: string;
source: string;
}
export interface QueuedSong {
title: string;
artist: string;
url: string;
length: number;
playlist: QueuedPlaylist | null;
isLive: boolean;
}
export default class {
private queue: QueuedSong[] = [];
private position = 0;
forward(): void {
if (this.position < this.size() + 1) {
this.position++;
} else {
throw new Error('No songs in queue to forward to.');
}
}
back(): void {
if (this.position - 1 >= 0) {
this.position--;
} else {
throw new Error('No songs in queue to go back to.');
}
}
getCurrent(): QueuedSong | null {
if (this.queue[this.position]) {
return this.queue[this.position];
}
return null;
}
get(): QueuedSong[] {
return this.queue.slice(this.position + 1);
}
add(song: QueuedSong): void {
if (song.playlist) {
// Add to end of queue
this.queue.push(song);
} else {
// Not from playlist, add immediately
let insertAt = this.position;
// Loop until playlist song
this.queue.some(song => {
if (song.playlist) {
return true;
}
insertAt++;
return false;
});
this.queue = [...this.queue.slice(0, insertAt), song, ...this.queue.slice(insertAt)];
}
}
shuffle(): void {
this.queue = [...this.queue.slice(0, this.position), this.queue[this.position], this.queue[0], ...shuffle(this.queue.slice(this.position + 1))];
}
clear(): void {
const newQueue = [];
// Don't clear curently playing song
if (this.queue.length > 0) {
newQueue.push(this.queue[this.position]);
}
this.queue = newQueue;
}
removeCurrent(): void {
this.queue = [...this.queue.slice(0, this.position), ...this.queue.slice(this.position + 1)];
}
size(): number {
return this.get().length;
}
isEmpty(): boolean {
return this.get().length === 0;
}
}

View file

@ -14,8 +14,7 @@ export const TYPES = {
Spotify: Symbol('Spotify') Spotify: Symbol('Spotify')
}, },
Managers: { Managers: {
Player: Symbol('PlayerManager'), Player: Symbol('PlayerManager')
Queue: Symbol('QueueManager')
}, },
Services: { Services: {
GetSongs: Symbol('GetSongs'), GetSongs: Symbol('GetSongs'),

View file

@ -60,10 +60,16 @@ export default class {
} }
async stop(str = 'u betcha'): Promise<Message> { async stop(str = 'u betcha'): Promise<Message> {
const wasAlreadyStopped = this.isStopped;
this.isStopped = true; this.isStopped = true;
if (str) { if (str) {
if (wasAlreadyStopped) {
await this.msg.edit(str);
} else {
await Promise.all([this.msg.reactions.removeAll(), this.msg.edit(str)]); await Promise.all([this.msg.reactions.removeAll(), this.msg.edit(str)]);
}
} else { } else {
await this.msg.reactions.removeAll(); await this.msg.reactions.removeAll();
} }

View file

@ -13,3 +13,5 @@ export const prettyTime = (seconds: number): string => {
return res; return res;
}; };
export const parseTime = (str: string): number => str.split(':').reduce((acc, time) => (60 * acc) + parseInt(time, 10), 0);

View file

@ -1160,6 +1160,11 @@ get-stream@^5.0.0, get-stream@^5.1.0:
dependencies: dependencies:
pump "^3.0.0" pump "^3.0.0"
get-youtube-id@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/get-youtube-id/-/get-youtube-id-1.0.1.tgz#adb6f475e292d98f98ed5bfb530887656193e157"
integrity sha512-5yidLzoLXbtw82a/Wb7LrajkGn29BM6JuLWeHyNfzOGp1weGyW4+7eMz6cP23+etqj27VlOFtq8fFFDMLq/FXQ==
getpass@^0.1.1: getpass@^0.1.1:
version "0.1.7" version "0.1.7"
resolved "https://registry.yarnpkg.com/getpass/-/getpass-0.1.7.tgz#5eff8e3e684d569ae4cb2b1282604e8ba62149fa" resolved "https://registry.yarnpkg.com/getpass/-/getpass-0.1.7.tgz#5eff8e3e684d569ae4cb2b1282604e8ba62149fa"