mirror of
https://github.com/BluemediaGER/muse.git
synced 2024-11-14 13:45:28 +01:00
93 lines
2.5 KiB
TypeScript
93 lines
2.5 KiB
TypeScript
import {inject, injectable} from 'inversify';
|
|
import {Message, Guild} from 'discord.js';
|
|
import {TYPES} from '../types';
|
|
import PlayerManager from '../managers/player';
|
|
import {QueuedSong} from '../services/player';
|
|
import {getMostPopularVoiceChannel} from '../utils/channels';
|
|
|
|
@injectable()
|
|
export default class {
|
|
private readonly playerManager: PlayerManager;
|
|
|
|
constructor(@inject(TYPES.Managers.Player) playerManager: PlayerManager) {
|
|
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)
|
|
]);
|
|
|
|
return true;
|
|
}
|
|
|
|
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)
|
|
]);
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private async playClip(guild: Guild, song: QueuedSong, position: number, duration: number): Promise<void> {
|
|
const player = this.playerManager.get(guild.id);
|
|
|
|
const [channel, n] = getMostPopularVoiceChannel(guild);
|
|
|
|
if (!player.voiceConnection && n === 0) {
|
|
return;
|
|
}
|
|
|
|
if (!player.voiceConnection) {
|
|
await player.connect(channel);
|
|
}
|
|
|
|
const isPlaying = player.getCurrent() !== null;
|
|
let oldPosition = 0;
|
|
|
|
player.add(song, {immediate: true});
|
|
|
|
if (isPlaying) {
|
|
oldPosition = player.getPosition();
|
|
|
|
player.manualForward();
|
|
}
|
|
|
|
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);
|
|
} catch (error) {
|
|
reject(error);
|
|
}
|
|
});
|
|
}
|
|
}
|