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

@ -1,7 +1,7 @@
import {Message} from 'discord.js';
import {TYPES} from '../types';
import {inject, injectable} from 'inversify';
import QueueManager from '../managers/queue';
import PlayerManager from '../managers/player';
import Command from '.';
@injectable()
@ -14,14 +14,14 @@ export default class implements Command {
public requiresVC = true;
private readonly queueManager: QueueManager;
private readonly playerManager: PlayerManager;
constructor(@inject(TYPES.Managers.Queue) queueManager: QueueManager) {
this.queueManager = queueManager;
constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) {
this.playerManager = playerManager;
}
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');
}

View file

@ -2,7 +2,6 @@ import {Message, TextChannel} from 'discord.js';
import {TYPES} from '../types';
import {inject, injectable} from 'inversify';
import PlayerManager from '../managers/player';
import QueueManager from '../managers/queue';
import LoadingMessage from '../utils/loading-message';
import errorMsg from '../utils/error-msg';
import Command from '.';
@ -18,17 +17,15 @@ export default class implements Command {
public requiresVC = true;
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.queueManager = queueManager;
}
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) {
await msg.channel.send(errorMsg('nothing is playing'));
@ -42,7 +39,7 @@ export default class implements Command {
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'));
return;
}
@ -52,7 +49,7 @@ export default class implements Command {
await loading.start();
try {
await this.playerManager.get(msg.guild!.id).forwardSeek(seekTime);
await player.forwardSeek(seekTime);
await loading.stop();
} catch (error) {

View file

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

View file

@ -1,13 +1,13 @@
import {Message, MessageEmbed} from 'discord.js';
import {TYPES} from '../types';
import {inject, injectable} from 'inversify';
import QueueManager from '../managers/queue';
import PlayerManager from '../managers/player';
import {STATUS} from '../services/player';
import Command from '.';
import getProgressBar from '../utils/get-progress-bar';
import errorMsg from '../utils/error-msg';
import {prettyTime} from '../utils/time';
import getYouTubeID from 'get-youtube-id';
const PAGE_SIZE = 10;
@ -19,22 +19,19 @@ export default class implements Command {
['queue', 'shows current queue']
];
private readonly queueManager: QueueManager;
private readonly playerManager: PlayerManager;
constructor(@inject(TYPES.Managers.Queue) queueManager: QueueManager, @inject(TYPES.Managers.Player) playerManager: PlayerManager) {
this.queueManager = queueManager;
constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) {
this.playerManager = playerManager;
}
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 currentlyPlaying = queue.getCurrent();
const currentlyPlaying = player.getCurrent();
if (currentlyPlaying) {
const queueSize = queue.size();
const queueSize = player.queueSize();
const queuePage = args[0] ? parseInt(args[0], 10) : 1;
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();
embed.setTitle(currentlyPlaying.title);
embed.setURL(`https://www.youtube.com/watch?v=${currentlyPlaying.url}`);
embed.setFooter(`Source: ${currentlyPlaying.artist}`);
embed.setURL(`https://www.youtube.com/watch?v=${currentlyPlaying.url.length === 11 ? currentlyPlaying.url : getYouTubeID(currentlyPlaying.url) ?? ''}`);
let description = player.status === STATUS.PLAYING ? '⏹️' : '▶️';
description += ' ';
description += getProgressBar(20, player.getPosition() / currentlyPlaying.length);
description += ' ';
description += `\`[${prettyTime(player.getPosition())}/${prettyTime(currentlyPlaying.length)}]\``;
description += `\`[${prettyTime(player.getPosition())}/${currentlyPlaying.isLive ? 'live' : prettyTime(currentlyPlaying.length)}]\``;
description += ' 🔉';
description += queue.isEmpty() ? '' : '\n\n**Next up:**';
description += player.isQueueEmpty() ? '' : '\n\n**Next up:**';
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 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);
});

View file

@ -2,7 +2,6 @@ import {Message, TextChannel} from 'discord.js';
import {TYPES} from '../types';
import {inject, injectable} from 'inversify';
import PlayerManager from '../managers/player';
import QueueManager from '../managers/queue';
import LoadingMessage from '../utils/loading-message';
import errorMsg from '../utils/error-msg';
import Command from '.';
@ -20,17 +19,15 @@ export default class implements Command {
public requiresVC = true;
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.queueManager = queueManager;
}
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) {
await msg.channel.send(errorMsg('nothing is playing'));
@ -69,7 +66,7 @@ export default class implements Command {
await loading.start();
try {
await this.playerManager.get(msg.guild!.id).seek(seekTime);
await player.seek(seekTime);
await loading.stop();
} catch (error) {

View file

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