2024-08-08 14:05:30 -04:00
|
|
|
|
|
|
|
|
2024-08-08 09:37:51 -04:00
|
|
|
import { DataStore } from "@api/index";
|
2024-08-08 14:05:30 -04:00
|
|
|
import { definePluginSettings } from "@api/Settings";
|
2024-08-08 09:37:51 -04:00
|
|
|
import { Devs } from "@utils/constants";
|
2024-08-08 14:05:30 -04:00
|
|
|
import definePlugin, { OptionType } from "@utils/types";
|
2024-08-08 09:37:51 -04:00
|
|
|
import { cache } from "@webpack";
|
2024-08-08 14:05:30 -04:00
|
|
|
import { Constants, Forms, MessageStore, Parser, RestAPI, useEffect, UserStore, useState } from "@webpack/common";
|
2024-08-08 09:37:51 -04:00
|
|
|
import { Message } from "discord-types/general";
|
|
|
|
|
|
|
|
const DATA_STORE_KEY = "huskchart";
|
|
|
|
type Husk = {
|
|
|
|
userId: string;
|
|
|
|
channelId: string;
|
|
|
|
messageId: string;
|
|
|
|
};
|
2024-08-08 14:05:30 -04:00
|
|
|
type SortedHusk = {
|
|
|
|
id: string;
|
|
|
|
count: number;
|
|
|
|
};
|
2024-08-08 09:37:51 -04:00
|
|
|
const messageCache = new Map<string, {
|
|
|
|
message?: Message;
|
|
|
|
fetched: boolean;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
async function getMessage(channelId: string, messageId: string): Promise<Message | undefined> {
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
2024-08-08 14:05:30 -04:00
|
|
|
const UserData = () => {
|
|
|
|
const [data, setData] = useState([]);
|
2024-08-08 14:44:13 -04:00
|
|
|
const [collapsed, collapse] = useState(true);
|
2024-08-08 14:05:30 -04:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const fetchData = async () => {
|
|
|
|
const rawHusks: Husk[] = await DataStore.get(DATA_STORE_KEY) || [];
|
|
|
|
const unsortedHuskCountPerUser: SortedHusk[] = [];
|
|
|
|
for (const husk of rawHusks) {
|
|
|
|
let shouldAddInitialHusk = true;
|
|
|
|
for (const [i, hc] of unsortedHuskCountPerUser.entries()) {
|
|
|
|
const unsortedHusker: SortedHusk = hc;
|
|
|
|
if (unsortedHusker.id == husk.userId) {
|
|
|
|
unsortedHuskCountPerUser[i].count++;
|
|
|
|
shouldAddInitialHusk = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!shouldAddInitialHusk) continue;
|
|
|
|
unsortedHuskCountPerUser.push({ id: husk.userId, count: 1 });
|
|
|
|
}
|
|
|
|
const sortedHuskers = unsortedHuskCountPerUser.sort((a, b) => b.count - a.count);
|
|
|
|
// @ts-ignore EXPLODEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
|
|
|
|
setData(sortedHuskers);
|
|
|
|
};
|
|
|
|
fetchData();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2024-08-08 14:44:13 -04:00
|
|
|
<Forms.FormText style={{ fontSize: "1.07rem", fontWeight: "500" }}>User stats <a onClick={() => { collapsed ? collapse(false) : collapse(true); }}>[{collapsed ? "View all" : "Collapse"}]</a></Forms.FormText>
|
|
|
|
<div style={{ display: "grid", gridTemplateColumns: "auto auto" }}>
|
|
|
|
{
|
|
|
|
data && data.map(user => <>
|
|
|
|
{
|
|
|
|
collapsed && <>
|
|
|
|
{
|
|
|
|
data.indexOf(user) < 6 &&
|
|
|
|
<div style={{ marginTop: data.indexOf(user) < 2 ? "0" : "7px" }}>
|
|
|
|
{/* @ts-ignore */}
|
|
|
|
{Parser.parse(`<@${user.id}>`)} <Forms.FormText style={{ marginTop: "4px" }}>with {user.count} {user.count > 1 ? "husks" : "husk"}</Forms.FormText>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
{
|
|
|
|
!collapsed && <>
|
|
|
|
{
|
|
|
|
<div style={{ marginTop: data.indexOf(user) < 2 ? "0" : "7px" }}>
|
|
|
|
{/* @ts-ignore */}
|
|
|
|
{Parser.parse(`<@${user.id}>`)} <Forms.FormText style={{ marginTop: "4px" }}>with {user.count} {user.count > 1 ? "husks" : "husk"}</Forms.FormText>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
</>)
|
|
|
|
}
|
|
|
|
</div>
|
2024-08-08 14:05:30 -04:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2024-08-08 09:37:51 -04:00
|
|
|
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) {
|
2024-08-08 14:05:30 -04:00
|
|
|
try {
|
2024-08-08 14:44:13 -04:00
|
|
|
if (event.userId === "428188716641812481") return;
|
2024-08-08 14:05:30 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
// explode
|
|
|
|
}
|
2024-08-08 09:37:51 -04:00
|
|
|
}
|
2024-08-08 14:05:30 -04:00
|
|
|
},
|
|
|
|
settings: definePluginSettings({
|
|
|
|
buttons: {
|
|
|
|
type: OptionType.COMPONENT,
|
|
|
|
description: "User stats",
|
|
|
|
component: (aaa) => (
|
|
|
|
<>
|
2024-08-08 14:44:13 -04:00
|
|
|
|
2024-08-08 14:05:30 -04:00
|
|
|
<UserData />
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
2024-08-08 09:37:51 -04:00
|
|
|
});
|