Go Packers

This commit is contained in:
Max Isom 2020-03-18 17:15:45 -05:00
parent e57d86d7cc
commit 6a02088b04
5 changed files with 139 additions and 57 deletions

View file

@ -5,18 +5,21 @@ import {Settings, Shortcut} from './models';
import container from './inversify.config'; import container from './inversify.config';
import Command from './commands'; import Command from './commands';
import debug from './utils/debug'; import debug from './utils/debug';
import NaturalLanguage from './services/natural-language-commands';
import handleGuildCreate from './events/guild-create'; import handleGuildCreate from './events/guild-create';
import handleVoiceStateUpdate from './events/voice-state-update'; import handleVoiceStateUpdate from './events/voice-state-update';
@injectable() @injectable()
export default class { export default class {
private readonly client: Client; private readonly client: Client;
private readonly naturalLanguage: NaturalLanguage;
private readonly token: string; private readonly token: string;
private readonly clientId: string; private readonly clientId: string;
private readonly commands!: Collection<string, Command>; private readonly commands!: Collection<string, Command>;
constructor(@inject(TYPES.Client) client: Client, @inject(TYPES.Config.DISCORD_TOKEN) token: string, @inject(TYPES.Config.DISCORD_CLIENT_ID) clientId: string) { constructor(@inject(TYPES.Client) client: Client, @inject(TYPES.Services.NaturalLanguage) naturalLanguage: NaturalLanguage, @inject(TYPES.Config.DISCORD_TOKEN) token: string, @inject(TYPES.Config.DISCORD_CLIENT_ID) clientId: string) {
this.client = client; this.client = client;
this.naturalLanguage = naturalLanguage;
this.token = token; this.token = token;
this.clientId = clientId; this.clientId = clientId;
this.commands = new Collection(); this.commands = new Collection();
@ -41,10 +44,8 @@ export default class {
return this.client.emit('guildCreate', msg.guild); return this.client.emit('guildCreate', msg.guild);
} }
if (msg.content.startsWith('say') && msg.content.endsWith('muse')) { if (await this.naturalLanguage.execute(msg)) {
const res = msg.content.slice(3, msg.content.indexOf('muse')).trim(); // Natural language command handled message
await msg.channel.send(res);
return; return;
} }

View file

@ -21,6 +21,7 @@ import QueueManager from './managers/queue';
// Helpers // Helpers
import GetSongs from './services/get-songs'; import GetSongs from './services/get-songs';
import NaturalLanguage from './services/natural-language-commands';
// Comands // Comands
import Command from './commands'; import Command from './commands';
@ -49,6 +50,7 @@ container.bind<QueueManager>(TYPES.Managers.Queue).to(QueueManager).inSingletonS
// Helpers // Helpers
container.bind<GetSongs>(TYPES.Services.GetSongs).to(GetSongs).inSingletonScope(); container.bind<GetSongs>(TYPES.Services.GetSongs).to(GetSongs).inSingletonScope();
container.bind<NaturalLanguage>(TYPES.Services.NaturalLanguage).to(NaturalLanguage).inSingletonScope();
// Commands // Commands
container.bind<Command>(TYPES.Command).to(Clear).inSingletonScope(); container.bind<Command>(TYPES.Command).to(Clear).inSingletonScope();

View file

@ -0,0 +1,76 @@
import {inject, injectable} from 'inversify';
import {Message} from 'discord.js';
import {TYPES} from '../types';
import PlayerManager from '../managers/player';
import QueueManager from '../managers/queue';
import {getMostPopularVoiceChannel} from '../utils/channels';
@injectable()
export default class {
private readonly playerManager: PlayerManager;
private readonly queueManager: QueueManager;
constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager, @inject(TYPES.Managers.Queue) queueManager: QueueManager) {
this.playerManager = playerManager;
this.queueManager = queueManager;
}
async execute(msg: Message): Promise<boolean> {
if (msg.content.startsWith('say') && msg.content.endsWith('muse')) {
const res = msg.content.slice(3, msg.content.indexOf('muse')).trim();
await msg.channel.send(res);
return true;
}
if (msg.content.includes('packers')) {
const queue = this.queueManager.get(msg.guild!.id);
const player = this.playerManager.get(msg.guild!.id);
const [channel, n] = getMostPopularVoiceChannel(msg.guild!);
await msg.channel.send('GO PACKERS GO!!!');
if (!player.voiceConnection && n === 0) {
return false;
}
if (!player.voiceConnection) {
await player.connect(channel);
}
const isPlaying = queue.getCurrent();
let oldPosition = 0;
queue.add({title: 'GO PACKERS!', artist: 'Unknown', url: 'https://www.youtube.com/watch?v=qkdtID7mY3E', length: 204, playlist: null, isLive: false});
if (isPlaying) {
oldPosition = player.getPosition();
queue.forward();
}
await player.seek(8);
return new Promise((resolve, reject) => {
try {
setTimeout(async () => {
if (isPlaying) {
queue.back();
await player.seek(oldPosition);
} else {
queue.forward();
player.disconnect();
}
resolve(true);
}, 10000);
} catch (error) {
reject(error);
}
});
}
return false;
}
}

View file

@ -68,12 +68,8 @@ export default class {
throw new Error('Seek position is outside the range of the song.'); 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 {
const stream = await this.getStream(currentSong.url, {seek: positionSeconds}); const stream = await this.getStream(currentSong.url, {seek: positionSeconds});
this.dispatcher = this.voiceConnection.play(stream, {type: 'webm/opus'}); this.dispatcher = this.voiceConnection.play(stream, {type: 'webm/opus'});
}
this.attachListeners(); this.attachListeners();
this.startTrackingPosition(positionSeconds); this.startTrackingPosition(positionSeconds);
@ -107,12 +103,8 @@ export default class {
return; return;
} }
if (await this.isCached(currentSong.url)) {
this.dispatcher = this.voiceConnection.play(this.getCachedPath(currentSong.url));
} else {
const stream = await this.getStream(currentSong.url); const stream = await this.getStream(currentSong.url);
this.dispatcher = this.voiceConnection.play(stream, {type: 'webm/opus'}); this.dispatcher = this.voiceConnection.play(stream, {type: 'webm/opus'});
}
this.attachListeners(); this.attachListeners();
@ -154,13 +146,16 @@ export default class {
} }
} }
private async getStream(url: string, options: {seek?: number} = {}): Promise<Readable|string> { private async getStream(url: string, options: {seek?: number} = {}): Promise<Readable> {
const cachedPath = this.getCachedPath(url); const cachedPath = this.getCachedPath(url);
if (await this.isCached(url)) { let ffmpegInput = '';
return cachedPath; const ffmpegInputOptions = [];
} let shouldCacheVideo = false;
if (await this.isCached(url)) {
ffmpegInput = cachedPath;
} else {
// Not yet cached, must download // Not yet cached, must download
const info = await ytdl.getInfo(url); const info = await ytdl.getInfo(url);
@ -192,29 +187,36 @@ export default class {
} }
} }
const inputOptions = [ ffmpegInput = format.url;
// 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;
ffmpegInputOptions.push(...[
'-reconnect', '-reconnect',
'1', '1',
'-reconnect_streamed', '-reconnect_streamed',
'1', '1',
'-reconnect_delay_max', '-reconnect_delay_max',
'5' '5'
]; ]);
if (options.seek) {
inputOptions.push('-ss', options.seek.toString());
} }
const youtubeStream = ffmpeg(format.url).inputOptions(inputOptions).noVideo().audioCodec('libopus').outputFormat('webm').pipe() as PassThrough; // Add seek parameter if necessary
if (options.seek) {
ffmpegInputOptions.push('-ss', options.seek.toString());
}
// Create stream and pipe to capacitor
const youtubeStream = ffmpeg(ffmpegInput).inputOptions(ffmpegInputOptions).noVideo().audioCodec('libopus').outputFormat('webm').pipe() as PassThrough;
const capacitor = new WriteStream(); const capacitor = new WriteStream();
youtubeStream.pipe(capacitor); youtubeStream.pipe(capacitor);
// Don't cache livestreams or long videos // Cache video if necessary
const MAX_CACHE_LENGTH_SECONDS = 30 * 60; // 30 minutes if (shouldCacheVideo) {
if (!info.player_response.videoDetails.isLiveContent && parseInt(info.length_seconds, 10) < MAX_CACHE_LENGTH_SECONDS) {
const cacheTempPath = this.getCachedPathTemp(url); const cacheTempPath = this.getCachedPathTemp(url);
const cacheStream = createWriteStream(cacheTempPath); const cacheStream = createWriteStream(cacheTempPath);

View file

@ -18,6 +18,7 @@ export const TYPES = {
Queue: Symbol('QueueManager') Queue: Symbol('QueueManager')
}, },
Services: { Services: {
GetSongs: Symbol('GetSongs') GetSongs: Symbol('GetSongs'),
NaturalLanguage: Symbol('NaturalLanguage')
} }
}; };