diff options
author | Katlyn Lorimer <17418611+katlyn@users.noreply.github.com> | 2022-10-19 14:53:09 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-20 00:53:09 +0200 |
commit | 7eba5b99b0d1012c33fbca1a32dc2c5a3a800eb1 (patch) | |
tree | 8d2fa4d3fc52afc7e966d529ebee8b584943e6d0 | |
parent | f81ab5ef93459baf0a45eec1264412144dc4a18c (diff) | |
download | Vencord-7eba5b99b0d1012c33fbca1a32dc2c5a3a800eb1.tar.gz Vencord-7eba5b99b0d1012c33fbca1a32dc2c5a3a800eb1.tar.bz2 Vencord-7eba5b99b0d1012c33fbca1a32dc2c5a3a800eb1.zip |
feat: add sendify plugin (#122)
* feat: add sendify plugin
* fix: tweak plugin description
* fix: respect message replies
This implements code provided by Ven, while taking into account possible
errors from Discord as well as minor changes for readability.
Co-authored-by: Ven <vendicated@riseup.net>
-rw-r--r-- | src/plugins/sendify.ts | 123 | ||||
-rw-r--r-- | src/utils/constants.ts | 5 |
2 files changed, 127 insertions, 1 deletions
diff --git a/src/plugins/sendify.ts b/src/plugins/sendify.ts new file mode 100644 index 0000000..21d63b4 --- /dev/null +++ b/src/plugins/sendify.ts @@ -0,0 +1,123 @@ +import definePlugin from "../utils/types"; +import { lazyWebpack } from "../utils"; +import { filters } from "../webpack"; +import { ApplicationCommandInputType, sendBotMessage } from "../api/Commands"; +import { Devs } from "../utils/constants"; +import { PartialDeep } from "type-fest"; +import { Message } from "discord-types/general"; +import { FluxDispatcher } from "../webpack/common"; + +interface Album { + id: string + image: { + height: number + width: number + url: string + } + name: string +} + +interface Artist { + external_urls: { + spotify: string + } + href: string + id: string + name: string + type: "artist" | string + uri: string +} + +interface Track { + id: string + album: Album + artists: Artist[] + duration: number + isLocal: boolean + name: string +} + +const Spotify = lazyWebpack(filters.byProps(["getPlayerState"])); +const MessageCreator = lazyWebpack(filters.byProps(["getSendMessageOptionsForReply", "sendMessage"])); +const PendingReplyStore = lazyWebpack(filters.byProps(["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); + MessageCreator.sendMessage(channelId, message, void 0, MessageCreator.getSendMessageOptionsForReply(reply)) + .then(() => { + if (reply) { + FluxDispatcher.dispatch({ type: "DELETE_PENDING_REPLY", channelId }); + } + }); +} + +export default definePlugin({ + name: "Sendify", + description: "Send your current Spotify music to chat", + authors: [Devs.katlyn], + dependencies: ["CommandsAPI"], + 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 + }); + } + } + ] +}); diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 987cc4f..63bc01e 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -51,5 +51,8 @@ export const Devs = Object.freeze({ name: "ECHO", id: 712639419785412668n }, - + katlyn: { + name: "katlyn", + id: 250322741406859265n + } }); |