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";
|
2025-07-15 15:57:24 +02:00
|
|
|
import { openContributorModal } from "@components/settings/tabs";
|
2022-11-28 13:37:55 +01:00
|
|
|
import { Devs } from "@utils/constants";
|
2025-11-16 15:17:35 +01:00
|
|
|
import { copyWithToast } from "@utils/discord";
|
2024-06-08 05:26:03 +02:00
|
|
|
import { Logger } from "@utils/Logger";
|
2025-11-18 23:48:00 -05:00
|
|
|
import { shouldShowContributorBadge, shouldShowEquicordContributorBadge } from "@utils/misc";
|
2022-11-28 13:37:55 +01:00
|
|
|
import definePlugin from "@utils/types";
|
2025-07-10 00:38:39 +02:00
|
|
|
import { User } from "@vencord/discord-types";
|
2025-09-02 04:13:19 -04:00
|
|
|
import { ContextMenuApi, Menu, Toasts, UserStore } from "@webpack/common";
|
2022-11-11 22:50:09 +00:00
|
|
|
|
2025-11-09 11:48:57 -05:00
|
|
|
import Plugins, { PluginMeta } from "~plugins";
|
|
|
|
|
|
2025-05-14 14:46:57 -04:00
|
|
|
import { EquicordDonorModal, VencordDonorModal } from "./modals";
|
|
|
|
|
|
2025-06-06 18:30:19 +02:00
|
|
|
const CONTRIBUTOR_BADGE = "https://cdn.discordapp.com/emojis/1092089799109775453.png?size=64";
|
2025-10-02 18:56:23 -04:00
|
|
|
const EQUICORD_CONTRIBUTOR_BADGE = "https://equicord.org/assets/favicon.png";
|
2025-11-09 11:48:57 -05:00
|
|
|
const USERPLUGIN_CONTRIBUTOR_BADGE = "https://equicord.org/assets/icons/misc/userplugin.png";
|
2022-11-11 22:50:09 +00:00
|
|
|
|
|
|
|
|
const ContributorBadge: ProfileBadge = {
|
2024-04-18 19:53:52 -04:00
|
|
|
description: "Vencord Contributor",
|
2025-11-15 03:25:23 -08:00
|
|
|
iconSrc: CONTRIBUTOR_BADGE,
|
2022-11-11 22:50:09 +00:00
|
|
|
position: BadgePosition.START,
|
2025-05-28 15:24:48 +01:00
|
|
|
shouldShow: ({ userId }) => shouldShowContributorBadge(userId),
|
2025-11-11 11:22:01 -05:00
|
|
|
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",
|
2025-11-15 06:29:32 -05:00
|
|
|
iconSrc: EQUICORD_CONTRIBUTOR_BADGE,
|
2024-04-18 19:53:52 -04:00
|
|
|
position: BadgePosition.START,
|
2025-05-29 09:48:46 -04:00
|
|
|
shouldShow: ({ userId }) => shouldShowEquicordContributorBadge(userId),
|
2025-11-11 11:22:01 -05:00
|
|
|
onClick: (_, { userId }) => openContributorModal(UserStore.getUser(userId)),
|
2025-11-09 11:48:57 -05:00
|
|
|
props: {
|
|
|
|
|
style: {
|
|
|
|
|
borderRadius: "50%",
|
|
|
|
|
transform: "scale(0.9)"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const UserPluginContributorBadge: ProfileBadge = {
|
|
|
|
|
description: "User Plugin Contributor",
|
2025-11-15 06:29:32 -05:00
|
|
|
iconSrc: USERPLUGIN_CONTRIBUTOR_BADGE,
|
2025-11-09 11:48:57 -05:00
|
|
|
position: BadgePosition.START,
|
|
|
|
|
shouldShow: ({ userId }) => {
|
2025-11-18 23:48:00 -05:00
|
|
|
if (!IS_DEV) return false;
|
2025-11-09 11:48:57 -05:00
|
|
|
const allPlugins = Object.values(Plugins);
|
|
|
|
|
return allPlugins.some(p => {
|
|
|
|
|
const pluginMeta = PluginMeta[p.name];
|
2025-11-18 23:48:00 -05:00
|
|
|
return pluginMeta?.userPlugin && p.authors.some(a => a.id.toString() === userId);
|
2025-11-09 11:48:57 -05:00
|
|
|
});
|
|
|
|
|
},
|
2025-11-11 11:22:01 -05:00
|
|
|
onClick: (_, { userId }) => openContributorModal(UserStore.getUser(userId)),
|
2025-10-10 09:22:22 -04:00
|
|
|
props: {
|
|
|
|
|
style: {
|
|
|
|
|
borderRadius: "50%",
|
|
|
|
|
transform: "scale(0.9)"
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-04-18 19:53:52 -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-05-28 11:47:06 -04:00
|
|
|
const equicordBadges = await loadBadges("https://equicord.org/badges.json", 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
|
|
|
|
2025-11-27 02:36:33 -05:00
|
|
|
export function BadgeContextMenu({ badge }: { badge: ProfileBadge & BadgeUserArgs; }) {
|
2025-08-31 05:18:40 +02:00
|
|
|
return (
|
|
|
|
|
<Menu.Menu
|
|
|
|
|
navId="vc-badge-context"
|
|
|
|
|
onClose={ContextMenuApi.closeContextMenu}
|
|
|
|
|
aria-label="Badge Options"
|
|
|
|
|
>
|
|
|
|
|
{badge.description && (
|
|
|
|
|
<Menu.MenuItem
|
|
|
|
|
id="vc-badge-copy-name"
|
|
|
|
|
label="Copy Badge Name"
|
|
|
|
|
action={() => copyWithToast(badge.description!)}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2025-11-15 03:25:23 -08:00
|
|
|
{badge.iconSrc && (
|
2025-08-31 05:18:40 +02:00
|
|
|
<Menu.MenuItem
|
|
|
|
|
id="vc-badge-copy-link"
|
|
|
|
|
label="Copy Badge Image Link"
|
2025-11-15 03:25:23 -08:00
|
|
|
action={() => copyWithToast(badge.iconSrc!)}
|
2025-08-31 05:18:40 +02:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</Menu.Menu>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-11 22:50:09 +00:00
|
|
|
export default definePlugin({
|
|
|
|
|
name: "BadgeAPI",
|
2025-08-31 05:18:40 +02:00
|
|
|
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-11-15 03:25:23 -08:00
|
|
|
match: /alt:" ","aria-hidden":!0,src:.{0,50}(\i).iconSrc/,
|
|
|
|
|
replace: "...$1.props,$&"
|
2024-06-08 05:26:03 +02:00
|
|
|
},
|
|
|
|
|
{
|
2025-12-10 03:59:55 +01:00
|
|
|
match: /(?<="aria-label":(\i)\.description,.{0,200}?)children:/g,
|
2025-02-25 16:29:27 -03:00
|
|
|
replace: "children:$1.component?$self.renderBadgeComponent({...$1}) :"
|
2024-06-08 05:26:03 +02:00
|
|
|
},
|
2025-08-31 05:18:40 +02:00
|
|
|
// handle onClick and onContextMenu
|
2024-06-08 05:26:03 +02:00
|
|
|
{
|
|
|
|
|
match: /href:(\i)\.link/,
|
2025-08-31 05:18:40 +02:00
|
|
|
replace: "...$self.getBadgeMouseEventHandlers($1),$&"
|
2024-06-08 05:26:03 +02:00
|
|
|
}
|
|
|
|
|
]
|
2025-07-16 00:52:39 -04:00
|
|
|
},
|
|
|
|
|
{
|
2025-09-11 09:46:41 -04:00
|
|
|
find: "profileCardUsernameRow,children:",
|
2025-07-16 00:52:39 -04:00
|
|
|
replacement: {
|
2025-09-11 09:46:41 -04:00
|
|
|
match: /badges:(\i)(?<=displayProfile:(\i).+?)/,
|
|
|
|
|
replace: "badges:[...$self.getBadges($2),...$1]"
|
2025-07-16 00:52:39 -04: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-11-09 11:48:57 -05:00
|
|
|
userProfileBadges: [ContributorBadge, EquicordContributorBadge, UserPluginContributorBadge],
|
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
|
|
|
|
2025-08-31 05:18:40 +02:00
|
|
|
getBadgeMouseEventHandlers(badge: ProfileBadge & BadgeUserArgs) {
|
|
|
|
|
const handlers = {} as Record<string, (e: React.MouseEvent) => void>;
|
|
|
|
|
|
|
|
|
|
if (!badge) return handlers; // sanity check
|
|
|
|
|
|
|
|
|
|
const { onClick, onContextMenu } = badge;
|
|
|
|
|
|
|
|
|
|
if (onClick) handlers.onClick = e => onClick(e, badge);
|
|
|
|
|
if (onContextMenu) handlers.onContextMenu = e => onContextMenu(e, badge);
|
|
|
|
|
|
|
|
|
|
return handlers;
|
|
|
|
|
},
|
|
|
|
|
|
2023-04-29 02:06:52 +02:00
|
|
|
getDonorBadges(userId: string) {
|
|
|
|
|
return DonorBadges[userId]?.map(badge => ({
|
2025-11-15 03:25:23 -08:00
|
|
|
iconSrc: badge.badge,
|
2023-11-30 17:28:53 +01:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-08-31 05:18:40 +02:00
|
|
|
onContextMenu(event, badge) {
|
|
|
|
|
ContextMenuApi.openContextMenu(event, () => <BadgeContextMenu badge={badge} />);
|
|
|
|
|
},
|
2023-04-29 02:06:52 +02:00
|
|
|
onClick() {
|
2025-05-14 14:46:57 -04:00
|
|
|
return VencordDonorModal();
|
2023-04-29 02:06:52 +02:00
|
|
|
},
|
2025-09-02 04:13:19 -04:00
|
|
|
} satisfies ProfileBadge));
|
2024-04-18 19:15:01 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getEquicordDonorBadges(userId: string) {
|
|
|
|
|
return EquicordDonorBadges[userId]?.map(badge => ({
|
2025-11-15 06:29:32 -05:00
|
|
|
iconSrc: badge.badge,
|
2024-04-18 19:15:01 -04:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-09-02 04:13:19 -04:00
|
|
|
onContextMenu(event, badge) {
|
|
|
|
|
ContextMenuApi.openContextMenu(event, () => <BadgeContextMenu badge={badge} />);
|
|
|
|
|
},
|
2024-04-18 19:15:01 -04:00
|
|
|
onClick() {
|
2025-05-14 14:46:57 -04:00
|
|
|
return EquicordDonorModal();
|
2024-04-18 19:15:01 -04:00
|
|
|
},
|
2025-08-31 05:18:40 +02:00
|
|
|
} satisfies ProfileBadge));
|
2022-11-17 00:21:13 +01:00
|
|
|
}
|
2022-11-11 22:50:09 +00:00
|
|
|
});
|