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

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

functions/music.js (view raw)

 1const {
 2    createAudioResource,
 3    createAudioPlayer,
 4    joinVoiceChannel,
 5    AudioPlayerStatus,
 6} = require('@discordjs/voice');
 7const play = require('play-dl');
 8
 9module.exports = {
10    async playUrl(url, channel) {
11        if (!channel) {
12            console.log('Channel error:', channel);
13            return;
14        }
15        const stream = await play.stream(url);
16        const guild = channel.guild;
17        const connection = joinVoiceChannel({
18            channelId: channel.id,
19            guildId: guild.id,
20            adapterCreator: guild.voiceAdapterCreator,
21        });
22
23        player = createAudioPlayer();
24        player.on(AudioPlayerStatus.Idle, () => {
25            player.subscribers.forEach((element) => element.connection.disconnect());
26        });
27
28        player.play(createAudioResource(stream.stream, { inputType: stream.type }));
29        connection.subscribe(player);
30    },
31    async getChannel(interaction) {
32        const member = interaction.member;
33        if (!member)
34            return 'Please use this in your current server.';
35
36        const channel = member.voice.channel;
37        if (!channel)
38            return 'You\'re not in a voice channel.';
39
40        return channel;
41    },
42};