all repos — simple-discord-music-bot @ 740c3a8bc78d682b36adacd3beaa08c338ed2ccb

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

commands/play.js (view raw)

 1const { SlashCommandBuilder } = require('discord.js');
 2const play = require('play-dl');
 3const { playUrls, getChannel } = require('../functions/music');
 4
 5module.exports = {
 6    data: new SlashCommandBuilder()
 7        .setName('play')
 8        .setDescription('Play something off YouTube.')
 9        .addStringOption((option) => option
10            .setName('query')
11            .setDescription('YouTube URL or search query')
12            .setRequired(true),
13        ),
14
15    async execute(interaction) {
16        const channel = await getChannel(interaction);
17        if (typeof channel == 'string')
18            return await interaction.reply({ content: channel, ephemeral: true });
19
20        await interaction.deferReply();
21        const url = interaction.options.getString('query');
22
23        let video, yt_info;
24        switch (play.yt_validate(url)) {
25            case 'video':
26                playUrls([url], channel);
27                return await interaction.editReply(`Added ${url} to queue.`);
28
29            case 'search':
30                yt_info = await play.search(url, { source: { youtube: 'video' }, limit: 1 });
31
32                if (yt_info.length === 0)
33                    return await interaction.editReply('No results found.');
34
35                video = yt_info[0];
36                playUrls([video.url], channel);
37                return await interaction.editReply(`Added ${video.url} to queue.`);
38
39            case 'playlist':
40                const playlist = await play.playlist_info(url, { incomplete : true });
41                const videos = await playlist.all_videos();
42                const urls = videos.map((e) => e.url);
43                result = await playUrls(urls, channel);
44                if (result)
45                    return await interaction.editReply(`Added ${urls.length} videos from the following playlist: ${playlist.title}.`);
46                else
47                    return await interaction.editReply(`Could not add playlist.`);
48            default:
49                return await interaction.editReply('Not supported.');
50        }
51    },
52};