From c46e9515d6069f0930fa464894375f122630cbb6 Mon Sep 17 00:00:00 2001 From: MARCROCK22 <57925328+MARCROCK22@users.noreply.github.com> Date: Thu, 25 Apr 2024 17:06:53 -0400 Subject: [PATCH] fix: objectToLower & objectToSnake types --- src/commands/applications/menucontext.ts | 4 ++-- src/common/shorters/guilds.ts | 4 ++-- src/common/types/util.ts | 28 ++++++++++++++++++++++-- src/structures/AutoModerationRule.ts | 4 ++-- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/commands/applications/menucontext.ts b/src/commands/applications/menucontext.ts index 7265f55..697a9e4 100644 --- a/src/commands/applications/menucontext.ts +++ b/src/commands/applications/menucontext.ts @@ -1,4 +1,4 @@ -import { ApplicationCommandType, MessageFlags } from 'discord-api-types/v10'; +import { type APIMessage, ApplicationCommandType, MessageFlags } from 'discord-api-types/v10'; import type { ContextMenuCommand, ReturnCache, WebhookMessage } from '../..'; import { toSnakeCase, @@ -50,7 +50,7 @@ export class MenuCommandContext< switch (this.interaction.data.type) { case ApplicationCommandType.Message: { const data = this.interaction.data.resolved.messages[this.interaction.data.targetId as Lowercase]; - return new Message(this.client, toSnakeCase(data)) as never; + return new Message(this.client, toSnakeCase(data) as APIMessage) as never; } case ApplicationCommandType.User: { const data = this.interaction.data.resolved.users[this.interaction.data.targetId as Lowercase]; diff --git a/src/common/shorters/guilds.ts b/src/common/shorters/guilds.ts index 2afcc70..49d2bb9 100644 --- a/src/common/shorters/guilds.ts +++ b/src/common/shorters/guilds.ts @@ -10,7 +10,7 @@ import type { RESTPostAPIGuildChannelJSONBody, RESTPostAPIGuildsJSONBody, } from 'discord-api-types/v10'; -import type { ObjectToLower } from '..'; +import { toSnakeCase, type ObjectToLower } from '..'; import { resolveFiles } from '../../builders'; import { AnonymousGuild, @@ -267,7 +267,7 @@ export class GuildShorter extends BaseShorter { return this.client.proxy .guilds(guildId) ['auto-moderation'].rules(ruleId) - .patch({ body, reason }) + .patch({ body: toSnakeCase(body), reason }) .then(rule => new AutoModerationRule(this.client, rule)); }, }; diff --git a/src/common/types/util.ts b/src/common/types/util.ts index 786c8e0..a639839 100644 --- a/src/common/types/util.ts +++ b/src/common/types/util.ts @@ -104,17 +104,41 @@ export type ObjectToLower = Identify<{ ? Identify[]> : T[K] extends object ? Identify> - : T[K]; + : AuxIsStrictlyUndefined extends true + ? undefined + : ObjectToLowerUndefined; }>; +export type ObjectToLowerUndefined = T extends unknown[] + ? ObjectToLower[] + : Identify<{ + [K in keyof T as CamelCase>]?: T[K] extends unknown[] + ? ObjectToLower[] + : T[K] extends object + ? ObjectToLower + : T[K]; + }>; + export type ObjectToSnake = Identify<{ [K in keyof T as SnakeCase>]: T[K] extends unknown[] ? Identify[]> : T[K] extends object ? Identify> - : T[K]; + : AuxIsStrictlyUndefined extends true + ? undefined + : ObjectToSnakeUndefined; }>; +export type ObjectToSnakeUndefined = T extends unknown[] + ? ObjectToSnake[] + : Identify<{ + [K in keyof T as SnakeCase>]?: T[K] extends unknown[] + ? ObjectToSnake[] + : T[K] extends object + ? ObjectToSnake + : T[K]; + }>; + export type UnionToTuple = (U extends void ? void : (arg: () => U) => never) extends ( arg: infer I, ) => void diff --git a/src/structures/AutoModerationRule.ts b/src/structures/AutoModerationRule.ts index 46e6a8e..1d3d038 100644 --- a/src/structures/AutoModerationRule.ts +++ b/src/structures/AutoModerationRule.ts @@ -4,7 +4,7 @@ import type { RESTPostAPIAutoModerationRuleJSONBody, } from 'discord-api-types/v10'; import type { UsingClient } from '../commands'; -import type { MethodContext, ObjectToLower } from '../common'; +import { toCamelCase, type MethodContext, type ObjectToLower } from '../common'; import { DiscordBase } from './extra/DiscordBase'; export interface AutoModerationRule extends ObjectToLower {} @@ -41,7 +41,7 @@ export class AutoModerationRule extends DiscordBase { delete: (ruleId: string, reason?: string) => client.guilds.moderation.delete(guildId, ruleId, reason), fetch: (ruleId: string) => client.guilds.moderation.fetch(guildId, ruleId), edit: (ruleId: string, body: RESTPatchAPIAutoModerationRuleJSONBody, reason?: string) => - client.guilds.moderation.edit(guildId, ruleId, body, reason), + client.guilds.moderation.edit(guildId, ruleId, toCamelCase(body), reason), }; } }