From 7132bd75d082f3af04bcf9f4037471cfaeaab4b1 Mon Sep 17 00:00:00 2001 From: Zerio Date: Sat, 2 Oct 2021 10:48:17 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20New=20command=20search?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- commands/music/search.js | 67 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 commands/music/search.js diff --git a/commands/music/search.js b/commands/music/search.js new file mode 100644 index 0000000..18109da --- /dev/null +++ b/commands/music/search.js @@ -0,0 +1,67 @@ +const { MessageEmbed } = require('discord.js'); +const { QueryType } = require('discord-player'); + +module.exports = { + name: 'search', + aliases: ['sh'], + utilisation: '{prefix}search [song name]', + voiceChannel: true, + + async execute(client, message, args) { + if (!args[0]) return message.channel.send(`Please enter a valid search ${message.author}... try again ? ❌`); + + const res = await player.search(args.join(' '), { + requestedBy: message.member, + searchEngine: QueryType.AUTO + }); + + if (!res || !res.tracks.length) return message.channel.send(`No results found ${message.author}... try again ? ❌`); + + const queue = await player.createQueue(message.guild, { + metadata: message.channel + }); + + const embed = new MessageEmbed(); + + embed.setColor('RED'); + embed.setAuthor(`Results for ${args.join(' ')}`, client.user.displayAvatarURL({ size: 1024, dynamic: true })); + + const maxTracks = res.tracks.slice(0, 10); + + embed.setDescription(`${maxTracks.map((track, i) => `**${i + 1}**. ${track.title} | ${track.author}`).join('\n')}\n\nSelect choice between **1** and **${maxTracks.length}** or **cancel** ⬇️`); + + embed.setTimestamp(); + embed.setFooter('Music comes first - Made with heart by Zerio ❤️', message.author.avatarURL({ dynamic: true })); + + message.channel.send({ embeds: [embed] }); + + const collector = message.channel.createMessageCollector({ + time: 15000, + errors: ['time'], + filter: m => m.author.id === message.author.id + }); + + collector.on('collect', async (query) => { + if (query.content.toLowerCase() === 'cancel') return message.channel.send(`Search cancelled ✅`) && collector.stop(); + + const value = parseInt(query.content); + + if (!value || value <= 0 || value > maxTracks.length) return message.channel.send(`Invalid response, try a value between **1** and **${maxTracks.length}** or **cancel**... try again ? ❌`); + + collector.stop(); + + try { + if (!queue.connection) await queue.connect(message.member.voice.channel); + } catch { + await player.deleteQueue(message.guild.id); + return message.channel.send(`I can't join the voice channel ${message.author}... try again ? ❌`); + } + + await message.channel.send(`Loading your search... 🎧`); + + queue.addTrack(res.tracks[query.content - 1]); + + if (!queue.playing) await queue.play(); + }); + }, +}; \ No newline at end of file