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

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

src/commands/play.ts (view raw)

 1import { BaseInteraction, ChatInputCommandInteraction, CommandInteraction, SlashCommandBuilder } from 'discord.js';
 2import play from 'play-dl';
 3import { playUrls, getChannel } from '../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: ChatInputCommandInteraction) {
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 opt = interaction.options;
22        const url = opt.getString('query');
23
24        let video, yt_info;
25        switch (play.yt_validate(url)) {
26            case 'video':
27                playUrls([url], channel);
28                return await interaction.editReply(`Added ${url} to queue.`);
29
30            case 'search':
31                yt_info = await play.search(url, { source: { youtube: 'video' }, limit: 1 });
32
33                if (yt_info.length === 0)
34                    return await interaction.editReply('No results found.');
35
36                video = yt_info[0];
37                playUrls([video.url], channel);
38                return await interaction.editReply(`Added ${video.url} to queue.`);
39
40            case 'playlist':
41                const playlist = await play.playlist_info(url, { incomplete : true });
42                const videos = await playlist.all_videos();
43                const urls = videos.map((e) => e.url);
44                const result = await playUrls(urls, channel);
45                if (result)
46                    return await interaction.editReply(`Added ${urls.length} videos from the following playlist: ${playlist.title}.`);
47                else
48                    return await interaction.editReply(`Could not add playlist.`);
49            default:
50                return await interaction.editReply('Not supported.');
51        }
52    },
53};