Fix caching bug

This commit is contained in:
Max Isom 2020-03-27 18:28:50 -05:00
parent 7703506aae
commit 7fcd9a6a7d
4 changed files with 14 additions and 31 deletions

View file

@ -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);

View file

@ -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
};

View file

@ -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;

View file

@ -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) {