From c8b54234fa934e5216796e8bf9bcd00064ccab7f Mon Sep 17 00:00:00 2001 From: Ethan <45918948+IchiiDev@users.noreply.github.com> Date: Fri, 4 Apr 2025 23:19:03 +0200 Subject: [PATCH] SpotifyShareCommands: add message argument like inbuilt commands (#3320) also cleans up the plugin's code Co-authored-by: Vendicated --- src/plugins/spotifyShareCommands/index.ts | 104 +++++++--------------- 1 file changed, 34 insertions(+), 70 deletions(-) diff --git a/src/plugins/spotifyShareCommands/index.ts b/src/plugins/spotifyShareCommands/index.ts index 8c485666..ed793963 100644 --- a/src/plugins/spotifyShareCommands/index.ts +++ b/src/plugins/spotifyShareCommands/index.ts @@ -16,8 +16,9 @@ * along with this program. If not, see . */ -import { ApplicationCommandInputType, sendBotMessage } from "@api/Commands"; +import { ApplicationCommandInputType, Command, findOption, OptionalMessageOption, sendBotMessage } from "@api/Commands"; import { Devs } from "@utils/constants"; +import { sendMessage } from "@utils/discord"; import definePlugin from "@utils/types"; import { findByPropsLazy } from "@webpack"; import { FluxDispatcher, MessageActions } from "@webpack/common"; @@ -55,21 +56,36 @@ interface Track { const Spotify = findByPropsLazy("getPlayerState"); const PendingReplyStore = findByPropsLazy("getPendingReply"); -function sendMessage(channelId, message) { - message = { - // The following are required to prevent Discord from throwing an error - invalidEmojis: [], - tts: false, - validNonShortcutEmojis: [], - ...message - }; - const reply = PendingReplyStore.getPendingReply(channelId); - MessageActions.sendMessage(channelId, message, void 0, MessageActions.getSendMessageOptionsForReply(reply)) - .then(() => { - if (reply) { - FluxDispatcher.dispatch({ type: "DELETE_PENDING_REPLY", channelId }); +function makeCommand(name: string, formatUrl: (track: Track) => string): Command { + return { + name, + description: `Share your current Spotify ${name} in chat`, + inputType: ApplicationCommandInputType.BUILT_IN, + options: [OptionalMessageOption], + execute(options, { channel }) { + const track: Track | null = Spotify.getTrack(); + if (!track) { + return sendBotMessage(channel.id, { + content: "You're not listening to any music." + }); } - }); + + const data = formatUrl(track); + const message = findOption(options, "message"); + + // Note: Due to how Discord handles commands, we need to manually create and send the message + + sendMessage( + channel.id, + { content: message ? `${message} ${data}` : data }, + false, + MessageActions.getSendMessageOptionsForReply(PendingReplyStore.getPendingReply(channel.id)) + ).then(() => { + FluxDispatcher.dispatch({ type: "DELETE_PENDING_REPLY", channelId: channel.id }); + }); + + } + }; } export default definePlugin({ @@ -77,60 +93,8 @@ export default definePlugin({ description: "Share your current Spotify track, album or artist via slash command (/track, /album, /artist)", authors: [Devs.katlyn], commands: [ - { - name: "track", - description: "Send your current Spotify track to chat", - inputType: ApplicationCommandInputType.BUILT_IN, - options: [], - execute: (_, ctx) => { - const track: Track | null = Spotify.getTrack(); - if (track === null) { - sendBotMessage(ctx.channel.id, { - content: "You're not listening to any music." - }); - return; - } - // Note: Due to how Discord handles commands, we need to manually create and send the message - sendMessage(ctx.channel.id, { - content: `https://open.spotify.com/track/${track.id}` - }); - } - }, - { - name: "album", - description: "Send your current Spotify album to chat", - inputType: ApplicationCommandInputType.BUILT_IN, - options: [], - execute: (_, ctx) => { - const track: Track | null = Spotify.getTrack(); - if (track === null) { - sendBotMessage(ctx.channel.id, { - content: "You're not listening to any music." - }); - return; - } - sendMessage(ctx.channel.id, { - content: `https://open.spotify.com/album/${track.album.id}` - }); - } - }, - { - name: "artist", - description: "Send your current Spotify artist to chat", - inputType: ApplicationCommandInputType.BUILT_IN, - options: [], - execute: (_, ctx) => { - const track: Track | null = Spotify.getTrack(); - if (track === null) { - sendBotMessage(ctx.channel.id, { - content: "You're not listening to any music." - }); - return; - } - sendMessage(ctx.channel.id, { - content: track.artists[0].external_urls.spotify - }); - } - } + makeCommand("track", track => `https://open.spotify.com/track/${track.id}`), + makeCommand("album", track => `https://open.spotify.com/album/${track.album.id}`), + makeCommand("artist", track => track.artists[0].external_urls.spotify) ] });