Add queue output, various bug fixes

This commit is contained in:
Max Isom 2020-03-18 22:29:43 -05:00
parent 0357373123
commit 7f39642c49
8 changed files with 123 additions and 24 deletions

View file

@ -48,11 +48,13 @@ export default class implements Command {
}
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;
if (args.length === 0) {
if (this.playerManager.get(msg.guild!.id).status === STATUS.PLAYING) {
if (player.status === STATUS.PLAYING) {
await res.stop(errorMsg('already playing, give me a song name'));
return;
}
@ -63,8 +65,8 @@ export default class implements Command {
return;
}
await this.playerManager.get(msg.guild!.id).connect(targetVoiceChannel);
await this.playerManager.get(msg.guild!.id).play();
await player.connect(targetVoiceChannel);
await player.play();
await res.stop('the stop-and-go light is now green');
return;
@ -141,13 +143,13 @@ export default class implements Command {
await res.stop(`u betcha, **${firstSong.title}** and ${newSongs.length - 1} other songs were added to the queue${extraMsg}`);
}
if (this.playerManager.get(msg.guild!.id).voiceConnection === null) {
await this.playerManager.get(msg.guild!.id).connect(targetVoiceChannel);
if (player.voiceConnection === null) {
await player.connect(targetVoiceChannel);
}
if (queueOldSize === 0) {
// Only auto-play if queue was empty before
await this.playerManager.get(msg.guild!.id).play();
if (queueOldSize === 0 && !wasPlayingSong) {
// Only auto-play if queue was empty before and nothing was playing
await player.play();
}
}
}

View file

@ -1,8 +1,15 @@
import {Message} from 'discord.js';
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';
const PAGE_SIZE = 10;
@injectable()
export default class implements Command {
@ -13,16 +20,54 @@ export default class implements Command {
];
private readonly queueManager: QueueManager;
private readonly playerManager: PlayerManager;
constructor(@inject(TYPES.Managers.Queue) queueManager: QueueManager) {
constructor(@inject(TYPES.Managers.Queue) queueManager: QueueManager, @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, args: string []): Promise<void> {
const queue = this.queueManager.get(msg.guild!.id);
const player = this.playerManager.get(msg.guild!.id);
await msg.channel.send('`' + JSON.stringify(queue.getCurrent()) + '`');
const currentlyPlaying = queue.getCurrent();
await msg.channel.send('`' + JSON.stringify(queue.get().slice(0, 10)) + '`');
if (currentlyPlaying) {
const queueSize = queue.size();
const queuePage = args[0] ? parseInt(args[0], 10) : 1;
if (queuePage * PAGE_SIZE > queueSize && queuePage > Math.ceil((queueSize + 1) / PAGE_SIZE)) {
await msg.channel.send(errorMsg('the queue isn\'t that big'));
return;
}
const embed = new MessageEmbed();
embed.setTitle(currentlyPlaying.title);
embed.setURL(`https://www.youtube.com/watch?v=${currentlyPlaying.url}`);
embed.setFooter(`Source: ${currentlyPlaying.artist}`);
let description = player.status === STATUS.PLAYING ? '⏹️' : '▶️';
description += ' ';
description += getProgressBar(20, player.getPosition() / currentlyPlaying.length);
description += ' ';
description += `\`[${prettyTime(player.getPosition())}/${prettyTime(currentlyPlaying.length)}]\``;
description += ' 🔉';
description += queue.isEmpty() ? '' : '\n\n**Next up:**';
embed.setDescription(description);
const queuePageBegin = (queuePage - 1) * PAGE_SIZE;
const queuePageEnd = queuePageBegin + PAGE_SIZE;
queue.get().slice(queuePageBegin, queuePageEnd).forEach((song, i) => {
embed.addField(`${(i + 1 + queuePageBegin).toString()}/${queueSize.toString()}`, song.title, false);
});
await msg.channel.send(embed);
} else {
await msg.channel.send('queue empty');
}
}
}