Plugin

CopyStickerLinks

Adds the ability to copy & open Sticker links

Emotes Utility
index.tsx
Download

Source

src/plugins/copyStickerLinks/index.tsx
1import { findGroupChildrenByChildId, NavContextMenuPatchCallback } from "@api/ContextMenu";
2import { isPluginEnabled } from "@api/PluginManager";
3import ExpressionClonerPlugin from "@plugins/expressionCloner";
4import { Devs } from "@utils/constants";
5import { copyWithToast } from "@utils/discord";
6import definePlugin from "@utils/types";
7import { Message, Sticker } from "@vencord/discord-types";
8import { Menu, React, StickersStore } from "@webpack/common";
9
10const StickerExt = [, "png", "png", "json", "gif"] as const;
11
12type PartialSticker = Pick<Sticker, "id" | "format_type">;
13
14function getUrl(data: PartialSticker): string {
15 if (data.format_type === 4)
16 return `https:${window.GLOBAL_ENV.MEDIA_PROXY_ENDPOINT}/stickers/${data.id}.gif?size=512&lossless=true`;
17
18 return `https:class="ts-cmt">//${window.GLOBAL_ENV.CDN_HOST}/stickers/${data.id}.${StickerExt[data.format_type]}?size=512&lossless=true`;
19}
20
21function buildMenuItem(sticker: PartialSticker, addBottomSeparator: boolean) {
22 return (
23 <>
24 <Menu.MenuGroup>
25 <Menu.MenuItem
26 id="vc-copy-sticker-link"
27 key="vc-copy-sticker-link"
28 label="Copy Link"
29 action={() => copyWithToast(getUrl(sticker), "Link copied!")}
30 />
31
32 <Menu.MenuItem
33 id="vc-open-sticker-link"
34 key="vc-open-sticker-link"
35 label="Open Link"
36 action={() => VencordNative.native.openExternal(getUrl(sticker))}
37 />
38 </Menu.MenuGroup>
39 {addBottomSeparator && <Menu.MenuSeparator />}
40 </>
41 );
42}
43
44const messageContextMenuPatch: NavContextMenuPatchCallback = (
45 children,
46 { favoriteableId, favoriteableType, message }: { favoriteableId: string; favoriteableType: string; message: Message; }
47) => {
48 if (!favoriteableId || favoriteableType !== "sticker") return;
49
50 const sticker = message.stickerItems.find(s => s.id === favoriteableId);
51 if (!sticker?.format_type) return;
52
53 const idx = children.findIndex(c => Array.isArray(c) && findGroupChildrenByChildId("vc-copy-sticker-url", c) != null);
54
55 children.splice(idx, 0, buildMenuItem(sticker, idx !== -1));
56};
57
58const expressionPickerPatch: NavContextMenuPatchCallback = (children, props: { target: HTMLElement; }) => {
59 const id = props?.target?.dataset?.id;
60 if (!id) return;
61 if (props.target.className?.includes("lottieCanvas")) return;
62
63 const sticker = StickersStore.getStickerById(id);
64 if (sticker) {
65 children.push(buildMenuItem(sticker, isPluginEnabled(ExpressionClonerPlugin.name)));
66 }
67};
68
69export default definePlugin({
70 name: "CopyStickerLinks",
71 description: "Adds the ability to copy & open Sticker links",
72 tags: ["Emotes", "Utility"],
73 authors: [Devs.Ven, Devs.Byeoon],
74 contextMenus: {
75 "message": messageContextMenuPatch,
76 "expression-picker": expressionPickerPatch
77 }
78});
79