Equicord/src/utils/misc.ts

157 lines
5.3 KiB
TypeScript
Raw Normal View History

2022-10-22 01:17:06 +02: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/>.
*/
import { User } from "@vencord/discord-types";
import { ChannelStore, GuildMemberStore, IconUtils } from "@webpack/common";
2022-08-31 04:07:16 +02:00
2026-06-09 22:43:20 -04:00
import { EQUICORD_HELPERS, EquicordDevsById, GUILD_ID, KNOWN_ISSUES_CHANNEL_ID, SUPPORT_CHANNEL_ID, VencordDevsById } from "./constants";
2023-05-23 01:55:39 +02:00
2022-10-01 00:42:50 +02:00
/**
* Calls .join(" ") on the arguments
* classes("one", "two") => "one two"
*/
2024-07-02 00:17:40 +02:00
export function classes(...classes: Array<string | null | undefined | false>) {
return classes.filter(Boolean).join(" ");
2022-10-01 00:42:50 +02:00
}
2022-10-09 19:48:42 +02:00
2022-11-25 19:25:35 +01:00
/**
* Returns a promise that resolves after the specified amount of time
*/
2022-10-09 19:48:42 +02:00
export function sleep(ms: number): Promise<void> {
return new Promise(r => setTimeout(r, ms));
}
2022-10-12 05:35:34 +02:00
/**
* Check if obj is a true object: of type "object" and not null or array
*/
export function isObject(obj: unknown): obj is object {
return typeof obj === "object" && obj !== null && !Array.isArray(obj);
2022-10-12 22:22:21 +02:00
}
/**
* Check if an object is empty or in other words has no own properties
*/
export function isObjectEmpty(obj: object) {
for (const k in obj)
if (Object.hasOwn(obj, k)) return false;
return true;
}
/**
* Returns null if value is not a URL, otherwise return URL object.
* Avoids having to wrap url checks in a try/catch
*/
export function parseUrl(urlString: string): URL | null {
try {
return new URL(urlString);
} catch {
return null;
}
}
/**
* Checks whether an element is on screen
*/
export const checkIntersecting = (el: Element) => {
const elementBox = el.getBoundingClientRect();
const documentHeight = Math.max(document.documentElement.clientHeight, window.innerHeight);
return !(elementBox.bottom < 0 || elementBox.top - documentHeight >= 0);
};
export function identity<T>(value: T): T {
return value;
}
2023-04-15 02:27:11 +02:00
export const isPluginDev = (id: string) => Object.hasOwn(VencordDevsById, id);
export const shouldShowContributorBadge = (id: string) => isPluginDev(id) && VencordDevsById[id].badge !== false;
2024-04-18 19:53:52 -04:00
export const isEquicordPluginDev = (id: string) => Object.hasOwn(EquicordDevsById, id);
export const shouldShowEquicordContributorBadge = (id: string) => isEquicordPluginDev(id) && EquicordDevsById[id].badge !== false;
2024-05-02 14:58:31 +02:00
2025-09-28 23:14:12 -04:00
export const isAnyPluginDev = (id: string) => Object.hasOwn(VencordDevsById, id) || Object.hasOwn(EquicordDevsById, id);
2026-09-10 11:35:27 -04:00
export function pluralise(amount: number, singular: string, plural = singular + "s") {
2024-05-02 14:58:31 +02:00
return amount === 1 ? `${amount} ${singular}` : `${amount} ${plural}`;
}
export function interpolateIfDefined(strings: TemplateStringsArray, ...args: any[]) {
if (args.some(arg => arg == null)) return "";
return String.raw({ raw: strings }, ...args);
}
export function tryOrElse<T>(func: () => T, fallback: T): T {
try {
const res = func();
return res instanceof Promise
? res.catch(() => fallback) as T
: res;
} catch {
return fallback;
}
}
2025-09-15 00:15:30 -04:00
export function isEquicordGuild(id: string | null | undefined, isGuildId: boolean = false): boolean {
if (!id) return false;
2025-09-15 00:15:30 -04:00
if (isGuildId) return id === GUILD_ID;
const channel = ChannelStore.getChannel(id);
2026-02-11 00:54:49 -05:00
if (!channel) return false;
2025-09-15 00:15:30 -04:00
return channel.guild_id === GUILD_ID;
2025-09-15 00:27:27 -04:00
}
2025-09-15 00:15:30 -04:00
export function isSupportChannel(channelId: string | null | undefined): boolean {
2025-10-29 16:14:10 -04:00
if (!channelId) return false;
2025-09-15 00:27:27 -04:00
return channelId === SUPPORT_CHANNEL_ID;
2026-06-09 22:43:20 -04:00
}
export function isKnownIssuesCategory(channelId: string | null | undefined): boolean {
if (!channelId) return false;
return channelId === KNOWN_ISSUES_CHANNEL_ID;
2025-09-15 00:27:27 -04:00
}
export function isEquicordSupport(userId: string | null | undefined): boolean {
if (!userId) return false;
2025-09-15 00:27:27 -04:00
const member = GuildMemberStore.getMember(GUILD_ID, userId);
2026-02-11 00:54:49 -05:00
if (!member) return false;
return member.roles.includes(EQUICORD_HELPERS) || false;
2025-09-15 00:27:27 -04:00
}
2026-02-10 18:14:25 -05:00
export function removeFromArray<T>(arr: T[], predicate: (e: T) => boolean) {
const idx = arr.findIndex(predicate);
if (idx !== -1) arr.splice(idx, 1);
}
export function getUserAvatarUrl(user: User, guildId?: string, canAnimate?: boolean, size?: number): string {
const memberAvatar = guildId ? GuildMemberStore.getMember(guildId, user.id)?.avatar || null : null;
2026-05-25 16:23:27 -04:00
if (memberAvatar) {
return IconUtils.getGuildMemberAvatarURLSimple({
guildId: guildId!,
userId: user.id,
avatar: memberAvatar,
canAnimate,
size
2026-05-25 16:23:27 -04:00
});
}
return IconUtils.getUserAvatarURL(user, canAnimate, size) ?? IconUtils.getDefaultAvatarURL(user.id, user?.discriminator);
}
2026-09-10 11:35:27 -04:00
// this is all the way down here because i dont feel like dealing with conflicts
export const pluralize = pluralise;