commands/outro.js (view raw)
1const path = require('node:path');
2const { SlashCommandBuilder } = require('discord.js');
3const { createAudioResource, createAudioPlayer, joinVoiceChannel, NoSubscriberBehavior, AudioPlayerStatus } = require('@discordjs/voice');
4
5const outros = [
6 { name: 'Random!', value: 'random' },
7 { name: 'TheFatRat - Xenogenesis', value: 'thefatrat_xenogenesis.mp3' },
8 { name: 'OMFG - Hello', value: 'omfg_hello.mp3' },
9 { name: 'Pegboard Nerds - Disconnected', value: 'pegboard_nerds_disconnected.mp3' },
10 { name: 'Gym Class Heroes - Stereo Hearts', value: 'gym_class_heroes_stereo_hearts.mp3' },
11];
12
13function get_player(resource) {
14
15 const player = createAudioPlayer({ behaviors: { noSubscriber: NoSubscriberBehavior.Pause } });
16
17 player.on('error', error => console.error(`Error: ${error.message} with resource ${error.resource.metadata.title}`));
18 player.on(AudioPlayerStatus.Idle, () => {
19 player.stop();
20 player.subscribers.forEach(element => {
21 element.connection.disconnect();
22 });
23 });
24
25 player.play(createAudioResource(resource));
26 return player;
27}
28
29module.exports = {
30 data: new SlashCommandBuilder()
31 .setName('outro')
32 .setDescription('Leave with an outro.')
33 .addStringOption(option =>
34 option.setName('which')
35 .setDescription('Select which outro to play')
36 .setRequired(false)
37 .addChoices(...outros)),
38
39 async execute(interaction) {
40 const member = interaction.member;
41 if (!member) return await interaction.reply({ content: 'Please use this in your current server.', ephemeral: true });
42
43 const user_connection = member.voice;
44 const channel = user_connection.channel;
45 if (!channel) return await interaction.reply({ content: 'You\'re not in a voice channel.', ephemeral: true });
46
47 const guild = channel.guild;
48 const bot_connection = joinVoiceChannel({ channelId: channel.id, guildId: guild.id, adapterCreator: guild.voiceAdapterCreator });
49
50 const outro = interaction.options.getString('which');
51 let outro_file = outro ? outro : 'random';
52 if (outro_file == 'random')
53 outro_file = outros[Math.floor(Math.random() * (outros.length - 1)) + 1].value;
54
55 const song_path = path.join(process.cwd(), 'resources', outro_file);
56 bot_connection.subscribe(get_player(song_path));
57 setTimeout(() => user_connection.disconnect(), 20_000);
58
59 const outro_title = outros.find(element => element.value == outro_file).name;
60 return await interaction.reply({ content: `Prepare for takeoff with ${outro_title}!`, ephemeral: true });
61 },
62};