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

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

src/commands/play.ts (view raw)

 1import { ChatInputCommandInteraction, SlashCommandBuilder } from 'discord.js';
 2import play, { YouTubeVideo } from 'play-dl';
 3import { getQueue, formatTitle } from '../functions/music';
 4import { getChannel } from '../functions/voice';
 5
 6async function handleUserInput(input: string): Promise<YouTubeVideo[]> {
 7    try {
 8        switch (play.yt_validate(input)) {
 9            case 'video':
10                const info = await play.video_basic_info(input);
11                return [info.video_details];
12            case 'playlist':
13                const playlist = await play.playlist_info(input, { incomplete: true });
14                return await playlist.all_videos();
15            case 'search':
16                const results = await play.search(input, { source: { youtube: 'video' }, limit: 1 });
17                if (results.length > 0) return [results[0]];
18            default:
19                return [];
20        }
21    } catch (error) {
22        console.error(error);
23        return [];
24    }
25}
26
27module.exports = {
28    data: new SlashCommandBuilder()
29        .setName('play')
30        .setDescription('Play something off YouTube.')
31        .addStringOption((option) => option
32            .setName('query')
33            .setDescription('YouTube URL or search query')
34            .setRequired(true),
35        ),
36
37    async execute(interaction: ChatInputCommandInteraction) {
38        const channel = await getChannel(interaction);
39        if (typeof channel == 'string')
40            return await interaction.reply({ content: channel, ephemeral: true });
41
42        await interaction.deferReply();
43        const opt = interaction.options;
44        const input = opt.getString('query');
45        const yt_videos = await handleUserInput(input);
46        const queue = getQueue(interaction.guildId);
47        const added = await queue.addArray(yt_videos, channel);
48
49        switch (added.length) {
50            case 0:
51                return await interaction.editReply('No videos were added to the queue.');
52            case 1:
53                return await interaction.editReply(`Added ${formatTitle(added[0])} to queue.`);
54            default:
55                return await interaction.editReply(`Added ${added.length} videos to queue.`);
56        }
57    },
58};