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 playStream(url, channel) {
32 if (!channel) {
33 console.log('Channel error:', channel);
34 return;
35 }
36 const guild = channel.guild;
37 const connection = joinVoiceChannel({
38 channelId: channel.id,
39 guildId: guild.id,
40 adapterCreator: guild.voiceAdapterCreator,
41 });
42 player = createAudioPlayer();
43 player.on(AudioPlayerStatus.Idle, () => {
44 player.subscribers.forEach((element) => element.connection.disconnect());
45 });
46
47 player.play(createAudioResource(url, { inputType: 'mp3' })); // 'opus', 'mp3'
48 connection.subscribe(player);
49 },
50 async getChannel(interaction) {
51 const member = interaction.member;
52 if (!member)
53 return 'Please use this in your current server.';
54
55 const channel = member.voice.channel;
56 if (!channel)
57 return 'You\'re not in a voice channel.';
58
59 return channel;
60 },
61};