mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-15 05:41:51 +00:00
fix: missing data in template safe objects
This commit is contained in:
parent
d4292205b9
commit
ab54dc215f
2 changed files with 55 additions and 30 deletions
|
@ -59,29 +59,36 @@ function isTemplateSafeValue(value: unknown): value is TemplateSafeValue {
|
||||||
export class TemplateSafeValueContainer {
|
export class TemplateSafeValueContainer {
|
||||||
// Fake property used for stricter type checks since TypeScript uses structural typing
|
// Fake property used for stricter type checks since TypeScript uses structural typing
|
||||||
_isTemplateSafeValueContainer: true;
|
_isTemplateSafeValueContainer: true;
|
||||||
|
|
||||||
[key: string]: TemplateSafeValue;
|
[key: string]: TemplateSafeValue;
|
||||||
|
|
||||||
constructor(data: Record<string, TemplateSafeValue> = {}) {
|
constructor(data?: Record<string, TemplateSafeValue>) {
|
||||||
for (const [key, value] of Object.entries(data)) {
|
if (data) {
|
||||||
if (!isTemplateSafeValue(value)) {
|
ingestDataIntoTemplateSafeValueContainer(this, data);
|
||||||
// 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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type TypedTemplateSafeValueContainer<T> = TemplateSafeValueContainer & T;
|
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>>(
|
export function createTypedTemplateSafeValueContainer<T extends Record<string, TemplateSafeValue>>(
|
||||||
data: T,
|
data: T,
|
||||||
): TypedTemplateSafeValueContainer<T> {
|
): TypedTemplateSafeValueContainer<T> {
|
||||||
|
|
|
@ -22,7 +22,11 @@ import {
|
||||||
ISavedMessageStickerData,
|
ISavedMessageStickerData,
|
||||||
SavedMessage,
|
SavedMessage,
|
||||||
} from "../data/entities/SavedMessage";
|
} from "../data/entities/SavedMessage";
|
||||||
import { TemplateSafeValueContainer, TypedTemplateSafeValueContainer } from "../templateFormatter";
|
import {
|
||||||
|
ingestDataIntoTemplateSafeValueContainer,
|
||||||
|
TemplateSafeValueContainer,
|
||||||
|
TypedTemplateSafeValueContainer,
|
||||||
|
} from "../templateFormatter";
|
||||||
|
|
||||||
type InputProps<T> = Omit<
|
type InputProps<T> = Omit<
|
||||||
{
|
{
|
||||||
|
@ -36,7 +40,8 @@ export class TemplateSafeGuild extends TemplateSafeValueContainer {
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
constructor(data: InputProps<TemplateSafeGuild>) {
|
constructor(data: InputProps<TemplateSafeGuild>) {
|
||||||
super(data);
|
super();
|
||||||
|
ingestDataIntoTemplateSafeValueContainer(this, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,7 +56,8 @@ export class TemplateSafeUser extends TemplateSafeValueContainer {
|
||||||
createdAt?: number;
|
createdAt?: number;
|
||||||
|
|
||||||
constructor(data: InputProps<TemplateSafeUser>) {
|
constructor(data: InputProps<TemplateSafeUser>) {
|
||||||
super(data);
|
super();
|
||||||
|
ingestDataIntoTemplateSafeValueContainer(this, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,7 +67,8 @@ export class TemplateSafeUnknownUser extends TemplateSafeValueContainer {
|
||||||
discriminator: string;
|
discriminator: string;
|
||||||
|
|
||||||
constructor(data: InputProps<TemplateSafeUnknownUser>) {
|
constructor(data: InputProps<TemplateSafeUnknownUser>) {
|
||||||
super(data);
|
super();
|
||||||
|
ingestDataIntoTemplateSafeValueContainer(this, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,7 +80,8 @@ export class TemplateSafeRole extends TemplateSafeValueContainer {
|
||||||
hoist: boolean;
|
hoist: boolean;
|
||||||
|
|
||||||
constructor(data: InputProps<TemplateSafeRole>) {
|
constructor(data: InputProps<TemplateSafeRole>) {
|
||||||
super(data);
|
super();
|
||||||
|
ingestDataIntoTemplateSafeValueContainer(this, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,7 +94,8 @@ export class TemplateSafeMember extends TemplateSafeUser {
|
||||||
guildName: string;
|
guildName: string;
|
||||||
|
|
||||||
constructor(data: InputProps<TemplateSafeMember>) {
|
constructor(data: InputProps<TemplateSafeMember>) {
|
||||||
super(data);
|
super({});
|
||||||
|
ingestDataIntoTemplateSafeValueContainer(this, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,7 +103,8 @@ export class TemplateSafeUnknownMember extends TemplateSafeUnknownUser {
|
||||||
user: TemplateSafeUnknownUser;
|
user: TemplateSafeUnknownUser;
|
||||||
|
|
||||||
constructor(data: InputProps<TemplateSafeUnknownMember>) {
|
constructor(data: InputProps<TemplateSafeUnknownMember>) {
|
||||||
super(data);
|
super({});
|
||||||
|
ingestDataIntoTemplateSafeValueContainer(this, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,7 +115,8 @@ export class TemplateSafeChannel extends TemplateSafeValueContainer {
|
||||||
parentId?: Snowflake;
|
parentId?: Snowflake;
|
||||||
|
|
||||||
constructor(data: InputProps<TemplateSafeChannel>) {
|
constructor(data: InputProps<TemplateSafeChannel>) {
|
||||||
super(data);
|
super();
|
||||||
|
ingestDataIntoTemplateSafeValueContainer(this, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,7 +128,8 @@ export class TemplateSafeStage extends TemplateSafeValueContainer {
|
||||||
topic: string;
|
topic: string;
|
||||||
|
|
||||||
constructor(data: InputProps<TemplateSafeStage>) {
|
constructor(data: InputProps<TemplateSafeStage>) {
|
||||||
super(data);
|
super();
|
||||||
|
ingestDataIntoTemplateSafeValueContainer(this, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,7 +142,8 @@ export class TemplateSafeEmoji extends TemplateSafeValueContainer {
|
||||||
mention: string;
|
mention: string;
|
||||||
|
|
||||||
constructor(data: InputProps<TemplateSafeEmoji>) {
|
constructor(data: InputProps<TemplateSafeEmoji>) {
|
||||||
super(data);
|
super();
|
||||||
|
ingestDataIntoTemplateSafeValueContainer(this, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,7 +159,8 @@ export class TemplateSafeSticker extends TemplateSafeValueContainer {
|
||||||
url: string;
|
url: string;
|
||||||
|
|
||||||
constructor(data: InputProps<TemplateSafeSticker>) {
|
constructor(data: InputProps<TemplateSafeSticker>) {
|
||||||
super(data);
|
super();
|
||||||
|
ingestDataIntoTemplateSafeValueContainer(this, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,7 +173,8 @@ export class TemplateSafeSavedMessage extends TemplateSafeValueContainer {
|
||||||
data: TemplateSafeSavedMessageData;
|
data: TemplateSafeSavedMessageData;
|
||||||
|
|
||||||
constructor(data: InputProps<TemplateSafeSavedMessage>) {
|
constructor(data: InputProps<TemplateSafeSavedMessage>) {
|
||||||
super(data);
|
super();
|
||||||
|
ingestDataIntoTemplateSafeValueContainer(this, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,7 +190,8 @@ export class TemplateSafeSavedMessageData extends TemplateSafeValueContainer {
|
||||||
timestamp: number;
|
timestamp: number;
|
||||||
|
|
||||||
constructor(data: InputProps<TemplateSafeSavedMessageData>) {
|
constructor(data: InputProps<TemplateSafeSavedMessageData>) {
|
||||||
super(data);
|
super();
|
||||||
|
ingestDataIntoTemplateSafeValueContainer(this, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -196,7 +212,8 @@ export class TemplateSafeCase extends TemplateSafeValueContainer {
|
||||||
log_message_id: string | null;
|
log_message_id: string | null;
|
||||||
|
|
||||||
constructor(data: InputProps<TemplateSafeCase>) {
|
constructor(data: InputProps<TemplateSafeCase>) {
|
||||||
super(data);
|
super();
|
||||||
|
ingestDataIntoTemplateSafeValueContainer(this, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -207,7 +224,8 @@ export class TemplateSafeMessage extends TemplateSafeValueContainer {
|
||||||
channel: TemplateSafeChannel;
|
channel: TemplateSafeChannel;
|
||||||
|
|
||||||
constructor(data: InputProps<TemplateSafeMessage>) {
|
constructor(data: InputProps<TemplateSafeMessage>) {
|
||||||
super(data);
|
super();
|
||||||
|
ingestDataIntoTemplateSafeValueContainer(this, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue