Don't allow seeking past end of song

This commit is contained in:
Max Isom 2020-03-18 12:40:31 -05:00
parent 7844e80991
commit 11246812a6
6 changed files with 40 additions and 17 deletions

View file

@ -25,18 +25,25 @@ 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.getCurrent()) { const currentSong = queue.getCurrent();
if (!currentSong) {
await msg.channel.send(errorMsg('nothing is playing')); await msg.channel.send(errorMsg('nothing is playing'));
return; return;
} }
if (queue.getCurrent()?.isLive) { if (currentSong.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;
} }
const seekTime = parseInt(args[0], 10); const seekTime = parseInt(args[0], 10);
if (seekTime + this.playerManager.get(msg.guild!.id).getPosition() > currentSong.length) {
await msg.channel.send(errorMsg('can\'t seek past the end of the song'));
return;
}
const loading = new LoadingMessage(msg.channel as TextChannel); const loading = new LoadingMessage(msg.channel as TextChannel);
await loading.start(); await loading.start();

View file

@ -48,6 +48,8 @@ export default class implements Command {
const queue = this.queueManager.get(msg.guild!.id); const queue = this.queueManager.get(msg.guild!.id);
const queueOldSize = queue.size();
if (args.length === 0) { if (args.length === 0) {
if (this.playerManager.get(msg.guild!.id).status === STATUS.PLAYING) { if (this.playerManager.get(msg.guild!.id).status === STATUS.PLAYING) {
await res.stop(errorMsg('already playing, give me a song name')); await res.stop(errorMsg('already playing, give me a song name'));
@ -69,8 +71,6 @@ export default class implements Command {
const newSongs: QueuedSong[] = []; const newSongs: QueuedSong[] = [];
let nSongsNotFound = 0;
// Test if it's a complete URL // Test if it's a complete URL
try { try {
const url = new URL(args[0]); const url = new URL(args[0]);
@ -94,9 +94,7 @@ export default class implements Command {
} }
} }
} else if (url.protocol === 'spotify:' || url.host === 'open.spotify.com') { } else if (url.protocol === 'spotify:' || url.host === 'open.spotify.com') {
const [convertedSongs, nMissing] = await this.getSongs.spotifySource(args[0]); const [convertedSongs] = await this.getSongs.spotifySource(args[0]);
nSongsNotFound = nMissing;
newSongs.push(...convertedSongs); newSongs.push(...convertedSongs);
} }
@ -119,17 +117,22 @@ export default class implements Command {
return; return;
} }
newSongs.forEach(song => this.queueManager.get(msg.guild!.id).add(song)); newSongs.forEach(song => queue.add(song));
// TODO: better response const firstSong = newSongs[0];
await res.stop(`song(s) queued (${nSongsNotFound} not found)`);
if (newSongs.length === 1) {
await res.stop(`u betcha, **${firstSong.title}** added to the queue`);
} else {
await res.stop(`u betcha, **${firstSong.title}** and ${newSongs.length - 1} other songs were added to the queue`);
}
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) { if (queueOldSize === 0) {
// Only auto-play on first song added // Only auto-play if queue was empty before
await this.playerManager.get(msg.guild!.id).play(); await this.playerManager.get(msg.guild!.id).play();
} }
} }

View file

@ -18,8 +18,10 @@ export default class implements Command {
} }
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 queue = this.queueManager.get(msg.guild!.id);
await msg.channel.send('`' + JSON.stringify(queue.slice(0, 10)) + '`'); await msg.channel.send('`' + JSON.stringify(queue.getCurrent()) + '`');
await msg.channel.send('`' + JSON.stringify(queue.get().slice(0, 10)) + '`');
} }
} }

View file

@ -26,12 +26,14 @@ 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.getCurrent()) { const currentSong = queue.getCurrent();
if (!currentSong) {
await msg.channel.send(errorMsg('nothing is playing')); await msg.channel.send(errorMsg('nothing is playing'));
return; return;
} }
if (queue.getCurrent()?.isLive) { if (currentSong.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;
} }
@ -46,6 +48,11 @@ export default class implements Command {
seekTime = parseInt(time, 10); seekTime = parseInt(time, 10);
} }
if (seekTime > currentSong.length) {
await msg.channel.send(errorMsg('can\'t seek past the end of the song'));
return;
}
const loading = new LoadingMessage(msg.channel as TextChannel); const loading = new LoadingMessage(msg.channel as TextChannel);
await loading.start(); await loading.start();

View file

@ -64,6 +64,10 @@ export default class {
throw new Error('No song currently playing'); throw new Error('No song currently playing');
} }
if (positionSeconds > currentSong.length) {
throw new Error('Seek position is outside the range of the song.');
}
if (await this.isCached(currentSong.url)) { if (await this.isCached(currentSong.url)) {
this.dispatcher = this.voiceConnection.play(this.getCachedPath(currentSong.url), {seek: positionSeconds}); this.dispatcher = this.voiceConnection.play(this.getCachedPath(currentSong.url), {seek: positionSeconds});
} else { } else {

View file

@ -77,7 +77,7 @@ export default class {
// Don't clear curently playing song // Don't clear curently playing song
if (this.queue.length > 0) { if (this.queue.length > 0) {
newQueue.push(this.queue[0]); newQueue.push(this.queue[this.position]);
} }
this.queue = newQueue; this.queue = newQueue;