From 7fcd9a6a7de6704a9a9e357b792a8ed52b0c4edc Mon Sep 17 00:00:00 2001 From: Max Isom Date: Fri, 27 Mar 2020 18:28:50 -0500 Subject: [PATCH] Fix caching bug --- src/bot.ts | 2 -- src/events/handle-typing-start.ts | 21 --------------------- src/services/natural-language-commands.ts | 2 +- src/services/player.ts | 20 +++++++++++++------- 4 files changed, 14 insertions(+), 31 deletions(-) delete mode 100644 src/events/handle-typing-start.ts diff --git a/src/bot.ts b/src/bot.ts index d6874b7..296c06b 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -7,7 +7,6 @@ import Command from './commands'; import debug from './utils/debug'; import NaturalLanguage from './services/natural-language-commands'; import handleGuildCreate from './events/guild-create'; -import handleTypingStart from './events/handle-typing-start'; import handleVoiceStateUpdate from './events/voice-state-update'; import errorMsg from './utils/error-msg'; import {isUserInVoice} from './utils/channels'; @@ -105,7 +104,6 @@ export default class { // Register event handlers this.client.on('guildCreate', handleGuildCreate); - this.client.on('typingStart', handleTypingStart); this.client.on('voiceStateUpdate', handleVoiceStateUpdate); return this.client.login(this.token); diff --git a/src/events/handle-typing-start.ts b/src/events/handle-typing-start.ts deleted file mode 100644 index 603d2bd..0000000 --- a/src/events/handle-typing-start.ts +++ /dev/null @@ -1,21 +0,0 @@ -import {Channel, TextChannel, PartialDMChannel, User, PartialUser} from 'discord.js'; - -const WAIT_TIME_SECONDS = 12; - -export default (channel: Channel | PartialDMChannel, user: User | PartialUser): void => { - if (channel.type !== 'text') { - return; - } - - const textChannel = channel as TextChannel; - - setTimeout(async () => { - if (user.typingIn(channel)) { - const msg = await textChannel.send(`take your time why don'tcha <@${user.id}>`); - - setTimeout(async () => { - await msg.delete(); - }, 2000); - } - }, WAIT_TIME_SECONDS * 1000); // Discord sends typing updates every 10s -}; diff --git a/src/services/natural-language-commands.ts b/src/services/natural-language-commands.ts index 104cd4f..fa2b119 100644 --- a/src/services/natural-language-commands.ts +++ b/src/services/natural-language-commands.ts @@ -33,7 +33,7 @@ export default class { if (msg.content.toLowerCase().includes('bears')) { await Promise.all([ msg.channel.send('F*** THE BEARS'), - this.playClip(msg.guild!, {title: 'GO PACKERS!', artist: 'Charlie Berens', url: 'https://www.youtube.com/watch?v=UaqlE9Pyy_Q', length: 385, playlist: null, isLive: false}, 358, 5) + this.playClip(msg.guild!, {title: 'GO PACKERS!', artist: 'Charlie Berens', url: 'https://www.youtube.com/watch?v=UaqlE9Pyy_Q', length: 385, playlist: null, isLive: false}, 358, 5.5) ]); return true; diff --git a/src/services/player.ts b/src/services/player.ts index 074fe5d..6e4a9e9 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -294,8 +294,14 @@ export default class { const ffmpegInputOptions: string[] = []; let shouldCacheVideo = false; + let format: ytdl.videoFormat | undefined; + if (await this.isCached(url)) { ffmpegInput = cachedPath; + + if (options.seek) { + ffmpegInputOptions.push('-ss', options.seek.toString()); + } } else { // Not yet cached, must download const info = await ytdl.getInfo(url); @@ -304,7 +310,7 @@ export default class { const filter = (format: ytdl.videoFormat): boolean => format.codecs === 'opus' && format.container === 'webm' && format.audioSampleRate !== undefined && parseInt(format.audioSampleRate, 10) === 48000; - let format = formats.find(filter); + format = formats.find(filter); const nextBestFormat = (formats: ytdl.videoFormat[]): ytdl.videoFormat | undefined => { if (formats[0].live) { @@ -332,7 +338,7 @@ export default class { // Don't cache livestreams or long videos const MAX_CACHE_LENGTH_SECONDS = 30 * 60; // 30 minutes - shouldCacheVideo = !info.player_response.videoDetails.isLiveContent && parseInt(info.length_seconds, 10) < MAX_CACHE_LENGTH_SECONDS; + shouldCacheVideo = !info.player_response.videoDetails.isLiveContent && parseInt(info.length_seconds, 10) < MAX_CACHE_LENGTH_SECONDS && !options.seek; ffmpegInputOptions.push(...[ '-reconnect', @@ -342,11 +348,11 @@ export default class { '-reconnect_delay_max', '5' ]); - } - // Add seek parameter if necessary - if (options.seek) { - ffmpegInputOptions.push('-ss', options.seek.toString()); + if (options.seek) { + // Fudge seek position since FFMPEG doesn't do a great job + ffmpegInputOptions.push('-ss', (options.seek + 7).toString()); + } } // Create stream and pipe to capacitor @@ -372,7 +378,7 @@ export default class { const cacheStream = createWriteStream(cacheTempPath); cacheStream.on('finish', async () => { - // Only move if size is non-zero (may have errored out) + // Only move if size is non-zero (may have errored out) const stats = await fs.stat(cacheTempPath); if (stats.size !== 0) {