commands/outro.js (view raw)
1const path = require('node:path');
2const { SlashCommandBuilder } = require('discord.js');
3const { createAudioResource, createAudioPlayer, NoSubscriberBehavior, joinVoiceChannel, AudioPlayerStatus } = require('@discordjs/voice');
4
5const OUTRO_PATH = path.join(process.cwd(), 'resources', 'outro.mp3');
6
7function get_player(resource) {
8
9 const player = createAudioPlayer({
10 behaviors: {
11 noSubscriber: NoSubscriberBehavior.Pause,
12 },
13 });
14
15 player.on('error', error => {
16 console.error(`Error: ${error.message} with resource ${error.resource.metadata.title}`);
17 });
18
19 player.on(AudioPlayerStatus.Idle, () => {
20 player.stop();
21 player.subscribers.forEach(element => {
22 element.connection.disconnect();
23 });
24 });
25
26 player.play(createAudioResource(resource));
27 return player;
28}
29
30module.exports = {
31 data: new SlashCommandBuilder().setName('outro').setDescription('Leave with an outro.'),
32 async execute(interaction) {
33
34 const member = interaction.member;
35
36 if (!member) return await interaction.reply('Please use this in your current server.');
37
38 const user_connection = member.voice;
39 const channel = user_connection.channel;
40
41 if (!channel) return await interaction.reply('You\'re not in a voice channel.');
42
43 const guild = channel.guild;
44 const bot_connection = joinVoiceChannel({ channelId: channel.id, guildId: guild.id, adapterCreator: guild.voiceAdapterCreator });
45 const player = get_player(OUTRO_PATH);
46
47 bot_connection.subscribe(player);
48 setTimeout(() => user_connection.disconnect(), 20_000);
49 return await interaction.reply('Prepare for takeoff.');
50 },
51};