Plugin

CopyEmojiAsString

Add's button to copy emoji as formatted string!

index.tsx
Download

Source

src/plugins/copyEmojiAsFormattedString/index.tsx
1/*
2 * FiveCord — a Discord client mod
3 * Copyright (c) 2025 FiveCord
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
7import { Devs } from "@utils/constants";
8import definePlugin from "@utils/types";
9import { Clipboard, Menu, showToast, Toasts } from "@webpack/common";
10
11interface Emoji {
12 type: string;
13 id: string;
14 name: string;
15}
16
17interface Target {
18 dataset: Emoji;
19 firstChild: HTMLImageElement;
20}
21
22function removeCountingPostfix(name: string): string {
23 return name.replace(/~\d+$/, "");
24}
25
26function getEmojiFormattedString(target: Target): string {
27 const { dataset } = target;
28
29 if (!dataset.id) {
30 const fiberKey = Object.keys(target).find(key =>
31 /^__reactFiber\$\S+$/gm.test(key)
32 );
33
34 if (!fiberKey) return `:${dataset.name}:`;
35
36 const emojiUnicode =
37 target[fiberKey]?.child?.memoizedProps?.emoji?.surrogates;
38
39 return emojiUnicode || `:${dataset.name}:`;
40 }
41
42 const extension = target?.firstChild.src.match(
43 /https:\/\/cdn\.discordapp\.com\/emojis\/\d+\.(\w+)/
44 )?.[1];
45
46 const emojiName = removeCountingPostfix(dataset.name);
47 const emojiId = dataset.id;
48
49 return extension === "gif"
50 ? `<a:${emojiName}:${emojiId}>`
51 : `<:${emojiName}:${emojiId}>`;
52}
53
54export default definePlugin({
55 name: "CopyEmojiAsString",
56 description: "Add&#039;s button to copy emoji as formatted string!",
57 authors: [Devs.FiveCord],
58 contextMenus: {
59 "expression-picker"(children, { target }: { target: Target; }) {
60 if (target.dataset.type !== "emoji") return;
61
62 children.push(
63 <Menu.MenuItem
64 id="copy-formatted-string"
65 key="copy-formatted-string"
66 label={"Copy as formatted string"}
67 action={() => {
68 Clipboard.copy(getEmojiFormattedString(target));
69 showToast(
70 "Success! Copied to clipboard as formatted string.",
71 Toasts.Type.SUCCESS
72 );
73 }}
74 />
75 );
76 },
77 },
78});
79