From 8642d88ebb226d664cba79d2f4cbfb4a0afc1273 Mon Sep 17 00:00:00 2001 From: bignutty <3515180-bignutty@users.noreply.gitlab.com> Date: Wed, 7 Aug 2024 20:47:36 +0200 Subject: [PATCH] add /music-platforms --- .../slash/utils/music-platforms.js | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 commands/interaction/slash/utils/music-platforms.js diff --git a/commands/interaction/slash/utils/music-platforms.js b/commands/interaction/slash/utils/music-platforms.js new file mode 100644 index 0000000..4f34d02 --- /dev/null +++ b/commands/interaction/slash/utils/music-platforms.js @@ -0,0 +1,72 @@ +const { renderMusicButtons } = require('#utils/buttons'); +const { createEmbed } = require('#utils/embed'); +const { acknowledge } = require('#utils/interactions'); +const { editOrReply } = require('#utils/message'); + +const { ApplicationCommandOptionTypes } = require("detritus-client/lib/constants");; + +const superagent = require('superagent') + +const urlr = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/g + +module.exports = { + name: 'music-platforms', + description: 'Get links for a song across all streaming platforms.', + contexts: [ + 0, + 1, + 2 + ], + integrationTypes: [ + 1 + ], + options: [ + { + name: 'url', + description: 'Song URL.', + type: ApplicationCommandOptionTypes.TEXT, + required: true + }, + { + name: 'incognito', + description: 'Makes the response only visible to you.', + type: ApplicationCommandOptionTypes.BOOLEAN, + required: false, + default: false + } + ], + run: async (context, args) => { + try{ + await acknowledge(context, args.incognito); + + let urls = args.url.match(urlr) + if(urls){ + try{ + let songlink = await superagent.get(`https://api.song.link/v1-alpha.1/links?url=${encodeURIComponent(urls[0])}`) + let song = songlink.body.entitiesByUniqueId[songlink.body.entityUniqueId] + + let btns = renderMusicButtons(songlink.body.linksByPlatform) + return editOrReply(context, {embeds:[ + createEmbed("default", context, { + author: { + name: `${song.title} by ${song.artistName}`.substr(0,1000), + iconUrl: song.thumbnailUrl, + url: urls[0] + }, + footer: { + text: `powered by song.link • ${context.application.name}` + } + }) + ], components: btns }) + }catch(e){ + return editOrReply(context, createEmbed("warning", context, "No results found.")) + } + } else { + return editOrReply(context, createEmbed("warning", context, "No urls found.")) + } + }catch(e){ + console.log(e) + await editOrReply(context, createEmbed("error", context, "Unable to look up song url.")) + } + }, +}; \ No newline at end of file