From dad836e6302ff37c6c88cd2c539bb208a5beac64 Mon Sep 17 00:00:00 2001 From: mudkipscience Date: Tue, 14 Apr 2020 15:28:25 +1000 Subject: [PATCH] (WIP) fredboat style selection menu for music --- commands/forceskip.js | 2 +- commands/play.js | 4 ++-- helpers/music.js | 55 +++++++++++++++++++++++++++++++++++-------- 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/commands/forceskip.js b/commands/forceskip.js index d79a431..7a72b8f 100644 --- a/commands/forceskip.js +++ b/commands/forceskip.js @@ -2,7 +2,7 @@ exports.conf = { enabled: true, guildOnly: true, aliases: [], - permLevel: 'Moderator', + permLevel: 'User', requiredPerms: [], cooldown: 2000 } diff --git a/commands/play.js b/commands/play.js index a294716..2fe5497 100644 --- a/commands/play.js +++ b/commands/play.js @@ -15,6 +15,6 @@ exports.help = { parameters: '[query] - A query to find video by or a link to the video.' } -exports.run = async (client, message, [...args], level, data) => { - await client.music.play(message, args) +exports.run = async (client, message, args, level, data) => { + await client.music.play(message, args.join(' ')) } diff --git a/helpers/music.js b/helpers/music.js index 732e0b7..11ef0dd 100644 --- a/helpers/music.js +++ b/helpers/music.js @@ -1,5 +1,6 @@ const ytdl = require('ytdl-core-discord') const fetch = require('node-fetch') +const { MessageEmbed } = require('discord.js') module.exports = client => { client.music = { guilds: {} } @@ -63,16 +64,16 @@ module.exports = client => { response = await fetch('https://www.googleapis.com/youtube/v3/search?key=' + client.config.keys.yt + '&part=id,snippet&maxResults=1&type=video&id=' + id) } else { // TODO: replace this workaround - response = await fetch('https://www.googleapis.com/youtube/v3/search?key=' + client.config.keys.yt + '&part=id,snippet&maxResults=1&type=video&q=**' + encodeURIComponent(query) + '**') + response = await fetch('https://invidio.us/api/v1/search?q=' + encodeURIComponent(query) + '**') } const parsed = await response.json() - if (parsed.items) { - const video = parsed.items[0] + if (parsed[0]) { + const videos = parsed - if (video) { - return video + if (videos) { + return videos } else { return false } @@ -91,12 +92,13 @@ module.exports = client => { const vc = message.member.voice.channel let video + let videos if (!ignoreQueue) { - video = await client.music.getVideoByQuery(query) + videos = await client.music.getVideoByQuery(query) } - if (video || ignoreQueue) { + if (videos || ignoreQueue) { if (!ignoreQueue) { // Fix the bot if somehow broken // music "playing", nothing in queue @@ -108,13 +110,44 @@ module.exports = client => { guild.queue = [] } + if (videos[1]) { + let output = '' + let i = 0 + for (i = 0; i < 5; i++) { + if (!videos[i]) break + output += `\`${i + 1}:\` **[${videos[i].title}](https://www.youtube.com/watch?v=${videos[i].videoId})** \`[${client.createTimestamp(videos[i].lengthSeconds)}]\`\n` + } + + const embed = new MessageEmbed() + embed.setTitle('Please reply with a number `1-' + i + '` to select which song you want to add to the queue.') + embed.setColor(client.embedColour(message.guild)) + embed.setDescription(output) + const selection = await client.awaitReply(message, embed) + + for (i = 0; i < 4; i++) { + if ([`${i + 1}`].includes(selection)) { + if (!videos[i]) { + return message.channel.send('Invalid selection') + } + + video = videos[i] + + break + } + } + } + + if (!video) { + video = videos[0] + } + // Add video to queue guild.queue.push({ video: video, requestedBy: message.member.id }) } // Figure out if the bot should add it to queue or play it right now if (guild.playing) { - message.reply('added **' + video.snippet.title + '** to the queue') + message.reply('added **' + video.title + '** to the queue') } else { guild.playing = true @@ -124,10 +157,12 @@ module.exports = client => { const v = guild.queue[0] - guild.dispatcher = connection.play(await ytdl(client.music.getLinkFromID(v.video.id.videoId), { highWaterMark: 1024 * 1024 * 32 }), { type: 'opus' }) + console.log(v.video) + + guild.dispatcher = connection.play(await ytdl(client.music.getLinkFromID(v.video.videoId), { highWaterMark: 1024 * 1024 * 32 }), { type: 'opus' }) guild.dispatcher.setVolume(0.25) - message.channel.send('Playing **' + v.video.snippet.title + '**') + message.channel.send('Playing **' + v.video.title + '**') // play next in queue on end guild.dispatcher.once('finish', () => {