From 18e851821caa13747c8d946abe36d3e2e5d6dcbd Mon Sep 17 00:00:00 2001 From: Max Isom Date: Sun, 15 Mar 2020 20:11:45 -0500 Subject: [PATCH] Don't allow seeking in livestream --- src/commands/fseek.ts | 17 ++++++++++++++++- src/commands/play.ts | 9 ++++++--- src/commands/seek.ts | 17 ++++++++++++++++- src/services/queue.ts | 1 + 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/commands/fseek.ts b/src/commands/fseek.ts index 2fae1e6..e442612 100644 --- a/src/commands/fseek.ts +++ b/src/commands/fseek.ts @@ -2,6 +2,7 @@ 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 Command from '.'; @@ -10,12 +11,26 @@ export default class implements Command { public name = 'fseek'; public description = 'forward seek position in currently playing song'; private readonly playerManager: PlayerManager; + private readonly queueManager: QueueManager; - constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) { + constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager, @inject(TYPES.Managers.Queue) queueManager: QueueManager) { this.playerManager = playerManager; + this.queueManager = queueManager; } public async execute(msg: Message, args: string []): Promise { + const queue = this.queueManager.get(msg.guild!.id); + + if (queue.get().length === 0) { + await msg.channel.send('nothing is playing'); + return; + } + + if (queue.get()[0].isLive) { + await msg.channel.send('can\'t seek in a livestream'); + return; + } + const seekTime = parseInt(args[0], 10); const loading = new LoadingMessage(msg.channel as TextChannel, 'hold on a sec'); diff --git a/src/commands/play.ts b/src/commands/play.ts index 2d91d19..d791be3 100644 --- a/src/commands/play.ts +++ b/src/commands/play.ts @@ -72,7 +72,8 @@ export default class implements Command { artist: videoDetails.snippet.channelTitle, length: toSeconds(parse(videoDetails.contentDetails.duration)), url: videoDetails.id, - playlist: null + playlist: null, + isLive: videoDetails.snippet.liveBroadcastContent === 'live' }); }; @@ -106,7 +107,8 @@ export default class implements Command { artist: video.snippet.channelTitle, length, url: video.contentDetails.videoId, - playlist: queuedPlaylist + playlist: queuedPlaylist, + isLive: false }); }); } else { @@ -190,7 +192,8 @@ export default class implements Command { artist: track.artists[0].name, length: track.duration_ms / 1000, url: video.link, - playlist + playlist, + isLive: video.live }; } catch (_) { // TODO: handle error diff --git a/src/commands/seek.ts b/src/commands/seek.ts index ac8609c..ec8f0d7 100644 --- a/src/commands/seek.ts +++ b/src/commands/seek.ts @@ -2,6 +2,7 @@ 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 Command from '.'; @@ -10,12 +11,26 @@ export default class implements Command { public name = 'seek'; public description = 'seeks position in currently playing song'; private readonly playerManager: PlayerManager; + private readonly queueManager: QueueManager; - constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) { + constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager, @inject(TYPES.Managers.Queue) queueManager: QueueManager) { this.playerManager = playerManager; + this.queueManager = queueManager; } public async execute(msg: Message, args: string []): Promise { + const queue = this.queueManager.get(msg.guild!.id); + + if (queue.get().length === 0) { + await msg.channel.send('nothing is playing'); + return; + } + + if (queue.get()[0].isLive) { + await msg.channel.send('can\'t seek in a livestream'); + return; + } + const time = args[0]; let seekTime = 0; diff --git a/src/services/queue.ts b/src/services/queue.ts index f439a5c..ffc16ae 100644 --- a/src/services/queue.ts +++ b/src/services/queue.ts @@ -11,6 +11,7 @@ export interface QueuedSong { url: string; length: number; playlist: QueuedPlaylist | null; + isLive: boolean; } export default class {