SpotifyShareCommands: add message argument like inbuilt commands (#3320)

also cleans up the plugin's code

Co-authored-by: Vendicated <vendicated@riseup.net>
This commit is contained in:
Ethan 2025-04-04 23:19:03 +02:00 committed by GitHub
parent 7450ecd6f3
commit c8b54234fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -16,8 +16,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { ApplicationCommandInputType, sendBotMessage } from "@api/Commands"; import { ApplicationCommandInputType, Command, findOption, OptionalMessageOption, sendBotMessage } from "@api/Commands";
import { Devs } from "@utils/constants"; import { Devs } from "@utils/constants";
import { sendMessage } from "@utils/discord";
import definePlugin from "@utils/types"; import definePlugin from "@utils/types";
import { findByPropsLazy } from "@webpack"; import { findByPropsLazy } from "@webpack";
import { FluxDispatcher, MessageActions } from "@webpack/common"; import { FluxDispatcher, MessageActions } from "@webpack/common";
@ -55,21 +56,36 @@ interface Track {
const Spotify = findByPropsLazy("getPlayerState"); const Spotify = findByPropsLazy("getPlayerState");
const PendingReplyStore = findByPropsLazy("getPendingReply"); const PendingReplyStore = findByPropsLazy("getPendingReply");
function sendMessage(channelId, message) { function makeCommand(name: string, formatUrl: (track: Track) => string): Command {
message = { return {
// The following are required to prevent Discord from throwing an error name,
invalidEmojis: [], description: `Share your current Spotify ${name} in chat`,
tts: false, inputType: ApplicationCommandInputType.BUILT_IN,
validNonShortcutEmojis: [], options: [OptionalMessageOption],
...message execute(options, { channel }) {
}; const track: Track | null = Spotify.getTrack();
const reply = PendingReplyStore.getPendingReply(channelId); if (!track) {
MessageActions.sendMessage(channelId, message, void 0, MessageActions.getSendMessageOptionsForReply(reply)) return sendBotMessage(channel.id, {
.then(() => { content: "You're not listening to any music."
if (reply) { });
FluxDispatcher.dispatch({ type: "DELETE_PENDING_REPLY", channelId });
} }
});
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({ 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)", description: "Share your current Spotify track, album or artist via slash command (/track, /album, /artist)",
authors: [Devs.katlyn], authors: [Devs.katlyn],
commands: [ commands: [
{ makeCommand("track", track => `https://open.spotify.com/track/${track.id}`),
name: "track", makeCommand("album", track => `https://open.spotify.com/album/${track.album.id}`),
description: "Send your current Spotify track to chat", makeCommand("artist", track => track.artists[0].external_urls.spotify)
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
});
}
}
] ]
}); });