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