From ad60f0c734349a1988109aac1e1210ee686adbc8 Mon Sep 17 00:00:00 2001 From: Mex <79592017+XstreamSpeed@users.noreply.github.com> Date: Sat, 22 Oct 2022 21:36:33 +0530 Subject: [PATCH 1/6] Add autocomplete to play.js - interactionCreate.js --- events/interactionCreate.js | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/events/interactionCreate.js b/events/interactionCreate.js index 1396e7355..db8214c03 100644 --- a/events/interactionCreate.js +++ b/events/interactionCreate.js @@ -6,6 +6,61 @@ const Controller = require("../util/Controller"); * @param {import("discord.js").Interaction}interaction */ module.exports = (client, interaction) => { + +if (interaction.isAutocomplete()) { + switch (interaction.commandName) { + case 'play': + /** + * @type {import("discord.js").AutocompleteFocusedOption} + */ + const focused = interaction.options.getFocused(true); + + if (focused.name === "query") { + if (focused.value === "") return; + /** + * @type {SearchResult} + */ + const result = await client.manager.search( + focused.value, + interaction.user + ); + + if (result.loadType === "TRACK_LOADED" || "SEARCH_RESULT") { + /** + * @type {Track[]} + */ + const sliced = result.tracks.slice(0, 5).sort(); + + if ( + focused.value.match( + /(?:https:\/\/open\.spotify\.com\/|spotify:)(?:.+)?(track|playlist|artist|episode|show|album)[\/:]([A-Za-z0-9]+)/ || + /^(?:https?:\/\/|)?(?:www\.)?deezer\.com\/(?:\w{2}\/)?(track|album|playlist)\/(\d+)/ || + /^((?:https?:)\/\/)?((?:deezer)\.)?((?:page.link))\/([a-zA-Z0-9]+)/ || + /(?:https:\/\/music\.apple\.com\/)(?:\w{2}\/)?(track|album|playlist)/g || + /(http(s|):\/\/music\.apple\.com\/..\/.....\/.*\/([0-9]){1,})\?i=([0-9]){1,}/gim || + /(?:https?:\/\/)?(?:www.|web.|m.)?(facebook|fb).(com|watch)\/(?:video.php\?v=\d+|(\S+)|photo.php\?v=\d+|\?v=\d+)|\S+\/videos\/((\S+)\/(\d+)|(\d+))\/?/g + ) + ) { + await interaction.respond( + sliced.map((track) => ({ + name: track.title, + value: focused.value, + })) + ); + return; + } else { + await interaction.respond( + sliced.map((track) => ({ + name: track.title, + value: track.uri, + })) + ); + } + } else if (result.loadType === "LOAD_FAILED" || "NO_MATCHES") + return; + } + } + } if (interaction.isCommand()) { let command = client.slashCommands.find( (x) => x.name == interaction.commandName, From 5c42a3933277cb33c78d71a8b1d0602fa97d255a Mon Sep 17 00:00:00 2001 From: Mex <79592017+XstreamSpeed@users.noreply.github.com> Date: Sat, 22 Oct 2022 21:40:36 +0530 Subject: [PATCH 2/6] Enable autocompletion in the command itself --- commands/slash/play.js | 1 + 1 file changed, 1 insertion(+) diff --git a/commands/slash/play.js b/commands/slash/play.js index 525913d4e..073eed620 100644 --- a/commands/slash/play.js +++ b/commands/slash/play.js @@ -10,6 +10,7 @@ const command = new SlashCommand() option .setName("query") .setDescription("What am I looking for?") + .setAutocomplete(true) .setRequired(true) ) .setRun(async (client, interaction, options) => { From 097f27f1dd2e9a7743f1bfa15b75fde05de06560 Mon Sep 17 00:00:00 2001 From: Mex <79592017+XstreamSpeed@users.noreply.github.com> Date: Sun, 23 Oct 2022 13:00:55 +0530 Subject: [PATCH 3/6] Make it an `async` function --- events/interactionCreate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/events/interactionCreate.js b/events/interactionCreate.js index db8214c03..9377293e4 100644 --- a/events/interactionCreate.js +++ b/events/interactionCreate.js @@ -5,7 +5,7 @@ const Controller = require("../util/Controller"); * @param {import("../lib/DiscordMusicBot")} client * @param {import("discord.js").Interaction}interaction */ -module.exports = (client, interaction) => { +module.exports = async(client, interaction) => { if (interaction.isAutocomplete()) { switch (interaction.commandName) { From 2d6442b4810b1e539b878e7fcbc52108676583da Mon Sep 17 00:00:00 2001 From: Mex <79592017+XstreamSpeed@users.noreply.github.com> Date: Sun, 30 Oct 2022 22:22:32 +0530 Subject: [PATCH 4/6] Update interactionCreate.js --- events/interactionCreate.js | 72 +++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/events/interactionCreate.js b/events/interactionCreate.js index 9377293e4..7d77d58ba 100644 --- a/events/interactionCreate.js +++ b/events/interactionCreate.js @@ -6,10 +6,43 @@ const Controller = require("../util/Controller"); * @param {import("discord.js").Interaction}interaction */ module.exports = async(client, interaction) => { + if (interaction.isCommand()) { + let command = client.slashCommands.find( + (x) => x.name == interaction.commandName, + ); + if (!command || !command.run) { + return interaction.reply( + "Sorry the command you used doesn't have any run function", + ); + } + client.commandsRan++; + command.run(client, interaction, interaction.options); + return; + } + + if (interaction.isContextMenu()) { + let command = client.contextCommands.find( + (x) => x.command.name == interaction.commandName, + ); + if (!command || !command.run) { + return interaction.reply( + "Sorry the command you used doesn't have any run function", + ); + } + client.commandsRan++; + command.run(client, interaction, interaction.options); + return; + } + + if (interaction.isButton()) { + if (interaction.customId.startsWith("controller")) { + Controller(client, interaction); + } + } if (interaction.isAutocomplete()) { switch (interaction.commandName) { - case 'play': + case 'play': /** * @type {import("discord.js").AutocompleteFocusedOption} */ @@ -31,7 +64,8 @@ if (interaction.isAutocomplete()) { */ const sliced = result.tracks.slice(0, 5).sort(); - if ( + //yall want this or not? + if ( focused.value.match( /(?:https:\/\/open\.spotify\.com\/|spotify:)(?:.+)?(track|playlist|artist|episode|show|album)[\/:]([A-Za-z0-9]+)/ || /^(?:https?:\/\/|)?(?:www\.)?deezer\.com\/(?:\w{2}\/)?(track|album|playlist)\/(\d+)/ || @@ -61,37 +95,5 @@ if (interaction.isAutocomplete()) { } } } - if (interaction.isCommand()) { - let command = client.slashCommands.find( - (x) => x.name == interaction.commandName, - ); - if (!command || !command.run) { - return interaction.reply( - "Sorry the command you used doesn't have any run function", - ); - } - client.commandsRan++; - command.run(client, interaction, interaction.options); - return; - } - - if (interaction.isContextMenu()) { - let command = client.contextCommands.find( - (x) => x.command.name == interaction.commandName, - ); - if (!command || !command.run) { - return interaction.reply( - "Sorry the command you used doesn't have any run function", - ); - } - client.commandsRan++; - command.run(client, interaction, interaction.options); - return; - } - - if (interaction.isButton()) { - if (interaction.customId.startsWith("controller")) { - Controller(client, interaction); - } - } + }; From 6074deae1bdb13043ad345db1ae5acabfebd606a Mon Sep 17 00:00:00 2001 From: LewdHuTao <90232327+LewdHuTao@users.noreply.github.com> Date: Sun, 22 Jan 2023 11:52:47 +0800 Subject: [PATCH 5/6] Update interactionCreate.js --- events/interactionCreate.js | 151 ++++++++++++++++-------------------- 1 file changed, 65 insertions(+), 86 deletions(-) diff --git a/events/interactionCreate.js b/events/interactionCreate.js index 7d77d58ba..6d2eb7bcb 100644 --- a/events/interactionCreate.js +++ b/events/interactionCreate.js @@ -1,99 +1,78 @@ const Controller = require("../util/Controller"); +const yt = require("youtube-sr").default; /** * * @param {import("../lib/DiscordMusicBot")} client * @param {import("discord.js").Interaction}interaction */ -module.exports = async(client, interaction) => { - if (interaction.isCommand()) { - let command = client.slashCommands.find( - (x) => x.name == interaction.commandName, - ); - if (!command || !command.run) { - return interaction.reply( - "Sorry the command you used doesn't have any run function", - ); - } - client.commandsRan++; - command.run(client, interaction, interaction.options); - return; - } - - if (interaction.isContextMenu()) { - let command = client.contextCommands.find( - (x) => x.command.name == interaction.commandName, - ); - if (!command || !command.run) { - return interaction.reply( - "Sorry the command you used doesn't have any run function", - ); - } - client.commandsRan++; - command.run(client, interaction, interaction.options); - return; - } - - if (interaction.isButton()) { - if (interaction.customId.startsWith("controller")) { - Controller(client, interaction); - } - } - -if (interaction.isAutocomplete()) { - switch (interaction.commandName) { - case 'play': - /** - * @type {import("discord.js").AutocompleteFocusedOption} - */ - const focused = interaction.options.getFocused(true); +module.exports = async (client, interaction) => { + if (interaction.isCommand()) { + let command = client.slashCommands.find( + (x) => x.name == interaction.commandName, + ); + if (!command || !command.run) { + return interaction.reply( + "Sorry the command you used doesn't have any run function", + ); + } + client.commandsRan++; + command.run(client, interaction, interaction.options); + return; + } - if (focused.name === "query") { - if (focused.value === "") return; - /** - * @type {SearchResult} - */ - const result = await client.manager.search( - focused.value, - interaction.user + if (interaction.isContextMenu()) { + let command = client.contextCommands.find( + (x) => x.command.name == interaction.commandName, + ); + if (!command || !command.run) { + return interaction.reply( + "Sorry the command you used doesn't have any run function", ); + } + client.commandsRan++; + command.run(client, interaction, interaction.options); + return; + } + + if (interaction.isButton()) { + if (interaction.customId.startsWith("controller")) { + Controller(client, interaction); + } + } + + if (interaction.isAutocomplete()) { + const url = interaction.options.getString("query") + if (url === "") return; + + const match = [ + /^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube(-nocookie)?\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$/, + /^(?:spotify:|https:\/\/[a-z]+\.spotify\.com\/(track\/|user\/(.*)\/playlist\/|playlist\/))(.*)$/, + /^https?:\/\/(?:www\.)?deezer\.com\/[a-z]+\/(track|album|playlist)\/(\d+)$/, + /^(?:(https?):\/\/)?(?:(?:www|m)\.)?(soundcloud\.com|snd\.sc)\/(.*)$/, + /(?:https:\/\/music\.apple\.com\/)(?:.+)?(artist|album|music-video|playlist)\/([\w\-\.]+(\/)+[\w\-\.]+|[^&]+)\/([\w\-\.]+(\/)+[\w\-\.]+|[^&]+)/ + ].some(function (match) { + return match.test(url) == true; + }); - if (result.loadType === "TRACK_LOADED" || "SEARCH_RESULT") { - /** - * @type {Track[]} - */ - const sliced = result.tracks.slice(0, 5).sort(); + async function checkRegex() { + if (match == true) { + let choice = [] + choice.push({ name: url, value: url }) + await interaction.respond(choice).catch(() => { }); + } + } - //yall want this or not? - if ( - focused.value.match( - /(?:https:\/\/open\.spotify\.com\/|spotify:)(?:.+)?(track|playlist|artist|episode|show|album)[\/:]([A-Za-z0-9]+)/ || - /^(?:https?:\/\/|)?(?:www\.)?deezer\.com\/(?:\w{2}\/)?(track|album|playlist)\/(\d+)/ || - /^((?:https?:)\/\/)?((?:deezer)\.)?((?:page.link))\/([a-zA-Z0-9]+)/ || - /(?:https:\/\/music\.apple\.com\/)(?:\w{2}\/)?(track|album|playlist)/g || - /(http(s|):\/\/music\.apple\.com\/..\/.....\/.*\/([0-9]){1,})\?i=([0-9]){1,}/gim || - /(?:https?:\/\/)?(?:www.|web.|m.)?(facebook|fb).(com|watch)\/(?:video.php\?v=\d+|(\S+)|photo.php\?v=\d+|\?v=\d+)|\S+\/videos\/((\S+)\/(\d+)|(\d+))\/?/g - ) - ) { - await interaction.respond( - sliced.map((track) => ({ - name: track.title, - value: focused.value, - })) - ); - return; - } else { - await interaction.respond( - sliced.map((track) => ({ - name: track.title, - value: track.uri, - })) - ); - } - } else if (result.loadType === "LOAD_FAILED" || "NO_MATCHES") - return; - } - } - } + const Random = "ytsearch"[Math.floor(Math.random() * "ytsearch".length)]; + if (interaction.commandName == "play") { + checkRegex() + let choice = [] + await yt.search(url || Random, { safeSearch: false, limit: 25 }).then(result => { + result.forEach(x => { choice.push({ name: x.title, value: x.url }) }) + }); + return await interaction.respond(choice).catch(() => { }); + } else if (result.loadType === "LOAD_FAILED" || "NO_MATCHES") + return; + } }; From 119047b54930970278d7a44b9150ba63bce812c9 Mon Sep 17 00:00:00 2001 From: LewdHuTao <90232327+LewdHuTao@users.noreply.github.com> Date: Sun, 22 Jan 2023 11:53:53 +0800 Subject: [PATCH 6/6] Update package.json --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 3eff4673a..f8df8837d 100644 --- a/package.json +++ b/package.json @@ -68,7 +68,8 @@ "pretty-ms": "^7.0.1", "systeminformation": "^5.9.12", "winston": "^3.3.3", - "lyrics-searcher-musixmatch": "1.0.2" + "lyrics-searcher-musixmatch": "1.0.2", + "youtube-sr": "^4.3.4" }, "devDependencies": { "prettier": "2.6.2"