all repos — simple-discord-music-bot @ 61364f5fe32e6420a82abd998c4a0baa264184e0

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

functions/music.js (view raw)

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