3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-14 21:31:50 +00:00

fix: missing data in template safe objects

This commit is contained in:
Dragory 2023-04-01 21:33:40 +03:00
parent d4292205b9
commit ab54dc215f
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
2 changed files with 55 additions and 30 deletions

View file

@ -59,29 +59,36 @@ function isTemplateSafeValue(value: unknown): value is TemplateSafeValue {
export class TemplateSafeValueContainer {
// Fake property used for stricter type checks since TypeScript uses structural typing
_isTemplateSafeValueContainer: true;
[key: string]: TemplateSafeValue;
constructor(data: Record<string, TemplateSafeValue> = {}) {
for (const [key, value] of Object.entries(data)) {
if (!isTemplateSafeValue(value)) {
// tslint:disable:no-console
console.error("=== CONTEXT FOR UNSAFE VALUE ===");
console.error("stringified:", JSON.stringify(value));
console.error("typeof:", typeof value);
console.error("constructor name:", (value as any)?.constructor?.name);
console.error("=== /CONTEXT FOR UNSAFE VALUE ===");
// tslint:enable:no-console
throw new Error(`Unsafe value for key "${key}" in SafeTemplateValueContainer`);
}
this[key] = value;
constructor(data?: Record<string, TemplateSafeValue>) {
if (data) {
ingestDataIntoTemplateSafeValueContainer(this, data);
}
}
}
export type TypedTemplateSafeValueContainer<T> = TemplateSafeValueContainer & T;
export function ingestDataIntoTemplateSafeValueContainer(
target: TemplateSafeValueContainer,
data: Record<string, TemplateSafeValue> = {},
) {
for (const [key, value] of Object.entries(data)) {
if (!isTemplateSafeValue(value)) {
// tslint:disable:no-console
console.error("=== CONTEXT FOR UNSAFE VALUE ===");
console.error("stringified:", JSON.stringify(value));
console.error("typeof:", typeof value);
console.error("constructor name:", (value as any)?.constructor?.name);
console.error("=== /CONTEXT FOR UNSAFE VALUE ===");
// tslint:enable:no-console
throw new Error(`Unsafe value for key "${key}" in SafeTemplateValueContainer`);
}
target[key] = value;
}
}
export function createTypedTemplateSafeValueContainer<T extends Record<string, TemplateSafeValue>>(
data: T,
): TypedTemplateSafeValueContainer<T> {

View file

@ -22,7 +22,11 @@ import {
ISavedMessageStickerData,
SavedMessage,
} from "../data/entities/SavedMessage";
import { TemplateSafeValueContainer, TypedTemplateSafeValueContainer } from "../templateFormatter";
import {
ingestDataIntoTemplateSafeValueContainer,
TemplateSafeValueContainer,
TypedTemplateSafeValueContainer,
} from "../templateFormatter";
type InputProps<T> = Omit<
{
@ -36,7 +40,8 @@ export class TemplateSafeGuild extends TemplateSafeValueContainer {
name: string;
constructor(data: InputProps<TemplateSafeGuild>) {
super(data);
super();
ingestDataIntoTemplateSafeValueContainer(this, data);
}
}
@ -51,7 +56,8 @@ export class TemplateSafeUser extends TemplateSafeValueContainer {
createdAt?: number;
constructor(data: InputProps<TemplateSafeUser>) {
super(data);
super();
ingestDataIntoTemplateSafeValueContainer(this, data);
}
}
@ -61,7 +67,8 @@ export class TemplateSafeUnknownUser extends TemplateSafeValueContainer {
discriminator: string;
constructor(data: InputProps<TemplateSafeUnknownUser>) {
super(data);
super();
ingestDataIntoTemplateSafeValueContainer(this, data);
}
}
@ -73,7 +80,8 @@ export class TemplateSafeRole extends TemplateSafeValueContainer {
hoist: boolean;
constructor(data: InputProps<TemplateSafeRole>) {
super(data);
super();
ingestDataIntoTemplateSafeValueContainer(this, data);
}
}
@ -86,7 +94,8 @@ export class TemplateSafeMember extends TemplateSafeUser {
guildName: string;
constructor(data: InputProps<TemplateSafeMember>) {
super(data);
super({});
ingestDataIntoTemplateSafeValueContainer(this, data);
}
}
@ -94,7 +103,8 @@ export class TemplateSafeUnknownMember extends TemplateSafeUnknownUser {
user: TemplateSafeUnknownUser;
constructor(data: InputProps<TemplateSafeUnknownMember>) {
super(data);
super({});
ingestDataIntoTemplateSafeValueContainer(this, data);
}
}
@ -105,7 +115,8 @@ export class TemplateSafeChannel extends TemplateSafeValueContainer {
parentId?: Snowflake;
constructor(data: InputProps<TemplateSafeChannel>) {
super(data);
super();
ingestDataIntoTemplateSafeValueContainer(this, data);
}
}
@ -117,7 +128,8 @@ export class TemplateSafeStage extends TemplateSafeValueContainer {
topic: string;
constructor(data: InputProps<TemplateSafeStage>) {
super(data);
super();
ingestDataIntoTemplateSafeValueContainer(this, data);
}
}
@ -130,7 +142,8 @@ export class TemplateSafeEmoji extends TemplateSafeValueContainer {
mention: string;
constructor(data: InputProps<TemplateSafeEmoji>) {
super(data);
super();
ingestDataIntoTemplateSafeValueContainer(this, data);
}
}
@ -146,7 +159,8 @@ export class TemplateSafeSticker extends TemplateSafeValueContainer {
url: string;
constructor(data: InputProps<TemplateSafeSticker>) {
super(data);
super();
ingestDataIntoTemplateSafeValueContainer(this, data);
}
}
@ -159,7 +173,8 @@ export class TemplateSafeSavedMessage extends TemplateSafeValueContainer {
data: TemplateSafeSavedMessageData;
constructor(data: InputProps<TemplateSafeSavedMessage>) {
super(data);
super();
ingestDataIntoTemplateSafeValueContainer(this, data);
}
}
@ -175,7 +190,8 @@ export class TemplateSafeSavedMessageData extends TemplateSafeValueContainer {
timestamp: number;
constructor(data: InputProps<TemplateSafeSavedMessageData>) {
super(data);
super();
ingestDataIntoTemplateSafeValueContainer(this, data);
}
}
@ -196,7 +212,8 @@ export class TemplateSafeCase extends TemplateSafeValueContainer {
log_message_id: string | null;
constructor(data: InputProps<TemplateSafeCase>) {
super(data);
super();
ingestDataIntoTemplateSafeValueContainer(this, data);
}
}
@ -207,7 +224,8 @@ export class TemplateSafeMessage extends TemplateSafeValueContainer {
channel: TemplateSafeChannel;
constructor(data: InputProps<TemplateSafeMessage>) {
super(data);
super();
ingestDataIntoTemplateSafeValueContainer(this, data);
}
}