all repos — simple-discord-music-bot @ 394cdf4f8bb25b2fdc6eb4a21d67aa1b4bff9db8

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 { 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    
19    channel = await getChannel(interaction);
20    if (typeof channel == "string")
21      return await interaction.reply({ content: channel, ephemeral: true });
22
23    await interaction.deferReply();
24
25    // Get the YouTube URL or search query
26    let url = interaction.options.getString("query");
27
28    let video;
29    switch (play.yt_validate(url)) {
30      case "video":
31        video = {url: url};
32        break;
33      
34      case "search":
35        yt_info = await play.search(url, { source : { youtube : "video" }, limit: 1 });
36
37        if(yt_info.length === 0)
38          return await interaction.editReply(`No results found.`);
39
40        video = yt_info[0];
41        break;
42    
43      default:
44        return await interaction.editReply(`Not supported.`);
45    }
46
47    playUrl(video.url, channel);
48    return await interaction.editReply(`Playing ${video.url}`);
49  },
50};