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/>.
|
|
|
|
*/
|
|
|
|
|
2023-04-05 20:45:14 +00:00
|
|
|
import { BadgePosition, BadgeUserArgs, ProfileBadge } from "@api/Badges";
|
2022-11-28 12:37:55 +00:00
|
|
|
import DonateButton from "@components/DonateButton";
|
|
|
|
import ErrorBoundary from "@components/ErrorBoundary";
|
|
|
|
import { Flex } from "@components/Flex";
|
|
|
|
import { Heart } from "@components/Heart";
|
|
|
|
import { Devs } from "@utils/constants";
|
2023-05-05 23:36:00 +00:00
|
|
|
import { Logger } from "@utils/Logger";
|
2023-03-01 20:35:08 +00:00
|
|
|
import { Margins } from "@utils/margins";
|
2023-05-27 12:53:15 +00:00
|
|
|
import { isPluginDev } from "@utils/misc";
|
2022-11-28 12:37:55 +00:00
|
|
|
import { closeModal, Modals, openModal } from "@utils/modal";
|
|
|
|
import definePlugin from "@utils/types";
|
2023-05-02 00:55:38 +00:00
|
|
|
import { Forms, Toasts } from "@webpack/common";
|
2022-11-11 22:50:09 +00:00
|
|
|
|
2023-04-02 14:16:15 +00:00
|
|
|
const CONTRIBUTOR_BADGE = "https://cdn.discordapp.com/attachments/1033680203433660458/1092089947126780035/favicon.png";
|
2022-11-11 22:50:09 +00:00
|
|
|
|
|
|
|
const ContributorBadge: ProfileBadge = {
|
2023-04-13 19:04:19 +00:00
|
|
|
description: "Vencord Contributor",
|
2022-11-11 22:50:09 +00:00
|
|
|
image: CONTRIBUTOR_BADGE,
|
|
|
|
position: BadgePosition.START,
|
|
|
|
props: {
|
|
|
|
style: {
|
|
|
|
borderRadius: "50%",
|
|
|
|
transform: "scale(0.9)" // The image is a bit too big compared to default badges
|
|
|
|
}
|
|
|
|
},
|
2023-05-27 12:53:15 +00:00
|
|
|
shouldShow: ({ user }) => isPluginDev(user.id),
|
2023-04-13 19:04:19 +00:00
|
|
|
link: "https://github.com/Vendicated/Vencord"
|
2022-11-11 22:50:09 +00:00
|
|
|
};
|
|
|
|
|
2023-05-02 01:11:40 +00:00
|
|
|
let DonorBadges = {} as Record<string, Pick<ProfileBadge, "image" | "description">[]>;
|
2022-11-16 23:21:13 +00:00
|
|
|
|
2023-05-02 00:55:38 +00:00
|
|
|
async function loadBadges(noCache = false) {
|
2023-05-02 01:11:40 +00:00
|
|
|
DonorBadges = {};
|
|
|
|
|
2023-05-02 00:55:38 +00:00
|
|
|
const init = {} as RequestInit;
|
|
|
|
if (noCache)
|
|
|
|
init.cache = "no-cache";
|
|
|
|
|
|
|
|
const badges = await fetch("https://gist.githubusercontent.com/Vendicated/51a3dd775f6920429ec6e9b735ca7f01/raw/badges.csv", init)
|
|
|
|
.then(r => r.text());
|
|
|
|
|
|
|
|
const lines = badges.trim().split("\n");
|
|
|
|
if (lines.shift() !== "id,tooltip,image") {
|
|
|
|
new Logger("BadgeAPI").error("Invalid badges.csv file!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const line of lines) {
|
|
|
|
const [id, description, image] = line.split(",");
|
|
|
|
(DonorBadges[id] ??= []).push({ image, description });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-11 22:50:09 +00:00
|
|
|
export default definePlugin({
|
|
|
|
name: "BadgeAPI",
|
|
|
|
description: "API to add badges to users.",
|
2023-04-05 20:45:14 +00:00
|
|
|
authors: [Devs.Megu, Devs.Ven, Devs.TheSun],
|
2022-11-11 22:50:09 +00:00
|
|
|
required: true,
|
|
|
|
patches: [
|
|
|
|
/* Patch the badge list component on user profiles */
|
|
|
|
{
|
|
|
|
find: "Messages.PROFILE_USER_BADGES,role:",
|
2022-12-21 19:16:32 +00:00
|
|
|
replacement: [
|
2023-04-18 00:47:48 +00:00
|
|
|
{
|
2023-05-27 12:53:15 +00:00
|
|
|
match: /(?<=(\i)\.isTryItOutFlow,)(.{0,300})null==\i\?void 0:(\i)\.getBadges\(\)/,
|
|
|
|
replace: (_, props, restCode, badgesMod) => `vencordProps=${props},${restCode}Vencord.Api.Badges._getBadges(vencordProps).concat(${badgesMod}?.getBadges()??[])`,
|
2023-04-18 00:47:48 +00:00
|
|
|
},
|
2022-12-21 19:16:32 +00:00
|
|
|
{
|
2023-04-13 19:04:19 +00:00
|
|
|
// alt: "", aria-hidden: false, src: originalSrc
|
2023-04-18 00:47:48 +00:00
|
|
|
match: /alt:" ","aria-hidden":!0,src:(?=(\i)\.src)/g,
|
2023-04-13 19:04:19 +00:00
|
|
|
// ...badge.props, ..., src: badge.image ?? ...
|
|
|
|
replace: "...$1.props,$& $1.image??"
|
2022-12-21 19:16:32 +00:00
|
|
|
},
|
|
|
|
{
|
2023-04-05 20:45:14 +00:00
|
|
|
match: /children:function(?<=(\i)\.(?:tooltip|description),spacing:\d.+?)/g,
|
|
|
|
replace: "children:$1.component ? () => $self.renderBadgeComponent($1) : function"
|
2023-04-13 19:04:19 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
match: /onClick:function(?=.{0,200}href:(\i)\.link)/,
|
|
|
|
replace: "onClick:$1.onClick??function"
|
2022-12-21 19:16:32 +00:00
|
|
|
}
|
|
|
|
]
|
2022-11-11 22:50:09 +00:00
|
|
|
}
|
|
|
|
],
|
2022-11-16 23:21:13 +00:00
|
|
|
|
2023-05-02 00:55:38 +00:00
|
|
|
toolboxActions: {
|
|
|
|
async "Refetch Badges"() {
|
|
|
|
await loadBadges(true);
|
|
|
|
Toasts.show({
|
|
|
|
id: Toasts.genId(),
|
|
|
|
message: "Successfully refetched badges!",
|
|
|
|
type: Toasts.Type.SUCCESS
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async start() {
|
|
|
|
Vencord.Api.Badges.addBadge(ContributorBadge);
|
|
|
|
await loadBadges();
|
|
|
|
},
|
|
|
|
|
2023-04-05 20:45:14 +00:00
|
|
|
renderBadgeComponent: ErrorBoundary.wrap((badge: ProfileBadge & BadgeUserArgs) => {
|
|
|
|
const Component = badge.component!;
|
|
|
|
return <Component {...badge} />;
|
|
|
|
}, { noop: true }),
|
|
|
|
|
2022-11-16 23:21:13 +00:00
|
|
|
|
2023-04-29 00:06:52 +00:00
|
|
|
getDonorBadges(userId: string) {
|
|
|
|
return DonorBadges[userId]?.map(badge => ({
|
|
|
|
...badge,
|
|
|
|
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 00:50:51 +00:00
|
|
|
VencordNative.native.openExternal("https://github.com/sponsors/Vendicated");
|
2023-04-29 00:06:52 +00:00
|
|
|
}}>
|
|
|
|
<Modals.ModalRoot {...props}>
|
|
|
|
<Modals.ModalHeader>
|
|
|
|
<Flex style={{ width: "100%", justifyContent: "center" }}>
|
|
|
|
<Forms.FormTitle
|
|
|
|
tag="h2"
|
|
|
|
style={{
|
|
|
|
width: "100%",
|
|
|
|
textAlign: "center",
|
|
|
|
margin: 0
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Heart />
|
|
|
|
Vencord Donor
|
|
|
|
</Forms.FormTitle>
|
|
|
|
</Flex>
|
|
|
|
</Modals.ModalHeader>
|
|
|
|
<Modals.ModalContent>
|
|
|
|
<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>
|
|
|
|
</Modals.ModalContent>
|
|
|
|
<Modals.ModalFooter>
|
|
|
|
<Flex style={{ width: "100%", justifyContent: "center" }}>
|
|
|
|
<DonateButton />
|
|
|
|
</Flex>
|
|
|
|
</Modals.ModalFooter>
|
|
|
|
</Modals.ModalRoot>
|
|
|
|
</ErrorBoundary>
|
|
|
|
));
|
|
|
|
},
|
|
|
|
}));
|
2022-11-16 23:21:13 +00:00
|
|
|
}
|
2022-11-11 22:50:09 +00:00
|
|
|
});
|