3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-06-08 00:05:01 +00:00

Merge branch 'master' of github.com:ZeppelinBot/Zeppelin into feat/application-commands

This commit is contained in:
Lily Bergonzat 2024-02-16 14:26:34 +01:00
commit 2c0e4b37ca
235 changed files with 3464 additions and 4799 deletions

View file

@ -0,0 +1,6 @@
import { ZodIssue } from "zod";
export function formatZodIssue(issue: ZodIssue): string {
const path = issue.path.join("/");
return `${path}: ${issue.message}`;
}

View file

@ -1,16 +0,0 @@
import { either } from "fp-ts/lib/Either";
import * as t from "io-ts";
import { intToRgb } from "./intToRgb";
import { parseColor } from "./parseColor";
import { rgbToInt } from "./rgbToInt";
export const tColor = new t.Type<number, string>(
"tColor",
(s): s is number => typeof s === "number",
(from, to) =>
either.chain(t.string.validate(from, to), (input) => {
const parsedColor = parseColor(input);
return parsedColor == null ? t.failure(from, to, "Invalid color") : t.success(rgbToInt(parsedColor));
}),
(s) => intToRgb(s).join(","),
);

View file

@ -1,13 +0,0 @@
import { either } from "fp-ts/lib/Either";
import * as t from "io-ts";
import { isValidTimezone } from "./isValidTimezone";
export const tValidTimezone = new t.Type<string, string>(
"tValidTimezone",
(s): s is string => typeof s === "string",
(from, to) =>
either.chain(t.string.validate(from, to), (input) => {
return isValidTimezone(input) ? t.success(input) : t.failure(from, to, `Invalid timezone: ${input}`);
}),
(s) => s,
);

View file

@ -13,7 +13,6 @@ import {
User,
} from "discord.js";
import { GuildPluginData } from "knub";
import { UnknownUser, renderUserUsername } from "src/utils";
import { Case } from "../data/entities/Case";
import {
ISavedMessageAttachmentData,
@ -27,6 +26,7 @@ import {
TypedTemplateSafeValueContainer,
ingestDataIntoTemplateSafeValueContainer,
} from "../templateFormatter";
import { UnknownUser, renderUsername } from "../utils";
type InputProps<T> = Omit<
{
@ -49,9 +49,10 @@ export class TemplateSafeUser extends TemplateSafeValueContainer {
id: Snowflake | string;
username: string;
discriminator: string;
globalName?: string;
mention: string;
tag: string;
avatarURL?: string;
avatarURL: string;
bot?: boolean;
createdAt?: number;
renderedUsername: string;
@ -91,7 +92,7 @@ export class TemplateSafeMember extends TemplateSafeUser {
nick: string;
roles: TemplateSafeRole[];
joinedAt?: number;
// guildAvatarURL: string, Once DJS supports per-server avatars
guildAvatarURL: string;
guildName: string;
constructor(data: InputProps<TemplateSafeMember>) {
@ -249,7 +250,7 @@ export function userToTemplateSafeUser(user: User | UnknownUser): TemplateSafeUs
discriminator: "0000",
mention: `<@${user.id}>`,
tag: "Unknown#0000",
renderedUsername: renderUserUsername(user),
renderedUsername: renderUsername(user),
});
}
@ -257,12 +258,13 @@ export function userToTemplateSafeUser(user: User | UnknownUser): TemplateSafeUs
id: user.id,
username: user.username,
discriminator: user.discriminator,
globalName: user.globalName,
mention: `<@${user.id}>`,
tag: user.tag,
avatarURL: user.displayAvatarURL?.(),
avatarURL: user.displayAvatarURL(),
bot: user.bot,
createdAt: user.createdTimestamp,
renderedUsername: renderUserUsername(user),
renderedUsername: renderUsername(user),
});
}
@ -285,6 +287,7 @@ export function memberToTemplateSafeMember(member: GuildMember | PartialGuildMem
nick: member.nickname ?? "*None*",
roles: [...member.roles.cache.mapValues((r) => roleToTemplateSafeRole(r)).values()],
joinedAt: member.joinedTimestamp ?? undefined,
guildAvatarURL: member.displayAvatarURL(),
guildName: member.guild.name,
});
}

View file

@ -14,3 +14,11 @@ export type Awaitable<T = unknown> = T | Promise<T>;
export type DeepMutable<T> = {
-readonly [P in keyof T]: DeepMutable<T[P]>;
};
// From https://stackoverflow.com/a/70262876/316944
export declare abstract class As<Tag extends keyof never> {
private static readonly $as$: unique symbol;
private [As.$as$]: Record<Tag, true>;
}
export type Brand<T, B extends keyof never> = T & As<B>;

View file

@ -0,0 +1,15 @@
import z from "zod";
import { parseColor } from "./parseColor";
import { rgbToInt } from "./rgbToInt";
export const zColor = z.string().transform((val, ctx) => {
const parsedColor = parseColor(val);
if (parsedColor == null) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Invalid color",
});
return z.NEVER;
}
return rgbToInt(parsedColor);
});

View file

@ -0,0 +1,8 @@
import { ZodString } from "zod";
import { isValidTimezone } from "./isValidTimezone";
export function zValidTimezone<Z extends ZodString>(z: Z) {
return z.refine((val) => isValidTimezone(val), {
message: "Invalid timezone",
});
}