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> {
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'));
return;
}
if (queue.getCurrent()?.isLive) {
if (currentSong.isLive) {
await msg.channel.send(errorMsg('can\'t seek in a livestream'));
return;
}
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);
await loading.start();

View file

@ -48,6 +48,8 @@ export default class implements Command {
const queue = this.queueManager.get(msg.guild!.id);
const queueOldSize = queue.size();
if (args.length === 0) {
if (this.playerManager.get(msg.guild!.id).status === STATUS.PLAYING) {
await res.stop(errorMsg('already playing, give me a song name'));
@ -69,8 +71,6 @@ export default class implements Command {
const newSongs: QueuedSong[] = [];
let nSongsNotFound = 0;
// Test if it's a complete URL
try {
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') {
const [convertedSongs, nMissing] = await this.getSongs.spotifySource(args[0]);
nSongsNotFound = nMissing;
const [convertedSongs] = await this.getSongs.spotifySource(args[0]);
newSongs.push(...convertedSongs);
}
@ -119,17 +117,22 @@ export default class implements Command {
return;
}
newSongs.forEach(song => this.queueManager.get(msg.guild!.id).add(song));
newSongs.forEach(song => queue.add(song));
// TODO: better response
await res.stop(`song(s) queued (${nSongsNotFound} not found)`);
const firstSong = newSongs[0];
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) {
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
if (queueOldSize === 0) {
// Only auto-play if queue was empty before
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> {
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> {
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'));
return;
}
if (queue.getCurrent()?.isLive) {
if (currentSong.isLive) {
await msg.channel.send(errorMsg('can\'t seek in a livestream'));
return;
}
@ -46,6 +48,11 @@ export default class implements Command {
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);
await loading.start();

View file

@ -64,6 +64,10 @@ export default class {
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)) {
this.dispatcher = this.voiceConnection.play(this.getCachedPath(currentSong.url), {seek: positionSeconds});
} else {

View file

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