From 37c31ca8c62d178cce4ada739f6b82b79116fdc5 Mon Sep 17 00:00:00 2001 From: Tharki-God Date: Wed, 19 Jun 2024 01:26:15 +0530 Subject: [PATCH] better ig --- src/Components/Instruction.tsx | 4 +- src/Components/USRBGButton.tsx | 1 + src/index.ts | 5 +- src/injections/BannerLoader.ts | 2 +- src/injections/ProfileHeader.tsx | 26 -------- src/injections/UserProfileContext.tsx | 28 ++++++++ src/injections/index.ts | 4 +- src/lib/consts.ts | 4 +- src/lib/requiredModules.ts | 96 ++++++++++++++++++++------- src/lib/utils.ts | 8 ++- src/types.ts | 23 +++++-- 11 files changed, 133 insertions(+), 68 deletions(-) delete mode 100644 src/injections/ProfileHeader.tsx create mode 100644 src/injections/UserProfileContext.tsx diff --git a/src/Components/Instruction.tsx b/src/Components/Instruction.tsx index 2cb5216..9ad9f61 100644 --- a/src/Components/Instruction.tsx +++ b/src/Components/Instruction.tsx @@ -44,12 +44,12 @@ export default () => ( FluxDispatcher.dispatch({ type: "LAYER_POP_ALL" }); Modules.TransitionUtil?.transitionTo(`/channels/449175561529589761/886287835018178560`); }}> - #background-request + #click-here-to-request - 🔹 Click UPLOAD button and UPLOAD your image from your device. + 🔹 Type /bg and select the command. Upload for banner in the file option.
• Only .GIF .PNG .JPG .JPEG will be accepted.
• Max-size: 10MB diff --git a/src/Components/USRBGButton.tsx b/src/Components/USRBGButton.tsx index 0a9eff7..8f1c64c 100644 --- a/src/Components/USRBGButton.tsx +++ b/src/Components/USRBGButton.tsx @@ -39,5 +39,6 @@ export default () => ( ...inviteInfo, }); }} + variant="banner" /> ); diff --git a/src/index.ts b/src/index.ts index 54e3f30..81e8c80 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,17 +3,16 @@ import { defaultSettings } from "./lib/consts"; import "./style.css"; export const PluginInjector = new Injector(); export const PluginLogger = Logger.plugin("ReUSRBG", "#b380ff"); -export const USRDB = new Map(); +export const USRDB = new Map(); export const SettingValues = await settings.init("dev.tharki.ReUSRBG", defaultSettings); import Settings from "./Components/Settings"; import Injections from "./injections/index"; import Utils from "./lib/utils"; -import Types from "./types"; export const start = (): void => { void Utils.loadUSRBD(); Settings.registerSettings(); - void Injections.applyInjections(); + void Injections.applyInjections().catch((err) => PluginLogger.error(err)); }; export const stop = (): void => { diff --git a/src/injections/BannerLoader.ts b/src/injections/BannerLoader.ts index d3fc99d..fbb969f 100644 --- a/src/injections/BannerLoader.ts +++ b/src/injections/BannerLoader.ts @@ -11,7 +11,7 @@ export default (): void => { SettingValues.get("nitroBanner", defaultSettings.nitroBanner)) ) return res; - const { img } = USRDB.get(displayProfile?.userId); + const img = USRDB.get(displayProfile?.userId); res.bannerSrc = img; return res; }); diff --git a/src/injections/ProfileHeader.tsx b/src/injections/ProfileHeader.tsx deleted file mode 100644 index bfbaa60..0000000 --- a/src/injections/ProfileHeader.tsx +++ /dev/null @@ -1,26 +0,0 @@ -import { util } from "replugged"; -import { PluginInjector, SettingValues, USRDB } from "../index"; -import { defaultSettings } from "../lib/consts"; -import Modules from "../lib/requiredModules"; -import USRBGButton from "../Components/USRBGButton"; -import Types from "../types"; -export default (): void => { - const { ProfileHeader } = Modules; - PluginInjector.after(ProfileHeader, "default", (_args, res: Types.ReactTree) => { - if (!res) return res; - if (!Array.isArray(res.props.children)) { - res.props.children = [res.props.children]; - } - const { user } = (util.findInReactTree(res, (u: Types.ReactTree) => Boolean(u?.user)) ?? - {}) as Types.ReactTree & { user: Types.User }; - if ( - (!user || - !USRDB.has(user.id) || - (res?.banner && SettingValues.get("nitroBanner", defaultSettings.nitroBanner))) && - !res.props.children.some((c) => c?.type?.toString?.()?.includes?.(".Messages.EDIT_PROFILE")) - ) - return res; - res.props.children.unshift(); - return res; - }); -}; diff --git a/src/injections/UserProfileContext.tsx b/src/injections/UserProfileContext.tsx new file mode 100644 index 0000000..8b8c374 --- /dev/null +++ b/src/injections/UserProfileContext.tsx @@ -0,0 +1,28 @@ +import { util } from "replugged"; +import { users as UltimateUserStore } from "replugged/common"; +import { PluginInjector, SettingValues, USRDB } from "../index"; +import { defaultSettings } from "../lib/consts"; +import Modules from "../lib/requiredModules"; +import USRBGButton from "../Components/USRBGButton"; +import Types from "../types"; +export default (): void => { + const { UserProfileContext } = Modules; + PluginInjector.before(UserProfileContext, "render", (args) => { + const [props] = args; + const profileHeader = props?.children?.find((c) => + /{profileType:\w+,children:\w+}=/.exec(c?.type?.toString()), + ); + if ( + !profileHeader || + !props.user || + !USRDB.has(props.user.id) || + (props.user?.banner && SettingValues.get("nitroBanner", defaultSettings.nitroBanner)) + ) + return args; + if (!Array.isArray(profileHeader.props.children)) { + profileHeader.props.children = [profileHeader.props.children]; + } + profileHeader.props.children.unshift(); + return args; + }); +}; diff --git a/src/injections/index.ts b/src/injections/index.ts index cd0e1be..dbd7df6 100644 --- a/src/injections/index.ts +++ b/src/injections/index.ts @@ -2,13 +2,13 @@ import Modules from "../lib/requiredModules"; import injectBannerLoader from "./BannerLoader"; import injectBanners from "./Banners"; import injectDisplayProfileUtils from "./DisplayProfileUtils"; -import injectProfileHeader from "./ProfileHeader"; +import injectUserProfileContext from "./UserProfileContext"; export const applyInjections = async (): Promise => { await Modules.loadModules(); injectBannerLoader(); injectBanners(); injectDisplayProfileUtils(); - injectProfileHeader(); + injectUserProfileContext(); }; export default { applyInjections }; diff --git a/src/lib/consts.ts b/src/lib/consts.ts index 2f776e5..30b2e16 100644 --- a/src/lib/consts.ts +++ b/src/lib/consts.ts @@ -1,7 +1,7 @@ export const USBBG_SERVER_INVITE_CODE = "TeRQEPb"; export const USBBG_SERVER_ID = "449175561529589761"; -export const USRBG_URL = - "https://raw.githubusercontent.com/Discord-Custom-Covers/usrbg/master/dist/usrbg.json"; +// got link from https://github.com/Rico040/bunny-plugins/blob/master/plugins/userbg/src/index.tsx#L23C40-L23C76 +export const USRBG_URL = "https://usrbg.is-hardly.online/users"; export const defaultSettings = { nitroBanner: true, settingsBanner: true, diff --git a/src/lib/requiredModules.ts b/src/lib/requiredModules.ts index 8d01da5..42ea405 100644 --- a/src/lib/requiredModules.ts +++ b/src/lib/requiredModules.ts @@ -4,39 +4,87 @@ import Types from "../types"; export const Modules: Types.Modules = {}; Modules.loadModules = async (): Promise => { - Modules.IconClasses ??= await webpack.waitForProps("iconItem"); - Modules.InviteActions ??= await webpack.waitForProps( - "acceptInviteAndTransitionToInviteChannel", - ); - Modules.DisplayProfileUtils ??= await webpack.waitForProps( - "getDisplayProfile", - ); + Modules.IconClasses ??= await webpack + .waitForProps(["iconItem"], { + timeout: 10000, + }) + .catch(() => { + throw new Error("Failed To Find IconClasses Module"); + }); + + Modules.InviteActions ??= await webpack + .waitForProps(["acceptInviteAndTransitionToInviteChannel"], { + timeout: 10000, + }) + .catch(() => { + throw new Error("Failed To Find InviteActions Module"); + }); + + Modules.DisplayProfileUtils ??= await webpack + .waitForProps(["getDisplayProfile"], { timeout: 10000 }) + .catch(() => { + throw new Error("Failed To Find DisplayProfileUtils Module"); + }); + Modules.UserBannerParent ??= await webpack .waitForModule(webpack.filters.bySource("darkenOnHover:"), { raw: true, + timeout: 10000, }) - .then(({ exports }) => exports); + .then(({ exports }) => exports) + .catch(() => { + throw new Error("Failed To Find UserBannerParent Module"); + }); + Modules.UserBannerConstructor ??= await webpack .waitForModule(webpack.filters.bySource(".bannerSVGWrapper"), { raw: true, + timeout: 10000, }) - .then(({ exports }) => exports); - Modules.TransitionUtil ??= await webpack.waitForProps( - "transitionTo", - "transitionToGuild", - ); - Modules.Invite ??= await webpack.waitForModule( - webpack.filters.bySource(".AnalyticsPages.INVITE_EMBED"), - ); - Modules.BannerLoader ??= await webpack.waitForProps("BannerLoadingStatus"); - Modules.HeaderButton ??= await webpack.waitForModule( - webpack.filters.bySource(".TooltipContainer,{tooltipClassName"), - ); - Modules.ProfileHeader ??= await webpack - .waitForModule(webpack.filters.bySource(".biteSize]"), { - raw: true, + .then(({ exports }) => exports) + .catch(() => { + throw new Error("Failed To Find UserBannerConstructor Module"); + }); + + Modules.TransitionUtil ??= await webpack + .waitForProps(["transitionTo", "transitionToGuild"], { timeout: 10000 }) + .catch(() => { + throw new Error("Failed To Find TransitionUtil Module"); + }); + + Modules.Invite ??= await webpack + .waitForModule( + webpack.filters.bySource(".AnalyticsPages.INVITE_EMBED"), + { timeout: 10000 }, + ) + .catch(() => { + throw new Error("Failed To Find Invite Module"); + }); + + Modules.BannerLoader ??= await webpack + .waitForProps(["BannerLoadingStatus"], { + timeout: 10000, }) - .then(({ exports }) => exports); + .catch(() => { + throw new Error("Failed To Find BannerLoader Module"); + }); + + Modules.HeaderButton ??= await webpack + .waitForModule(webpack.filters.bySource('.banner]:"banner"==='), { + timeout: 10000, + }) + .catch(() => { + throw new Error("Failed To Find HeaderButton Module"); + }); + + Modules.UserProfileContext ??= await webpack + .waitForModule( + webpack.filters.bySource(".UserProfileThemeContextProvider"), + { timeout: 10000 }, + ) + .catch(() => { + throw new Error("Failed To Find UserProfileContext Module"); + }); }; export default Modules; diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 1df972d..11f77ff 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -4,7 +4,13 @@ export const loadUSRBD = async (reload?: boolean): Promise => { const fetchStart = performance.now(); const USRBG_RESPONSE = await fetch(USRBG_URL); const USRBG_JSON = await USRBG_RESPONSE.json(); - for (const USRBG_ITEM of USRBG_JSON) USRDB.set(USRBG_ITEM.uid, USRBG_ITEM); + for (const [USRBG_USERID, USRBG_ETAG] of Object.entries(USRBG_JSON.users)) + USRDB.set( + USRBG_USERID, + `${USRBG_JSON.endpoint}/${USRBG_JSON.bucket}/${USRBG_JSON.prefix}${USRBG_USERID}?${ + USRBG_ETAG as string + }`, + ); const fetchEnd = performance.now(); PluginLogger.log( `${reload ? "Reloaded" : "Loaded"} USRBG Database in ${(fetchEnd - fetchStart).toFixed(2)}ms.`, diff --git a/src/types.ts b/src/types.ts index d9a308b..f03a6c4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -90,12 +90,6 @@ export namespace Types { showPremiumBadgeUpsell: boolean; user: User; } - - export interface USRBD_USER { - img: string; - uid: string; - _id: string; - } export type InviteComponent = React.ComponentType<{ code: string; author: User }>; export interface Invite { code: string; @@ -191,7 +185,22 @@ export namespace Types { icon?: () => React.ReactNode; text?: string; onClick?: () => void; + variant?: "banner" | "icon" | "text"; }>; + export interface UserProfileContext { + $$typeof: symbol; + Overlay: DefaultTypes.AnyFunction; + render: ( + props: { + children: React.ReactElement[]; + className: string; + displayProfile: DisplayProfile; + profileType: string; + user: User; + }, + ...args: unknown[] + ) => React.ReactElement; + } export interface Modules { loadModules?: () => Promise; IconClasses?: IconClasses; @@ -203,7 +212,7 @@ export namespace Types { Invite?: InviteComponent; BannerLoader?: BannerLoader; HeaderButton?: HeaderButton; - ProfileHeader?: GenericModule; + UserProfileContext?: UserProfileContext; } export interface Settings { nitroBanner: boolean;