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";
|
|
|
|
|
import { Flex } from "@components/Flex";
|
|
|
|
|
import { Heart } from "@components/Heart";
|
2025-07-15 15:57:24 +02:00
|
|
|
import DonateButton from "@components/settings/DonateButton";
|
|
|
|
|
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";
|
2023-03-01 20:35:08 +00:00
|
|
|
import { Margins } from "@utils/margins";
|
2025-11-16 15:17:35 +01:00
|
|
|
import { shouldShowContributorBadge } from "@utils/misc";
|
2025-01-29 01:04:36 -03:00
|
|
|
import { closeModal, ModalContent, ModalFooter, ModalHeader, ModalRoot, openModal } from "@utils/modal";
|
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-08-31 05:18:40 +02:00
|
|
|
import { ContextMenuApi, Forms, Menu, Toasts, UserStore } from "@webpack/common";
|
2022-11-11 22:50:09 +00:00
|
|
|
|
2025-06-06 18:30:19 +02:00
|
|
|
const CONTRIBUTOR_BADGE = "https://cdn.discordapp.com/emojis/1092089799109775453.png?size=64";
|
2022-11-11 22:50:09 +00:00
|
|
|
|
|
|
|
|
const ContributorBadge: ProfileBadge = {
|
2023-04-13 21:04:19 +02: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),
|
2024-06-08 05:26:03 +02:00
|
|
|
onClick: (_, { userId }) => openContributorModal(UserStore.getUser(userId))
|
2022-11-11 22:50:09 +00:00
|
|
|
};
|
|
|
|
|
|
2023-11-30 17:28:53 +01:00
|
|
|
let DonorBadges = {} as Record<string, Array<Record<"tooltip" | "badge", string>>>;
|
2022-11-17 00:21:13 +01:00
|
|
|
|
2023-05-02 02:55:38 +02:00
|
|
|
async function loadBadges(noCache = false) {
|
|
|
|
|
const init = {} as RequestInit;
|
|
|
|
|
if (noCache)
|
|
|
|
|
init.cache = "no-cache";
|
|
|
|
|
|
2023-11-30 17:28:53 +01:00
|
|
|
DonorBadges = await fetch("https://badges.vencord.dev/badges.json", init)
|
|
|
|
|
.then(r => r.json());
|
2023-05-02 02:55:38 +02:00
|
|
|
}
|
|
|
|
|
|
2025-05-13 22:39:56 +02:00
|
|
|
let intervalId: any;
|
|
|
|
|
|
2025-08-31 05:18:40 +02:00
|
|
|
function BadgeContextMenu({ badge }: { badge: ProfileBadge & BadgeUserArgs; }) {
|
|
|
|
|
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-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
|
|
|
},
|
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),$&"
|
2022-12-21 21:16:32 +02:00
|
|
|
}
|
|
|
|
|
]
|
2025-09-21 23:15:14 -04:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
find: "profileCardUsernameRow,children:",
|
|
|
|
|
replacement: {
|
|
|
|
|
match: /badges:(\i)(?<=displayProfile:(\i).+?)/,
|
|
|
|
|
replace: "badges:[...$self.getBadges($2),...$1]"
|
|
|
|
|
}
|
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;
|
|
|
|
|
},
|
|
|
|
|
|
2023-05-02 02:55:38 +02:00
|
|
|
toolboxActions: {
|
|
|
|
|
async "Refetch Badges"() {
|
|
|
|
|
await loadBadges(true);
|
|
|
|
|
Toasts.show({
|
|
|
|
|
id: Toasts.genId(),
|
|
|
|
|
message: "Successfully refetched badges!",
|
|
|
|
|
type: Toasts.Type.SUCCESS
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2025-01-23 02:48:44 +01:00
|
|
|
userProfileBadge: ContributorBadge,
|
|
|
|
|
|
2023-05-02 02:55:38 +02:00
|
|
|
async start() {
|
|
|
|
|
await loadBadges();
|
2025-05-13 22:39:56 +02:00
|
|
|
|
|
|
|
|
clearInterval(intervalId);
|
|
|
|
|
intervalId = setInterval(loadBadges, 1000 * 60 * 30); // 30 minutes
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
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() {
|
|
|
|
|
const modalKey = openModal(props => (
|
|
|
|
|
<ErrorBoundary noop onError={() => {
|
|
|
|
|
closeModal(modalKey);
|
2023-05-02 02:50:51 +02:00
|
|
|
VencordNative.native.openExternal("https://github.com/sponsors/Vendicated");
|
2023-04-29 02:06:52 +02:00
|
|
|
}}>
|
2025-01-29 01:04:36 -03:00
|
|
|
<ModalRoot {...props}>
|
|
|
|
|
<ModalHeader>
|
2025-10-11 01:42:14 +02:00
|
|
|
<Forms.FormTitle
|
|
|
|
|
tag="h2"
|
|
|
|
|
style={{
|
|
|
|
|
width: "100%",
|
|
|
|
|
textAlign: "center",
|
|
|
|
|
margin: 0
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Flex style={{ justifyContent: "center", alignItems: "center", gap: "0.5em" }}>
|
2023-04-29 02:06:52 +02:00
|
|
|
<Heart />
|
|
|
|
|
Vencord Donor
|
2025-10-11 01:42:14 +02:00
|
|
|
</Flex>
|
|
|
|
|
</Forms.FormTitle>
|
2025-01-29 01:04:36 -03:00
|
|
|
</ModalHeader>
|
|
|
|
|
<ModalContent>
|
2023-04-29 02:06:52 +02:00
|
|
|
<Flex>
|
|
|
|
|
<img
|
|
|
|
|
role="presentation"
|
|
|
|
|
src="https://cdn.discordapp.com/emojis/1026533070955872337.png"
|
|
|
|
|
alt=""
|
|
|
|
|
style={{ margin: "auto" }}
|
|
|
|
|
/>
|
|
|
|
|
<img
|
|
|
|
|
role="presentation"
|
|
|
|
|
src="https://cdn.discordapp.com/emojis/1026533090627174460.png"
|
|
|
|
|
alt=""
|
|
|
|
|
style={{ margin: "auto" }}
|
|
|
|
|
/>
|
|
|
|
|
</Flex>
|
|
|
|
|
<div style={{ padding: "1em" }}>
|
|
|
|
|
<Forms.FormText>
|
|
|
|
|
This Badge is a special perk for Vencord Donors
|
|
|
|
|
</Forms.FormText>
|
|
|
|
|
<Forms.FormText className={Margins.top20}>
|
|
|
|
|
Please consider supporting the development of Vencord by becoming a donor. It would mean a lot!!
|
|
|
|
|
</Forms.FormText>
|
|
|
|
|
</div>
|
2025-01-29 01:04:36 -03:00
|
|
|
</ModalContent>
|
|
|
|
|
<ModalFooter>
|
2023-04-29 02:06:52 +02:00
|
|
|
<Flex style={{ width: "100%", justifyContent: "center" }}>
|
|
|
|
|
<DonateButton />
|
|
|
|
|
</Flex>
|
2025-01-29 01:04:36 -03:00
|
|
|
</ModalFooter>
|
|
|
|
|
</ModalRoot>
|
2023-04-29 02:06:52 +02:00
|
|
|
</ErrorBoundary>
|
|
|
|
|
));
|
|
|
|
|
},
|
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
|
|
|
});
|