2022-11-11 22:50:09 +00:00
|
|
|
/*
|
|
|
|
|
* 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 <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-11-29 22:46:28 +01:00
|
|
|
import "./fixDiscordBadgePadding.css";
|
|
|
|
|
|
2024-06-08 05:26:03 +02:00
|
|
|
import { _getBadges, BadgePosition, BadgeUserArgs, ProfileBadge } from "@api/Badges";
|
2022-11-28 13:37:55 +01:00
|
|
|
import ErrorBoundary from "@components/ErrorBoundary";
|
2024-05-04 23:15:33 -03:00
|
|
|
import { openContributorModal } from "@components/PluginSettings/ContributorModal";
|
2025-03-15 19:25:05 -04:00
|
|
|
import { isEquicordDonor } from "@components/VencordSettings/VencordTab";
|
2022-11-28 13:37:55 +01:00
|
|
|
import { Devs } from "@utils/constants";
|
2024-06-08 05:26:03 +02:00
|
|
|
import { Logger } from "@utils/Logger";
|
2024-07-29 16:09:35 -04:00
|
|
|
import { isEquicordPluginDev, isPluginDev } from "@utils/misc";
|
2022-11-28 13:37:55 +01:00
|
|
|
import definePlugin from "@utils/types";
|
2025-05-14 14:46:57 -04:00
|
|
|
import { Toasts, UserStore } from "@webpack/common";
|
2024-06-08 05:26:03 +02:00
|
|
|
import { User } from "discord-types/general";
|
2022-11-11 22:50:09 +00:00
|
|
|
|
2025-05-14 14:46:57 -04:00
|
|
|
import { EquicordDonorModal, VencordDonorModal } from "./modals";
|
|
|
|
|
|
2024-04-18 19:53:52 -04:00
|
|
|
const CONTRIBUTOR_BADGE = "https://vencord.dev/assets/favicon.png";
|
2024-07-23 04:56:53 -04:00
|
|
|
const EQUICORD_CONTRIBUTOR_BADGE = "https://i.imgur.com/57ATLZu.png";
|
2025-03-15 19:25:05 -04:00
|
|
|
const EQUICORD_DONOR_BADGE = "https://cdn.nest.rip/uploads/78cb1e77-b7a6-4242-9089-e91f866159bf.png";
|
2022-11-11 22:50:09 +00:00
|
|
|
|
|
|
|
|
const ContributorBadge: ProfileBadge = {
|
2024-04-18 19:53:52 -04:00
|
|
|
description: "Vencord Contributor",
|
2022-11-11 22:50:09 +00:00
|
|
|
image: CONTRIBUTOR_BADGE,
|
|
|
|
|
position: BadgePosition.START,
|
2024-06-08 05:26:03 +02:00
|
|
|
shouldShow: ({ userId }) => isPluginDev(userId),
|
|
|
|
|
onClick: (_, { userId }) => openContributorModal(UserStore.getUser(userId))
|
2022-11-11 22:50:09 +00:00
|
|
|
};
|
|
|
|
|
|
2024-04-18 19:53:52 -04:00
|
|
|
const EquicordContributorBadge: ProfileBadge = {
|
|
|
|
|
description: "Equicord Contributor",
|
|
|
|
|
image: EQUICORD_CONTRIBUTOR_BADGE,
|
|
|
|
|
position: BadgePosition.START,
|
2024-06-08 10:32:39 -04:00
|
|
|
shouldShow: ({ userId }) => isEquicordPluginDev(userId),
|
2024-06-10 17:00:32 -04:00
|
|
|
onClick: (_, { userId }) => openContributorModal(UserStore.getUser(userId))
|
2024-04-18 19:53:52 -04:00
|
|
|
};
|
|
|
|
|
|
2025-03-15 19:25:05 -04:00
|
|
|
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;
|
2025-05-14 14:46:57 -04:00
|
|
|
},
|
|
|
|
|
onClick: () => {
|
|
|
|
|
return EquicordDonorModal();
|
2025-03-15 19:25:05 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-11-30 17:28:53 +01:00
|
|
|
let DonorBadges = {} as Record<string, Array<Record<"tooltip" | "badge", string>>>;
|
2024-04-18 19:15:01 -04:00
|
|
|
let EquicordDonorBadges = {} as Record<string, Array<Record<"tooltip" | "badge", string>>>;
|
2022-11-17 00:21:13 +01:00
|
|
|
|
2024-04-18 19:15:01 -04:00
|
|
|
async function loadBadges(url: string, noCache = false) {
|
2023-05-02 02:55:38 +02:00
|
|
|
const init = {} as RequestInit;
|
2024-04-18 19:15:01 -04:00
|
|
|
if (noCache) init.cache = "no-cache";
|
2023-05-02 02:55:38 +02:00
|
|
|
|
2024-04-18 19:15:01 -04:00
|
|
|
return await fetch(url, init).then(r => r.json());
|
|
|
|
|
}
|
2024-04-17 14:29:47 -04:00
|
|
|
|
2024-04-18 19:15:01 -04:00
|
|
|
async function loadAllBadges(noCache = false) {
|
|
|
|
|
const vencordBadges = await loadBadges("https://badges.vencord.dev/badges.json", noCache);
|
2025-03-15 13:19:24 -04:00
|
|
|
const equicordBadges = await loadBadges("https://equicord.org/badges", noCache);
|
2024-04-17 14:29:47 -04:00
|
|
|
|
2024-04-18 19:15:01 -04:00
|
|
|
DonorBadges = vencordBadges;
|
|
|
|
|
EquicordDonorBadges = equicordBadges;
|
2023-05-02 02:55:38 +02:00
|
|
|
}
|
|
|
|
|
|
2025-05-13 22:39:56 +02:00
|
|
|
let intervalId: any;
|
2024-04-18 19:15:01 -04:00
|
|
|
|
2022-11-11 22:50:09 +00:00
|
|
|
export default definePlugin({
|
|
|
|
|
name: "BadgeAPI",
|
|
|
|
|
description: "API to add badges to users.",
|
2023-04-05 22:45:14 +02:00
|
|
|
authors: [Devs.Megu, Devs.Ven, Devs.TheSun],
|
2022-11-11 22:50:09 +00:00
|
|
|
required: true,
|
|
|
|
|
patches: [
|
2024-06-08 05:26:03 +02:00
|
|
|
{
|
2025-04-23 14:36:43 -04:00
|
|
|
find: ".MODAL]:26",
|
2024-06-08 05:26:03 +02:00
|
|
|
replacement: {
|
2025-03-04 16:15:42 -05:00
|
|
|
match: /(?=;return 0===(\i)\.length\?)(?<=(\i)\.useMemo.+?)/,
|
|
|
|
|
replace: ";$1=$2.useMemo(()=>[...$self.getBadges(arguments[0].displayProfile),...$1],[$1])"
|
2024-06-08 05:26:03 +02:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
2025-03-04 16:15:42 -05:00
|
|
|
find: "#{intl::PROFILE_USER_BADGES}",
|
2024-06-08 05:26:03 +02:00
|
|
|
replacement: [
|
|
|
|
|
{
|
2025-03-14 23:33:24 -03:00
|
|
|
match: /(alt:" ","aria-hidden":!0,src:)(.+?)(?=,)(?<=href:(\i)\.link.+?)/,
|
2025-02-25 16:29:27 -03:00
|
|
|
replace: (_, rest, originalSrc, badge) => `...${badge}.props,${rest}${badge}.image??(${originalSrc})`
|
2024-06-08 05:26:03 +02:00
|
|
|
},
|
|
|
|
|
{
|
2025-01-18 14:52:35 -05:00
|
|
|
match: /(?<="aria-label":(\i)\.description,.{0,200})children:/,
|
2025-02-25 16:29:27 -03:00
|
|
|
replace: "children:$1.component?$self.renderBadgeComponent({...$1}) :"
|
2024-06-08 05:26:03 +02:00
|
|
|
},
|
|
|
|
|
// conditionally override their onClick with badge.onClick if it exists
|
|
|
|
|
{
|
|
|
|
|
match: /href:(\i)\.link/,
|
2025-02-25 16:29:27 -03:00
|
|
|
replace: "...($1.onClick&&{onClick:vcE=>$1.onClick(vcE,$1)}),$&"
|
2024-06-08 05:26:03 +02:00
|
|
|
}
|
|
|
|
|
]
|
2022-11-11 22:50:09 +00:00
|
|
|
}
|
|
|
|
|
],
|
2022-11-17 00:21:13 +01:00
|
|
|
|
2025-05-13 22:39:56 +02:00
|
|
|
// for access from the console or other plugins
|
|
|
|
|
get DonorBadges() {
|
|
|
|
|
return DonorBadges;
|
|
|
|
|
},
|
|
|
|
|
|
2025-05-14 14:46:57 -04:00
|
|
|
get EquicordDonorBadges() {
|
|
|
|
|
return EquicordDonorBadges;
|
|
|
|
|
},
|
|
|
|
|
|
2023-05-02 02:55:38 +02:00
|
|
|
toolboxActions: {
|
|
|
|
|
async "Refetch Badges"() {
|
2024-04-18 19:15:01 -04:00
|
|
|
await loadAllBadges(true);
|
2023-05-02 02:55:38 +02:00
|
|
|
Toasts.show({
|
|
|
|
|
id: Toasts.genId(),
|
|
|
|
|
message: "Successfully refetched badges!",
|
|
|
|
|
type: Toasts.Type.SUCCESS
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2025-05-14 14:46:57 -04:00
|
|
|
userProfileBadges: [ContributorBadge, EquicordContributorBadge, EquicordDonorBadge],
|
2025-01-23 02:48:44 +01:00
|
|
|
|
2023-05-02 02:55:38 +02:00
|
|
|
async start() {
|
2024-04-18 19:15:01 -04:00
|
|
|
await loadAllBadges();
|
2025-05-13 22:39:56 +02:00
|
|
|
clearInterval(intervalId);
|
2025-05-14 14:46:57 -04:00
|
|
|
intervalId = setInterval(loadAllBadges, 1000 * 60 * 30); // 30 minutes
|
2025-05-13 22:39:56 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async stop() {
|
|
|
|
|
clearInterval(intervalId);
|
2023-05-02 02:55:38 +02:00
|
|
|
},
|
|
|
|
|
|
2024-06-08 05:26:03 +02:00
|
|
|
getBadges(props: { userId: string; user?: User; guildId: string; }) {
|
2024-06-26 18:09:21 +02:00
|
|
|
if (!props) return [];
|
|
|
|
|
|
2024-06-08 05:26:03 +02:00
|
|
|
try {
|
|
|
|
|
props.userId ??= props.user?.id!;
|
|
|
|
|
|
|
|
|
|
return _getBadges(props);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
new Logger("BadgeAPI#hasBadges").error(e);
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2023-04-05 22:45:14 +02:00
|
|
|
renderBadgeComponent: ErrorBoundary.wrap((badge: ProfileBadge & BadgeUserArgs) => {
|
|
|
|
|
const Component = badge.component!;
|
|
|
|
|
return <Component {...badge} />;
|
|
|
|
|
}, { noop: true }),
|
|
|
|
|
|
2022-11-17 00:21:13 +01:00
|
|
|
|
2023-04-29 02:06:52 +02:00
|
|
|
getDonorBadges(userId: string) {
|
|
|
|
|
return DonorBadges[userId]?.map(badge => ({
|
2023-11-30 17:28:53 +01:00
|
|
|
image: badge.badge,
|
|
|
|
|
description: badge.tooltip,
|
2023-04-29 02:06:52 +02:00
|
|
|
position: BadgePosition.START,
|
|
|
|
|
props: {
|
|
|
|
|
style: {
|
|
|
|
|
borderRadius: "50%",
|
|
|
|
|
transform: "scale(0.9)" // The image is a bit too big compared to default badges
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
onClick() {
|
2025-05-14 14:46:57 -04:00
|
|
|
return VencordDonorModal();
|
2023-04-29 02:06:52 +02:00
|
|
|
},
|
|
|
|
|
}));
|
2024-04-18 19:15:01 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
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() {
|
2025-05-14 14:46:57 -04:00
|
|
|
return EquicordDonorModal();
|
2024-04-18 19:15:01 -04:00
|
|
|
},
|
|
|
|
|
}));
|
2022-11-17 00:21:13 +01:00
|
|
|
}
|
2022-11-11 22:50:09 +00:00
|
|
|
});
|