all repos — simple-discord-music-bot @ e21c6d80c7ffb903760e5c5d8f7f903ef2cd3309

A Discord bot making use of discord.js and play-yt.

commands/outro.js (view raw)

 1const { SlashCommandBuilder } = require('discord.js');
 2const { createAudioResource, createAudioPlayer, NoSubscriberBehavior, joinVoiceChannel, AudioPlayerStatus } = require('@discordjs/voice');
 3
 4const OUTRO_PATH = 'resources/outro.mp3';
 5
 6function get_player(resource) {
 7
 8	const player = createAudioPlayer({
 9		behaviors: {
10			noSubscriber: NoSubscriberBehavior.Pause,
11		},
12	});
13
14	player.on('error', error => {
15		console.error(`Error: ${error.message} with resource ${error.resource.metadata.title}`);
16	});
17
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().setName('outro').setDescription('Leave with an outro.'),
31	async execute(interaction) {
32
33		const member = interaction.member;
34
35		if (!member) return await interaction.reply('Please use this in your current server.');
36
37		const user_connection = member.voice;
38		const channel = user_connection.channel;
39
40		if (!channel) return await interaction.reply('You\'re not in a voice channel.');
41
42		const guild = channel.guild;
43		const bot_connection = joinVoiceChannel({ channelId: channel.id, guildId: guild.id, adapterCreator: guild.voiceAdapterCreator });
44		const player = get_player(OUTRO_PATH);
45
46		bot_connection.subscribe(player);
47		setTimeout(() => user_connection.disconnect(), 20_000);
48		return await interaction.reply('Prepare for takeoff.');
49	},
50};