Various bug fixes

This commit is contained in:
Max Isom 2020-03-17 19:42:28 -05:00
parent 15d4e251f2
commit c058ec95fe
7 changed files with 36 additions and 35 deletions

View file

@ -1,4 +1,4 @@
import {TextChannel, Message} from 'discord.js'; import {TextChannel, Message, GuildChannel} from 'discord.js';
import {injectable} from 'inversify'; import {injectable} from 'inversify';
import {Settings} from '../models'; import {Settings} from '../models';
import errorMsg from '../utils/error-msg'; import errorMsg from '../utils/error-msg';
@ -50,7 +50,13 @@ export default class implements Command {
} }
case 'channel': { case 'channel': {
const channel = msg.guild!.channels.cache.find(c => c.name === args[1]); let channel: GuildChannel | undefined;
if (args[1].includes('<#') && args[1].includes('>')) {
channel = msg.guild!.channels.cache.find(c => c.id === args[1].slice(2, args[1].indexOf('>')));
} else {
channel = msg.guild!.channels.cache.find(c => c.name === args[1]);
}
if (channel && channel.type === 'text') { if (channel && channel.type === 'text') {
await Settings.update({channel: channel.id}, {where: {guildId: msg.guild!.id}}); await Settings.update({channel: channel.id}, {where: {guildId: msg.guild!.id}});

View file

@ -25,12 +25,12 @@ export default class implements Command {
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 queue = this.queueManager.get(msg.guild!.id);
if (queue.get().length === 0) { if (!queue.getCurrent()) {
await msg.channel.send(errorMsg('nothing is playing')); await msg.channel.send(errorMsg('nothing is playing'));
return; return;
} }
if (queue.get()[0].isLive) { if (queue.getCurrent()?.isLive) {
await msg.channel.send(errorMsg('can\'t seek in a livestream')); await msg.channel.send(errorMsg('can\'t seek in a livestream'));
return; return;
} }

View file

@ -268,7 +268,10 @@ export default class implements Command {
if (this.playerManager.get(msg.guild!.id).voiceConnection === null) { if (this.playerManager.get(msg.guild!.id).voiceConnection === null) {
await this.playerManager.get(msg.guild!.id).connect(targetVoiceChannel); await this.playerManager.get(msg.guild!.id).connect(targetVoiceChannel);
}
if (this.queueManager.get(msg.guild!.id).size() === 0) {
// Only auto-play on first song added
await this.playerManager.get(msg.guild!.id).play(); await this.playerManager.get(msg.guild!.id).play();
} }
} }

View file

@ -26,12 +26,12 @@ export default class implements Command {
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 queue = this.queueManager.get(msg.guild!.id);
if (queue.get().length === 0) { if (!queue.getCurrent()) {
await msg.channel.send(errorMsg('nothing is playing')); await msg.channel.send(errorMsg('nothing is playing'));
return; return;
} }
if (queue.get()[0].isLive) { if (queue.getCurrent()?.isLive) {
await msg.channel.send(errorMsg('can\'t seek in a livestream')); await msg.channel.send(errorMsg('can\'t seek in a livestream'));
return; return;
} }

View file

@ -26,7 +26,7 @@ export default class implements Command {
try { try {
queue.forward(); queue.forward();
if (queue.isEmpty()) { if (queue.isEmpty() && !queue.getCurrent()) {
this.playerManager.get(msg.guild!.id).disconnect(); this.playerManager.get(msg.guild!.id).disconnect();
} else { } else {
await this.playerManager.get(msg.guild!.id).play(); await this.playerManager.get(msg.guild!.id).play();

View file

@ -19,6 +19,7 @@ export default class {
private readonly queue: Queue; private readonly queue: Queue;
private readonly cacheDir: string; private readonly cacheDir: string;
private dispatcher: StreamDispatcher | null = null; private dispatcher: StreamDispatcher | null = null;
private nowPlaying: QueuedSong | null = null;
private playPositionInterval: NodeJS.Timeout | undefined; private playPositionInterval: NodeJS.Timeout | undefined;
private positionInSeconds = 0; private positionInSeconds = 0;
@ -44,6 +45,7 @@ export default class {
this.voiceConnection.disconnect(); this.voiceConnection.disconnect();
} }
this.positionInSeconds = 0;
this.voiceConnection = null; this.voiceConnection = null;
this.dispatcher = null; this.dispatcher = null;
} }
@ -56,7 +58,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.getCurrentSong(); const currentSong = this.queue.getCurrent();
if (!currentSong) { if (!currentSong) {
throw new Error('No song currently playing'); throw new Error('No song currently playing');
@ -85,26 +87,17 @@ export default class {
throw new Error('Not connected to a voice channel.'); throw new Error('Not connected to a voice channel.');
} }
const currentSong = this.getCurrentSong(); const currentSong = this.queue.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) { if (this.status === STATUS.PAUSED && this.getPosition() !== 0 && this.dispatcher && currentSong.url === this.nowPlaying?.url) {
if (this.dispatcher) { this.dispatcher.resume();
this.dispatcher.resume(); this.status = STATUS.PLAYING;
this.status = STATUS.PLAYING; return;
return;
}
if (!currentSong.isLive) {
await this.seek(this.getPosition());
return;
}
// Must be livestream, continue
} }
if (await this.isCached(currentSong.url)) { if (await this.isCached(currentSong.url)) {
@ -117,6 +110,7 @@ export default class {
this.attachListeners(); this.attachListeners();
this.status = STATUS.PLAYING; this.status = STATUS.PLAYING;
this.nowPlaying = currentSong;
this.startTrackingPosition(); this.startTrackingPosition();
} }
@ -135,16 +129,6 @@ export default class {
this.stopTrackingPosition(); this.stopTrackingPosition();
} }
private getCurrentSong(): QueuedSong|null {
const songs = this.queue.get();
if (songs.length === 0) {
return null;
}
return songs[0];
}
private getCachedPath(url: string): string { private getCachedPath(url: string): string {
return path.join(this.cacheDir, hasha(url)); return path.join(this.cacheDir, hasha(url));
} }

View file

@ -34,8 +34,16 @@ export default class {
} }
} }
getCurrent(): QueuedSong | null {
if (this.queue[this.position]) {
return this.queue[this.position];
}
return null;
}
get(): QueuedSong[] { get(): QueuedSong[] {
return this.queue.slice(this.position); return this.queue.slice(this.position + 1);
} }
add(song: QueuedSong): void { add(song: QueuedSong): void {
@ -61,7 +69,7 @@ export default class {
} }
shuffle(): void { shuffle(): void {
this.queue = [this.queue[0], ...shuffle(this.queue.slice(1))]; this.queue = [...this.queue.slice(0, this.position), this.queue[this.position], this.queue[0], ...shuffle(this.queue.slice(this.position + 1))];
} }
clear(): void { clear(): void {
@ -76,7 +84,7 @@ export default class {
} }
size(): number { size(): number {
return this.queue.length; return this.get().length;
} }
isEmpty(): boolean { isEmpty(): boolean {