Plugin

SpotifyShareCommands

Share your current Spotify track, album or artist via slash command (/track, /album, /artist)

Media Commands
index.ts
Download

Source

src/plugins/spotifyShareCommands/index.ts
1/*
2 * FiveCord — a Discord client mod
3 * Copyright (c) 2025 FiveCord
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
7import { ApplicationCommandInputType, findOption, OptionalMessageOption, sendBotMessage } from "@api/Commands";
8import { Devs } from "@utils/constants";
9import { sendMessage } from "@utils/discord";
10import definePlugin from "@utils/types";
11import { Command } from "@vencord/discord-types";
12import { findByPropsLazy } from "@webpack";
13import { FluxDispatcher, MessageActions, PendingReplyStore } from "@webpack/common";
14
15interface Album {
16 id: string;
17 image: {
18 height: number;
19 width: number;
20 url: string;
21 };
22 name: string;
23}
24
25interface Artist {
26 external_urls: {
27 spotify: string;
28 };
29 href: string;
30 id: string;
31 name: string;
32 type: "artist" | string;
33 uri: string;
34}
35
36interface Track {
37 id: string | null;
38 album: Album;
39 artists: Artist[];
40 duration: number;
41 isLocal: boolean;
42 name: string;
43}
44
45const Spotify = findByPropsLazy("getPlayerState");
46
47function makeCommand(name: string, formatUrl: (track: Track) => string): Command {
48 return {
49 name,
50 description: `Share your current Spotify ${name} in chat`,
51 inputType: ApplicationCommandInputType.BUILT_IN,
52 options: [OptionalMessageOption],
53 execute(options, { channel }) {
54 const track: Track | null = Spotify.getTrack();
55 if (!track) {
56 return sendBotMessage(channel.id, {
57 content: "You're not listening to any music."
58 });
59 }
60
61 // local tracks have an id of null
62 if (track.id == null) {
63 return sendBotMessage(channel.id, {
64 content: "Failed to find the track on spotify."
65 });
66 }
67
68 const data = formatUrl(track);
69 const message = findOption(options, "message");
70
71 // Note: Due to how Discord handles commands, we need to manually create and send the message
72
73 sendMessage(
74 channel.id,
75 { content: message ? `${message} ${data}` : data },
76 false,
77 MessageActions.getSendMessageOptionsForReply(PendingReplyStore.getPendingReply(channel.id))
78 ).then(() => {
79 FluxDispatcher.dispatch({ type: "DELETE_PENDING_REPLY", channelId: channel.id });
80 });
81
82 }
83 };
84}
85
86export default definePlugin({
87 name: "SpotifyShareCommands",
88 description: "Share your current Spotify track, album or artist via slash command (/track, /album, /artist)",
89 tags: ["Media", "Commands"],
90 authors: [Devs.katlyn],
91 commands: [
92 makeCommand("track", track => `https:class="ts-cmt">//open.spotify.com/track/${track.id}`),
93 makeCommand("album", track => `https:class="ts-cmt">//open.spotify.com/album/${track.album.id}`),
94 makeCommand("artist", track => track.artists[0].external_urls.spotify)
95 ]
96});
97