From e7b79056e945f47bbc8497df97e14bc857baac4e Mon Sep 17 00:00:00 2001 From: nin0dev Date: Thu, 8 Aug 2024 09:37:51 -0400 Subject: [PATCH] add plugin (i forgot committing) --- index.tsx | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 index.tsx diff --git a/index.tsx b/index.tsx new file mode 100644 index 0000000..9acde93 --- /dev/null +++ b/index.tsx @@ -0,0 +1,66 @@ +import { DataStore } from "@api/index"; +import { Devs } from "@utils/constants"; +import definePlugin from "@utils/types"; +import { cache } from "@webpack"; +import { Constants, MessageStore, RestAPI, UserStore } from "@webpack/common"; +import { Message } from "discord-types/general"; + +const DATA_STORE_KEY = "huskchart"; +type Husk = { + userId: string; + channelId: string; + messageId: string; +}; +const messageCache = new Map(); + +async function getMessage(channelId: string, messageId: string): Promise { + const cached = messageCache.get(messageId); + if (cached) return cached?.message; + + const storeMessage = MessageStore.getMessage(channelId, messageId); + if (storeMessage) { + messageCache.set(storeMessage.id, { + message: storeMessage, + fetched: false + }); + return storeMessage; + } + + const apiMessage = await RestAPI.get({ + url: Constants.Endpoints.MESSAGES(channelId), + query: { + limit: 1, + around: messageId + }, + retries: 2 + }).catch(() => null); + if (apiMessage) { + messageCache.set(apiMessage.body[0].id, { + message: apiMessage, + fetched: true + }); + return apiMessage.body[0]; + } +} +export default definePlugin({ + name: "HuskChart", + description: "See how much you've been husked, and by who", + authors: [Devs.nin0dev], + flux: { + async MESSAGE_REACTION_ADD(event) { + const msg = await getMessage(event.channelId, event.messageId); + if (msg!.author.id !== UserStore.getCurrentUser().id) return; + if (!event.emoji.name.includes("husk")) return; + let husks: Husk[] = await DataStore.get(DATA_STORE_KEY) || []; + husks.push({ + userId: event.userId, + channelId: event.channelId, + messageId: event.messageId + }); + DataStore.set(DATA_STORE_KEY, husks); + } + } +});