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 DonateButton from "@components/DonateButton";
|
|
|
|
|
import ErrorBoundary from "@components/ErrorBoundary";
|
|
|
|
|
import { Flex } from "@components/Flex";
|
|
|
|
|
import { Heart } from "@components/Heart";
|
2024-05-04 23:15:33 -03:00
|
|
|
import { openContributorModal } from "@components/PluginSettings/ContributorModal";
|
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";
|
2023-03-01 20:35:08 +00:00
|
|
|
import { Margins } from "@utils/margins";
|
2023-05-23 01:55:39 +02:00
|
|
|
import { isPluginDev } 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";
|
2024-06-08 05:26:03 +02:00
|
|
|
import { Forms, Toasts, UserStore } from "@webpack/common";
|
|
|
|
|
import { User } from "discord-types/general";
|
2022-11-11 22:50:09 +00:00
|
|
|
|
2023-11-30 17:28:53 +01:00
|
|
|
const CONTRIBUTOR_BADGE = "https://vencord.dev/assets/favicon.png";
|
2022-11-11 22:50:09 +00:00
|
|
|
|
|
|
|
|
const ContributorBadge: ProfileBadge = {
|
2023-04-13 21:04:19 +02: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
|
|
|
};
|
|
|
|
|
|
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) {
|
2023-05-02 03:11:40 +02:00
|
|
|
DonorBadges = {};
|
|
|
|
|
|
2023-05-02 02:55:38 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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
|
|
|
{
|
2024-07-17 02:34:05 +02:00
|
|
|
find: ".FULL_SIZE]: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)}),$&"
|
2022-12-21 21:16:32 +02:00
|
|
|
}
|
|
|
|
|
]
|
2022-11-11 22:50:09 +00:00
|
|
|
}
|
|
|
|
|
],
|
2022-11-17 00:21:13 +01:00
|
|
|
|
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();
|
|
|
|
|
},
|
|
|
|
|
|
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() {
|
|
|
|
|
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>
|
2023-04-29 02:06:52 +02:00
|
|
|
<Flex style={{ width: "100%", justifyContent: "center" }}>
|
|
|
|
|
<Forms.FormTitle
|
|
|
|
|
tag="h2"
|
|
|
|
|
style={{
|
|
|
|
|
width: "100%",
|
|
|
|
|
textAlign: "center",
|
|
|
|
|
margin: 0
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Heart />
|
|
|
|
|
Vencord Donor
|
|
|
|
|
</Forms.FormTitle>
|
|
|
|
|
</Flex>
|
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>
|
|
|
|
|
));
|
|
|
|
|
},
|
|
|
|
|
}));
|
2022-11-17 00:21:13 +01:00
|
|
|
}
|
2022-11-11 22:50:09 +00:00
|
|
|
});
|