/* * Vencord, a modification for Discord's desktop app * Copyright (c) 2022 Vendicated and contributors * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ import "./fixDiscordBadgePadding.css"; import { _getBadges, BadgePosition, BadgeUserArgs, ProfileBadge } from "@api/Badges"; import ErrorBoundary from "@components/ErrorBoundary"; import { openContributorModal } from "@components/PluginSettings/ContributorModal"; import { isEquicordDonor } from "@components/VencordSettings/VencordTab"; import { Devs } from "@utils/constants"; import { Logger } from "@utils/Logger"; import { isEquicordPluginDev, isPluginDev } from "@utils/misc"; import definePlugin from "@utils/types"; import { Toasts, UserStore } from "@webpack/common"; import { User } from "discord-types/general"; import { EquicordDonorModal, VencordDonorModal } from "./modals"; const CONTRIBUTOR_BADGE = "https://vencord.dev/assets/favicon.png"; const EQUICORD_CONTRIBUTOR_BADGE = "https://i.imgur.com/57ATLZu.png"; const EQUICORD_DONOR_BADGE = "https://cdn.nest.rip/uploads/78cb1e77-b7a6-4242-9089-e91f866159bf.png"; const ContributorBadge: ProfileBadge = { description: "Vencord Contributor", image: CONTRIBUTOR_BADGE, position: BadgePosition.START, shouldShow: ({ userId }) => isPluginDev(userId), onClick: (_, { userId }) => openContributorModal(UserStore.getUser(userId)) }; const EquicordContributorBadge: ProfileBadge = { description: "Equicord Contributor", image: EQUICORD_CONTRIBUTOR_BADGE, position: BadgePosition.START, shouldShow: ({ userId }) => isEquicordPluginDev(userId), onClick: (_, { userId }) => openContributorModal(UserStore.getUser(userId)) }; const EquicordDonorBadge: ProfileBadge = { description: "Equicord Donor", image: EQUICORD_DONOR_BADGE, position: BadgePosition.START, shouldShow: ({ userId }) => { const donorBadges = EquicordDonorBadges[userId]?.map(badge => badge.badge); const hasDonorBadge = donorBadges?.includes("https://cdn.nest.rip/uploads/78cb1e77-b7a6-4242-9089-e91f866159bf.png"); return isEquicordDonor(userId) && !hasDonorBadge; }, onClick: () => { return EquicordDonorModal(); } }; let DonorBadges = {} as Record>>; let EquicordDonorBadges = {} as Record>>; async function loadBadges(url: string, noCache = false) { const init = {} as RequestInit; if (noCache) init.cache = "no-cache"; return await fetch(url, init).then(r => r.json()); } async function loadAllBadges(noCache = false) { const vencordBadges = await loadBadges("https://badges.vencord.dev/badges.json", noCache); const equicordBadges = await loadBadges("https://equicord.org/badges", noCache); DonorBadges = vencordBadges; EquicordDonorBadges = equicordBadges; } let intervalId: any; export default definePlugin({ name: "BadgeAPI", description: "API to add badges to users.", authors: [Devs.Megu, Devs.Ven, Devs.TheSun], required: true, patches: [ { find: ".MODAL]:26", replacement: { match: /(?=;return 0===(\i)\.length\?)(?<=(\i)\.useMemo.+?)/, replace: ";$1=$2.useMemo(()=>[...$self.getBadges(arguments[0].displayProfile),...$1],[$1])" } }, { find: "#{intl::PROFILE_USER_BADGES}", replacement: [ { match: /(alt:" ","aria-hidden":!0,src:)(.+?)(?=,)(?<=href:(\i)\.link.+?)/, replace: (_, rest, originalSrc, badge) => `...${badge}.props,${rest}${badge}.image??(${originalSrc})` }, { match: /(?<="aria-label":(\i)\.description,.{0,200})children:/, replace: "children:$1.component?$self.renderBadgeComponent({...$1}) :" }, // conditionally override their onClick with badge.onClick if it exists { match: /href:(\i)\.link/, replace: "...($1.onClick&&{onClick:vcE=>$1.onClick(vcE,$1)}),$&" } ] } ], // for access from the console or other plugins get DonorBadges() { return DonorBadges; }, get EquicordDonorBadges() { return EquicordDonorBadges; }, toolboxActions: { async "Refetch Badges"() { await loadAllBadges(true); Toasts.show({ id: Toasts.genId(), message: "Successfully refetched badges!", type: Toasts.Type.SUCCESS }); } }, userProfileBadges: [ContributorBadge, EquicordContributorBadge, EquicordDonorBadge], async start() { await loadAllBadges(); clearInterval(intervalId); intervalId = setInterval(loadAllBadges, 1000 * 60 * 30); // 30 minutes }, async stop() { clearInterval(intervalId); }, getBadges(props: { userId: string; user?: User; guildId: string; }) { if (!props) return []; try { props.userId ??= props.user?.id!; return _getBadges(props); } catch (e) { new Logger("BadgeAPI#hasBadges").error(e); return []; } }, renderBadgeComponent: ErrorBoundary.wrap((badge: ProfileBadge & BadgeUserArgs) => { const Component = badge.component!; return ; }, { noop: true }), getDonorBadges(userId: string) { return DonorBadges[userId]?.map(badge => ({ image: badge.badge, description: badge.tooltip, position: BadgePosition.START, props: { style: { borderRadius: "50%", transform: "scale(0.9)" // The image is a bit too big compared to default badges } }, onClick() { return VencordDonorModal(); }, })); }, getEquicordDonorBadges(userId: string) { return EquicordDonorBadges[userId]?.map(badge => ({ image: badge.badge, description: badge.tooltip, position: BadgePosition.START, props: { style: { borderRadius: "50%", transform: "scale(0.9)" // The image is a bit too big compared to default badges } }, onClick() { return EquicordDonorModal(); }, })); } });