3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 12:25:02 +00:00

Fix issues around embeds

This commit is contained in:
Dragory 2021-08-19 19:33:47 +03:00
parent dc53de8ae3
commit 81514276e9
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
8 changed files with 111 additions and 21 deletions

View file

@ -44,6 +44,7 @@ import { ChannelTypeStrings } from "./types";
import { sendDM } from "./utils/sendDM";
import { waitForButtonConfirm } from "./utils/waitForInteraction";
import { decodeAndValidateStrict, StrictValidationError } from "./validatorUtils";
import { z } from "zod";
const fsp = fs.promises;
@ -320,25 +321,117 @@ export const tEmbed = t.type({
),
});
export const zEmbedInput = z.object({
title: z.string().optional(),
description: z.string().optional(),
url: z.string().optional(),
timestamp: z.number().optional(),
color: z.number().optional(),
footer: z.optional(
z.object({
text: z.string(),
icon_url: z.string().optional(),
}),
),
image: z.optional(
z.object({
url: z.string().optional(),
width: z.number().optional(),
height: z.number().optional(),
}),
),
thumbnail: z.optional(
z.object({
url: z.string().optional(),
width: z.number().optional(),
height: z.number().optional(),
}),
),
video: z.optional(
z.object({
url: z.string().optional(),
width: z.number().optional(),
height: z.number().optional(),
}),
),
provider: z.optional(
z.object({
name: z.string(),
url: z.string().optional(),
}),
),
fields: z.optional(
z.array(
z.object({
name: z.string().optional(),
value: z.string().optional(),
inline: z.boolean().optional(),
}),
),
),
author: z
.optional(
z.object({
name: z.string(),
url: z.string().optional(),
width: z.number().optional(),
height: z.number().optional(),
}),
)
.nullable(),
});
export type EmbedWith<T extends keyof MessageEmbedOptions> = MessageEmbedOptions &
Pick<Required<MessageEmbedOptions>, T>;
export const zStrictMessageContent = z.object({
content: z.string().optional(),
tts: z.boolean().optional(),
embeds: z.array(zEmbedInput).optional(),
});
export type ZStrictMessageContent = z.infer<typeof zStrictMessageContent>;
export type StrictMessageContent = {
content?: string;
tts?: boolean;
disableEveryone?: boolean;
embed?: MessageEmbedOptions;
embeds?: MessageEmbedOptions[];
};
export const tStrictMessageContent = t.type({
content: tNullable(t.string),
tts: tNullable(t.boolean),
disableEveryone: tNullable(t.boolean),
embed: tNullable(tEmbed),
embeds: tNullable(t.array(tEmbed)),
});
export const tMessageContent = t.union([t.string, tStrictMessageContent]);
export function validateAndParseMessageContent(input: unknown): StrictMessageContent {
if (input == null) {
return {};
}
if (typeof input !== "object") {
return { content: String(input) };
}
// Migrate embed -> embeds
if ((input as any).embed) {
(input as any).embeds = [(input as any).embed];
delete (input as any).embed;
}
return (zStrictMessageContent.parse(input) as unknown) as StrictMessageContent;
}
/**
* Mirrors AllowedMentions from Eris
*/