feat: Badge API (#206)

This commit is contained in:
megumin 2022-11-11 22:50:09 +00:00 committed by GitHub
parent 62e0787cf2
commit 30ca4f1cf9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 237 additions and 82 deletions

100
src/api/Badges.ts Normal file
View file

@ -0,0 +1,100 @@
/*
* 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/>.
*/
import { User } from "discord-types/general";
import { HTMLProps } from "react";
export enum BadgePosition {
START,
END
}
export interface ProfileBadge {
/** The tooltip to show on hover */
tooltip: string;
/** The custom image to use */
image?: string;
/** Action to perform when you click the badge */
onClick?(): void;
/** Should the user display this badge? */
shouldShow?(userInfo: BadgeUserArgs): boolean;
/** Optional props (e.g. style) for the badge */
props?: HTMLProps<HTMLImageElement>;
/** Insert at start or end? */
position?: BadgePosition;
/** The badge name to display. Discord uses this, but we don't. */
key?: string;
}
const Badges = new Set<ProfileBadge>();
/**
* Register a new badge with the Badges API
* @param badge The badge to register
*/
export function addBadge(badge: ProfileBadge) {
Badges.add(badge);
}
/**
* Unregister a badge from the Badges API
* @param badge The badge to remove
*/
export function removeBadge(badge: ProfileBadge) {
return Badges.delete(badge);
}
/**
* Inject badges into the profile badges array.
* You probably don't need to use this.
*/
export function inject(badgeArray: ProfileBadge[], args: BadgeUserArgs) {
for (const badge of Badges) {
if (!badge.shouldShow || badge.shouldShow(args)) {
badge.position === BadgePosition.START
? badgeArray.unshift(badge)
: badgeArray.push(badge);
}
}
return badgeArray;
}
export interface BadgeUserArgs {
user: User;
profile: Profile;
premiumSince: Date;
premiumGuildSince?: Date;
}
interface ConnectedAccount {
type: string;
id: string;
name: string;
verified: boolean;
}
interface Profile {
connectedAccounts: ConnectedAccount[];
premiumType: number;
premiumSince: string;
premiumGuildSince?: any;
lastFetched: number;
profileFetchFailed: boolean;
application?: any;
}

View file

@ -16,6 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import * as $Badges from "./Badges";
import * as $Commands from "./Commands"; import * as $Commands from "./Commands";
import * as $DataStore from "./DataStore"; import * as $DataStore from "./DataStore";
import * as $MessageAccessories from "./MessageAccessories"; import * as $MessageAccessories from "./MessageAccessories";
@ -57,5 +58,9 @@ const DataStore = $DataStore;
* An API allowing you to add custom components as message accessories * An API allowing you to add custom components as message accessories
*/ */
const MessageAccessories = $MessageAccessories; const MessageAccessories = $MessageAccessories;
/**
* An API allowing you to add badges to user profiles
*/
const Badges = $Badges;
export { Commands,DataStore, MessageAccessories, MessageEvents, Notices }; export { Badges, Commands, DataStore, MessageAccessories, MessageEvents, Notices };

70
src/plugins/apiBadges.tsx Normal file
View file

@ -0,0 +1,70 @@
/*
* 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/>.
*/
import { BadgePosition, ProfileBadge } from "../api/Badges";
import { Devs } from "../utils/constants";
import IpcEvents from "../utils/IpcEvents";
import definePlugin from "../utils/types";
const CONTRIBUTOR_BADGE = "https://media.discordapp.net/stickers/1026517526106087454.webp";
/** List of vencord contributor IDs */
const contributorIds: string[] = Object.values(Devs).map(d => d.id.toString());
const ContributorBadge: ProfileBadge = {
tooltip: "Vencord Contributor",
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
}
},
shouldShow: ({ user }) => contributorIds.includes(user.id),
onClick: () => VencordNative.ipc.invoke(IpcEvents.OPEN_EXTERNAL, "https://github.com/Vendicated/Vencord")
};
export default definePlugin({
name: "BadgeAPI",
description: "API to add badges to users.",
authors: [Devs.Megu],
required: true,
patches: [
/* Patch the badges array */
{
find: "PREMIUM_GUILD_SUBSCRIPTION_TOOLTIP.format({date:",
replacement: {
match: /&&((\w{1,3})\.push\({tooltip:\w{1,3}\.\w{1,3}\.Messages\.PREMIUM_GUILD_SUBSCRIPTION_TOOLTIP\.format.+?;)(?:return\s\w{1,3};?})/,
replace: (_, m, badgeArray) => `&&${m} return Vencord.Api.Badges.inject(${badgeArray}, arguments[0]);}`,
}
},
/* Patch the badge list component on user profiles */
{
find: "Messages.PROFILE_USER_BADGES,role:",
replacement: {
match: /src:(\w{1,3})\[(\w{1,3})\.key\],/,
// <img src={badge.image ?? imageMap[badge.key]} {...badge.props} />
replace: (_, imageMap, badge) => `src: ${badge}.image ?? ${imageMap}[${badge}.key], ...${badge}.props,`
}
}
],
start() {
Vencord.Api.Badges.addBadge(ContributorBadge);
},
});

View file

@ -22,13 +22,7 @@ import definePlugin, { OptionType } from "../utils/types";
export default definePlugin({ export default definePlugin({
name: "BANger", name: "BANger",
description: "Replaces the GIF in the ban dialogue with a custom one.", description: "Replaces the GIF in the ban dialogue with a custom one.",
authors: [ authors: [Devs.Xinto, Devs.Glitch],
{
name: "Xinto",
id: 423915768191647755n
},
Devs.Glitch
],
patches: [ patches: [
{ {
find: "BAN_CONFIRM_TITLE.", find: "BAN_CONFIRM_TITLE.",

View file

@ -23,6 +23,7 @@ import {
removePreEditListener, removePreEditListener,
removePreSendListener, removePreSendListener,
} from "../../api/MessageEvents"; } from "../../api/MessageEvents";
import { Devs } from "../../utils/constants";
import definePlugin from "../../utils/types"; import definePlugin from "../../utils/types";
import { defaultRules } from "./defaultRules"; import { defaultRules } from "./defaultRules";
@ -33,12 +34,7 @@ const reHasRegExpChar = RegExp(reRegExpChar.source);
export default definePlugin({ export default definePlugin({
name: "clearURLs", name: "clearURLs",
description: "Removes tracking garbage from URLs", description: "Removes tracking garbage from URLs",
authors: [ authors: [Devs.adryd],
{
name: "adryd",
id: 0n,
},
],
dependencies: ["MessageEventsAPI"], dependencies: ["MessageEventsAPI"],
escapeRegExp(str: string) { escapeRegExp(str: string) {

View file

@ -31,7 +31,7 @@ export default definePlugin({
Devs.Megu, Devs.Megu,
Devs.Ven, Devs.Ven,
Devs.Nickyux, Devs.Nickyux,
{ name: "BanTheNons", id: 460478012794863637n }, Devs.BanTheNons
], ],
description: "Enable Access to Experiments in Discord!", description: "Enable Access to Experiments in Discord!",
patches: [{ patches: [{

View file

@ -22,10 +22,7 @@ import definePlugin from "../utils/types";
export default definePlugin({ export default definePlugin({
name: "iLoveSpam", name: "iLoveSpam",
description: "Do not hide messages from 'likely spammers'", description: "Do not hide messages from 'likely spammers'",
authors: [ authors: [Devs.botato, Devs.Animal],
Devs.botato,
Devs.Animal,
],
patches: [ patches: [
{ {
find: "),{hasFlag:", find: "),{hasFlag:",

View file

@ -17,17 +17,13 @@
*/ */
import { findOption, OptionalMessageOption } from "../api/Commands"; import { findOption, OptionalMessageOption } from "../api/Commands";
import { Devs } from "../utils/constants";
import definePlugin from "../utils/types"; import definePlugin from "../utils/types";
export default definePlugin({ export default definePlugin({
name: "moarKaomojis", name: "moarKaomojis",
description: "Adds more Kaomojis to discord. ヽ(´▽`)/", description: "Adds more Kaomojis to discord. ヽ(´▽`)/",
authors: [ authors: [Devs.JacobTm],
{
name: "Jacob.Tm",
id: 302872992097107991n
}
],
dependencies: ["CommandsAPI"], dependencies: ["CommandsAPI"],
commands: [ commands: [
{ name: "dissatisfaction", description: " " }, { name: "dissatisfaction", description: " " },

View file

@ -32,14 +32,7 @@ function mock(input: string): string {
export default definePlugin({ export default definePlugin({
name: "MoreCommands", name: "MoreCommands",
description: "echo, lenny, mock", description: "echo, lenny, mock",
authors: [ authors: [Devs.Arjix, Devs.echo, Devs.Samu],
Devs.Arjix,
Devs.echo,
{
name: "ICodeInAssembly",
id: 702973430449832038n
}
],
dependencies: ["CommandsAPI"], dependencies: ["CommandsAPI"],
commands: [ commands: [
{ {

View file

@ -52,11 +52,7 @@ interface StickerPack {
export default definePlugin({ export default definePlugin({
name: "NitroBypass", name: "NitroBypass",
authors: [ authors: [Devs.Arjix, Devs.D3SOX, Devs.Ven],
Devs.Arjix,
Devs.D3SOX,
Devs.Ven
],
description: "Allows you to stream in nitro quality and send fake emojis/stickers.", description: "Allows you to stream in nitro quality and send fake emojis/stickers.",
dependencies: ["MessageEventsAPI"], dependencies: ["MessageEventsAPI"],

View file

@ -24,10 +24,7 @@ import { Settings } from "../Vencord";
export default definePlugin({ export default definePlugin({
name: "NoCanaryMessageLinks", name: "NoCanaryMessageLinks",
description: "Allows you to change/remove the subdomain of discord message and channel links", description: "Allows you to change/remove the subdomain of discord message and channel links",
authors: [ authors: [Devs.Samu, Devs.nea],
Devs.Samu,
Devs.nea,
],
options: { options: {
linkPrefix: { linkPrefix: {
description: "The subdomain for your discord message links", description: "The subdomain for your discord message links",

View file

@ -16,15 +16,13 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { Devs } from "../utils/constants";
import definePlugin from "../utils/types"; import definePlugin from "../utils/types";
export default definePlugin({ export default definePlugin({
name: "NoReplyMention", name: "NoReplyMention",
description: "Disables reply pings by default", description: "Disables reply pings by default",
authors: [{ authors: [Devs.DustyAngel47],
name: "DustyAngel47",
id: 714583473804935238n
}],
patches: [ patches: [
{ {
find: "CREATE_PENDING_REPLY:function", find: "CREATE_PENDING_REPLY:function",

View file

@ -16,6 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { Devs } from "../../utils/constants";
import definePlugin, { OptionType } from "../../utils/types"; import definePlugin, { OptionType } from "../../utils/types";
import PronounsAboutComponent from "./components/PronounsAboutComponent"; import PronounsAboutComponent from "./components/PronounsAboutComponent";
import PronounsChatComponent from "./components/PronounsChatComponent"; import PronounsChatComponent from "./components/PronounsChatComponent";
@ -28,10 +29,7 @@ export enum PronounsFormat {
export default definePlugin({ export default definePlugin({
name: "PronounDB", name: "PronounDB",
authors: [{ authors: [Devs.Tyman],
name: "Tyman",
id: 487443883127472129n
}],
description: "Adds pronouns to user messages using pronoundb", description: "Adds pronouns to user messages using pronoundb",
patches: [ patches: [
// Patch the chat timestamp element // Patch the chat timestamp element

View file

@ -34,17 +34,7 @@ waitFor(m => m.can && m.initialize, m => ({ can } = m));
export default definePlugin({ export default definePlugin({
name: "ShowHiddenChannels", name: "ShowHiddenChannels",
description: "Show hidden channels", description: "Show hidden channels",
authors: [ authors: [Devs.BigDuck, Devs.AverageReactEnjoyer, Devs.D3SOX],
{
name: "BigDuck",
id: 1024588272623681609n
},
{
name: "Average React Enjoyer",
id: 1004904120056029256n
},
Devs.D3SOX,
],
options: { options: {
hideUnreads: { hideUnreads: {
description: "Hide unreads", description: "Hide unreads",

View file

@ -23,17 +23,7 @@ import { Player } from "./PlayerComponent";
export default definePlugin({ export default definePlugin({
name: "SpotifyControls", name: "SpotifyControls",
description: "Spotify Controls", description: "Spotify Controls",
authors: [ authors: [Devs.Ven, Devs.afn, Devs.KraXen72],
Devs.Ven,
{
id: 420043923822608384n,
name: "afn",
},
{
id: 379304073515499530n,
name: "KraXen72"
}
],
dependencies: ["MenuItemDeobfuscatorApi"], dependencies: ["MenuItemDeobfuscatorApi"],
patches: [ patches: [
{ {

View file

@ -17,6 +17,7 @@
*/ */
import { findOption, RequiredMessageOption } from "../api/Commands"; import { findOption, RequiredMessageOption } from "../api/Commands";
import { Devs } from "../utils/constants";
import definePlugin from "../utils/types"; import definePlugin from "../utils/types";
const endings = [ const endings = [
@ -109,10 +110,7 @@ function uwuify(message: string): string {
export default definePlugin({ export default definePlugin({
name: "UwUifier", name: "UwUifier",
description: "Simply uwuify commands", description: "Simply uwuify commands",
authors: [{ authors: [Devs.echo],
name: "ECHO",
id: 712639419785412668n
}],
dependencies: ["CommandsAPI"], dependencies: ["CommandsAPI"],
commands: [ commands: [

View file

@ -28,10 +28,7 @@ const timers = {} as Record<string, {
export default definePlugin({ export default definePlugin({
name: "vcDoubleClick", name: "vcDoubleClick",
description: "Join VCs via DoubleClick instead of single click", description: "Join VCs via DoubleClick instead of single click",
authors: [ authors: [Devs.Ven, Devs.D3SOX],
Devs.Ven,
Devs.D3SOX,
],
patches: [ patches: [
{ {
find: "VoiceChannel.renderPopout", find: "VoiceChannel.renderPopout",

View file

@ -23,7 +23,7 @@ export const WEBPACK_CHUNK = "webpackChunkdiscord_app";
export const REACT_GLOBAL = "Vencord.Webpack.Common.React"; export const REACT_GLOBAL = "Vencord.Webpack.Common.React";
export const VENCORD_USER_AGENT = `Vencord/${gitHash}${gitRemote ? ` (https://github.com/${gitRemote})` : ""}`; export const VENCORD_USER_AGENT = `Vencord/${gitHash}${gitRemote ? ` (https://github.com/${gitRemote})` : ""}`;
// Add yourself here if you made more than one plugin // Add yourself here if you made a plugin
export const Devs = Object.freeze({ export const Devs = Object.freeze({
Ven: { Ven: {
name: "Vendicated", name: "Vendicated",
@ -92,5 +92,45 @@ export const Devs = Object.freeze({
Nickyux: { Nickyux: {
name: "Nickyux", name: "Nickyux",
id: 427146305651998721n id: 427146305651998721n
},
Xinto: {
name: "Xinto",
id: 423915768191647755n
},
JacobTm: {
name: "Jacob.Tm",
id: 302872992097107991n
},
DustyAngel47: {
name: "DustyAngel47",
id: 714583473804935238n
},
BanTheNons: {
name: "BanTheNons",
id: 460478012794863637n
},
BigDuck: {
name: "BigDuck",
id: 1024588272623681609n
},
AverageReactEnjoyer: {
name: "Average React Enjoyer",
id: 1004904120056029256n
},
adryd: {
name: "adryd",
id: 0n
},
Tyman: {
name: "Tyman",
id: 487443883127472129n
},
afn: {
name: "afn",
id: 420043923822608384n
},
KraXen72: {
name: "KraXen72",
id: 379304073515499530n
} }
}); });