commands/play.js (view raw)
1const { SlashCommandBuilder } = require('discord.js');
2const play = require('play-dl');
3const { playUrl, getChannel } = require('../functions/music');
4
5// const reg = /^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube(-nocookie)?\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$/;
6
7module.exports = {
8 data: new SlashCommandBuilder()
9 .setName('play')
10 .setDescription('Play something off YouTube.')
11 .addStringOption((option) => option
12 .setName('query')
13 .setDescription('YouTube URL or search query')
14 .setRequired(true),
15 ),
16
17 async execute(interaction) {
18 const channel = await getChannel(interaction);
19 if (typeof channel == 'string')
20 return await interaction.reply({ content: channel, ephemeral: true });
21
22 await interaction.deferReply();
23
24 // Get the YouTube URL or search query
25 const url = interaction.options.getString('query');
26
27 let video, yt_info;
28 switch (play.yt_validate(url)) {
29 case 'video':
30 video = { url: url };
31 break;
32
33 case 'search':
34 yt_info = await play.search(url, { source: { youtube: 'video' }, limit: 1 });
35
36 if (yt_info.length === 0)
37 return await interaction.editReply('No results found.');
38
39 video = yt_info[0];
40 break;
41
42 default:
43 return await interaction.editReply('Not supported.');
44 }
45 playUrl(video.url, channel);
46 return await interaction.editReply(`Playing ${video.url}`);
47 },
48};