From 554c3e2f3c8dc19a2df7bc8fe299f5f92cb2bd21 Mon Sep 17 00:00:00 2001 From: D3SOX Date: Wed, 15 May 2024 18:33:18 +0200 Subject: [PATCH] feat(serverProfilesToolbox): add support for profile effects, avatar decorations and clipboard --- src/plugins/serverProfilesToolbox/index.tsx | 194 +++++++++++++++----- 1 file changed, 145 insertions(+), 49 deletions(-) diff --git a/src/plugins/serverProfilesToolbox/index.tsx b/src/plugins/serverProfilesToolbox/index.tsx index e2f873fec..85b815b9d 100644 --- a/src/plugins/serverProfilesToolbox/index.tsx +++ b/src/plugins/serverProfilesToolbox/index.tsx @@ -7,24 +7,59 @@ import { Devs } from "@utils/constants"; import definePlugin from "@utils/types"; import { findByPropsLazy, findComponentByCodeLazy } from "@webpack"; -import { Button, GuildMemberStore, UserProfileStore, UserStore } from "@webpack/common"; +import { + Button, + Clipboard, + GuildMemberStore, + Text, + Toasts, + UserProfileStore, + UserStore +} from "@webpack/common"; +import { GuildMember } from "discord-types/general"; const SummaryItem = findComponentByCodeLazy("borderType", "showBorder", "hideDivider"); -let savedNick: string | null = null; -let savedPronouns: string | null = null; -let savedBio: string | undefined = undefined; -let savedThemeColors: number[] | undefined = undefined; -let savedBanner: string | undefined = undefined; -let savedAvatar: string | undefined = undefined; +interface SavedProfile { + nick: string | null; + pronouns: string | null; + bio: string | null; + themeColors: number[] | undefined; + banner: string | undefined; + avatar: string | undefined; + profileEffectId: string | undefined; + avatarDecoration: string | undefined; +} -const { setPendingAvatar, setPendingBanner, setPendingBio, setPendingNickname, setPendingPronouns, setPendingThemeColors }: { +const savedProfile: SavedProfile = { + nick: null, + pronouns: null, + bio: null, + themeColors: undefined, + banner: undefined, + avatar: undefined, + profileEffectId: undefined, + avatarDecoration: undefined, +}; + +const { + setPendingAvatar, + setPendingBanner, + setPendingBio, + setPendingNickname, + setPendingPronouns, + setPendingThemeColors, + setPendingProfileEffectId, + setPendingAvatarDecoration, +}: { setPendingAvatar: (a: string | undefined) => void; setPendingBanner: (a: string | undefined) => void; - setPendingBio: (a: string | undefined) => void; + setPendingBio: (a: string | null) => void; setPendingNickname: (a: string | null) => void; setPendingPronouns: (a: string | null) => void; setPendingThemeColors: (a: number[] | undefined) => void; + setPendingProfileEffectId: (a: string | undefined) => void; + setPendingAvatarDecoration: (a: string | undefined) => void; } = findByPropsLazy("setPendingNickname", "setPendingPronouns"); export default definePlugin({ @@ -36,47 +71,108 @@ export default definePlugin({ const currentUser = UserStore.getCurrentUser(); const premiumType = currentUser.premiumType ?? 0; + const copy = () => { + const profile = UserProfileStore.getGuildMemberProfile(currentUser.id, guildId); + const nick = GuildMemberStore.getNick(guildId, currentUser.id); + const selfMember = GuildMemberStore.getMember(guildId, currentUser.id) as GuildMember & { avatarDecoration: string | undefined }; + savedProfile.nick = nick ?? ""; + savedProfile.pronouns = profile.pronouns; + savedProfile.bio = profile.bio; + savedProfile.themeColors = profile.themeColors; + savedProfile.banner = profile.banner; + savedProfile.avatar = selfMember.avatar; + savedProfile.profileEffectId = profile.profileEffectId; + savedProfile.avatarDecoration = selfMember.avatarDecoration; + }; + + const paste = () => { + setPendingNickname(savedProfile.nick); + setPendingPronouns(savedProfile.pronouns); + if (premiumType === 2) { + setPendingBio(savedProfile.bio); + setPendingThemeColors(savedProfile.themeColors); + setPendingBanner(savedProfile.banner); + setPendingAvatar(savedProfile.avatar); + setPendingProfileEffectId(savedProfile.profileEffectId); + setPendingAvatarDecoration(savedProfile.avatarDecoration); + } + }; + + const reset = () => { + setPendingNickname(null); + setPendingPronouns(""); + if (premiumType === 2) { + setPendingBio(null); + setPendingThemeColors([]); + setPendingBanner(undefined); + setPendingAvatar(undefined); + setPendingProfileEffectId(undefined); + setPendingAvatarDecoration(undefined); + } + }; + + const copyToClipboard = () => { + copy(); + Clipboard.copy(JSON.stringify(savedProfile)); + }; + + const pasteFromClipboard = async () => { + try { + const clip = await navigator.clipboard.readText(); + if (!clip) { + Toasts.show({ + message: "Clipboard is empty", + type: Toasts.Type.FAILURE, + id: Toasts.genId(), + }); + return; + } + const clipboardProfile: SavedProfile = JSON.parse(clip); + + if (!("nick" in clipboardProfile)) { + Toasts.show({ + message: "Data is not in correct format", + type: Toasts.Type.FAILURE, + id: Toasts.genId(), + }); + return; + } + + Object.assign(savedProfile, JSON.parse(clip)); + paste(); + } catch (e) { + Toasts.show({ + message: `Failed to read clipboard data: ${e}`, + type: Toasts.Type.FAILURE, + id: Toasts.genId(), + }); + } + }; + return -
- - - +
+ + Use the following buttons to mange the currently selected server + +
+ + + +
+
+ + +
; },