muse/src/services/natural-language-commands.ts

93 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-03-18 23:15:45 +01:00
import {inject, injectable} from 'inversify';
import {Message, Guild} from 'discord.js';
2020-03-18 23:15:45 +01:00
import {TYPES} from '../types';
import PlayerManager from '../managers/player';
import {QueuedSong} from '../services/player';
2020-03-18 23:15:45 +01:00
import {getMostPopularVoiceChannel} from '../utils/channels';
@injectable()
export default class {
private readonly playerManager: PlayerManager;
2020-03-21 02:47:04 +01:00
constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) {
2020-03-18 23:15:45 +01:00
this.playerManager = playerManager;
}
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.toLowerCase().includes('packers')) {
await Promise.all([
msg.channel.send('GO PACKERS GO!!!'),
this.playClip(msg.guild!, {title: 'GO PACKERS!', artist: 'Unknown', url: 'https://www.youtube.com/watch?v=qkdtID7mY3E', length: 204, playlist: null, isLive: false}, 8, 10)
]);
2020-03-18 23:15:45 +01:00
return true;
}
2020-03-18 23:15:45 +01:00
if (msg.content.toLowerCase().includes('bears')) {
await Promise.all([
msg.channel.send('F*** THE BEARS'),
2020-03-28 00:28:50 +01:00
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)
]);
2020-03-18 23:15:45 +01:00
return true;
}
2020-03-18 23:15:45 +01:00
return false;
}
2020-03-18 23:15:45 +01:00
private async playClip(guild: Guild, song: QueuedSong, position: number, duration: number): Promise<void> {
const player = this.playerManager.get(guild.id);
2020-03-18 23:15:45 +01:00
const [channel, n] = getMostPopularVoiceChannel(guild);
2020-03-18 23:15:45 +01:00
if (!player.voiceConnection && n === 0) {
return;
}
2020-03-21 02:47:04 +01:00
if (!player.voiceConnection) {
await player.connect(channel);
}
2020-03-18 23:15:45 +01:00
const isPlaying = player.getCurrent() !== null;
let oldPosition = 0;
2020-03-18 23:15:45 +01:00
player.add(song, {immediate: true});
2020-03-24 01:40:54 +01:00
if (isPlaying) {
oldPosition = player.getPosition();
2020-03-18 23:15:45 +01:00
2021-04-23 18:30:31 +02:00
player.manualForward(1);
2020-03-18 23:15:45 +01:00
}
await player.seek(position);
return new Promise((resolve, reject) => {
try {
setTimeout(async () => {
if (player.getCurrent()?.title === song.title) {
player.removeCurrent();
if (isPlaying) {
await player.back();
await player.seek(oldPosition);
} else {
player.disconnect();
}
}
resolve();
}, duration * 1000);
2020-10-24 18:32:43 +02:00
} catch (error: unknown) {
reject(error);
}
});
2020-03-18 23:15:45 +01:00
}
}