From 139b873c27c14c9a9f4e08ea3c5a6c15870b0b26 Mon Sep 17 00:00:00 2001 From: Haruka Date: Wed, 18 Dec 2024 18:49:15 +0200 Subject: [PATCH] EmoteCloner: Fix a server/sticker deleted edge-case The modal will hang and load infinitely if the server or the sticker was deleted as the API returns "Unknown Sticker" instead of the sticker object for cases like this --- src/plugins/emoteCloner/index.tsx | 46 +++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/src/plugins/emoteCloner/index.tsx b/src/plugins/emoteCloner/index.tsx index 98bd46427..5e18a0c1c 100644 --- a/src/plugins/emoteCloner/index.tsx +++ b/src/plugins/emoteCloner/index.tsx @@ -63,16 +63,20 @@ async function fetchSticker(id: string) { const cached = StickersStore.getStickerById(id); if (cached) return cached; - const { body } = await RestAPI.get({ - url: Constants.Endpoints.STICKER(id) - }); + try { + const { body } = await RestAPI.get({ + url: Constants.Endpoints.STICKER(id) + }); - FluxDispatcher.dispatch({ - type: "STICKER_FETCH_SUCCESS", - sticker: body - }); + FluxDispatcher.dispatch({ + type: "STICKER_FETCH_SUCCESS", + sticker: body + }); - return body as Sticker; + return body as Sticker; + } catch (err) { + return undefined; + } } async function cloneSticker(guildId: string, sticker: Sticker) { @@ -336,7 +340,31 @@ const messageContextMenuPatch: NavContextMenuPatchCallback = (children, props) = const sticker = props.message.stickerItems.find(s => s.id === favoriteableId); if (sticker?.format_type === 3 /* LOTTIE */) return; - return buildMenuItem("Sticker", () => fetchSticker(favoriteableId)); + // Workaround for cases when it's not available + // (e.g when using MessageLinkEmkbeds) + if (sticker == undefined) { + return; + } + + return buildMenuItem("Sticker", async () => { + const fetchedSticker = await fetchSticker(favoriteableId); + + // Workaround for incase the sticker or the server it's from is deleted. + // Allows the sticker to still be cloned, albeit less accurately. + if (fetchedSticker == undefined) { + return { + "id": sticker?.id, + "name": sticker?.name, + "format_type": sticker?.format_type, + "tags": sticker?.name, + "type": "2", + "available": true, + "guild_id": 0 + }; + } else { + return fetchedSticker; + } + }); } })();