From cf8304ccef5dca9d200b5c0d7552891e9d00a4ad Mon Sep 17 00:00:00 2001 From: Fleny Date: Tue, 4 Mar 2025 14:14:14 +0100 Subject: [PATCH] fix(types)!: Fix optionals for some props in auto moderation. (#4150) * Fix optionals for some props in autoModeration. Also fix some comments to be more clear * Fix type error in automodActionExecution * Fix type error in automod.spec.ts e2e test --------- Co-authored-by: Awesome Stickz --- .../transformers/automodActionExecution.ts | 12 ++++--- packages/bot/src/transformers/types.ts | 2 +- packages/rest/tests/e2e/automod.spec.ts | 4 +-- packages/types/src/discord/autoModeration.ts | 33 +++++++++++++++---- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/packages/bot/src/transformers/automodActionExecution.ts b/packages/bot/src/transformers/automodActionExecution.ts index 232250c61..ec9d1b720 100644 --- a/packages/bot/src/transformers/automodActionExecution.ts +++ b/packages/bot/src/transformers/automodActionExecution.ts @@ -15,11 +15,13 @@ export function transformAutoModerationActionExecution(bot: Bot, payload: Discor matchedContent: payload.matched_content ?? '', action: { type: payload.action.type, - metadata: { - durationSeconds: payload.action.metadata.duration_seconds, - customMessage: payload.action.metadata.custom_message, - channelId: payload.action.metadata.channel_id ? bot.transformers.snowflake(payload.action.metadata.channel_id) : undefined, - }, + metadata: payload.action.metadata + ? { + durationSeconds: payload.action.metadata.duration_seconds, + customMessage: payload.action.metadata.custom_message, + channelId: payload.action.metadata.channel_id ? bot.transformers.snowflake(payload.action.metadata.channel_id) : undefined, + } + : undefined, }, } as AutoModerationActionExecution diff --git a/packages/bot/src/transformers/types.ts b/packages/bot/src/transformers/types.ts index ff1958fef..6af74f74a 100644 --- a/packages/bot/src/transformers/types.ts +++ b/packages/bot/src/transformers/types.ts @@ -333,7 +333,7 @@ export interface AutoModerationActionExecution { export interface AutoModerationAction { type: AutoModerationActionType - metadata: AutoModerationActionMetadata + metadata?: AutoModerationActionMetadata } export interface AutoModerationActionMetadata { diff --git a/packages/rest/tests/e2e/automod.spec.ts b/packages/rest/tests/e2e/automod.spec.ts index 4247ead90..572173743 100644 --- a/packages/rest/tests/e2e/automod.spec.ts +++ b/packages/rest/tests/e2e/automod.spec.ts @@ -228,8 +228,8 @@ describe('Run automod tests', async () => { expect(fetchedRule.actions[0]).to.be.exist expect(fetchedRule.actions[1].metadata).to.be.exist expect(fetchedRule.actions[2].metadata).to.be.exist - expect(fetchedRule.actions[1].metadata.channelId).to.equal(channel.id) - expect(fetchedRule.actions[2].metadata.durationSeconds).to.equal(10) + expect(fetchedRule.actions[1].metadata?.channelId).to.equal(channel.id) + expect(fetchedRule.actions[2].metadata?.durationSeconds).to.equal(10) expect(fetchedRule.actions[0].type).to.equal(AutoModerationActionType.BlockMessage) expect(fetchedRule.actions[1].type).to.equal(AutoModerationActionType.SendAlertMessage) expect(fetchedRule.actions[2].type).to.equal(AutoModerationActionType.Timeout) diff --git a/packages/types/src/discord/autoModeration.ts b/packages/types/src/discord/autoModeration.ts index d83ae6ffe..fc0df3d05 100644 --- a/packages/types/src/discord/autoModeration.ts +++ b/packages/types/src/discord/autoModeration.ts @@ -59,7 +59,7 @@ export interface DiscordAutoModerationRuleTriggerMetadata { * * Can have up to 10 elements in the array and each string can have up to 260 characters */ - regex_patterns: string[] + regex_patterns?: string[] /** * The pre-defined lists of words to match from. * @@ -118,7 +118,7 @@ export interface DiscordAutoModerationAction { /** The type of action to take when a rule is triggered */ type: AutoModerationActionType /** additional metadata needed during execution for this specific action type */ - metadata: DiscordAutoModerationActionMetadata + metadata?: DiscordAutoModerationActionMetadata } /** https://discord.com/developers/docs/resources/auto-moderation#auto-moderation-action-object-action-types */ @@ -142,10 +142,31 @@ export enum AutoModerationActionType { /** https://discord.com/developers/docs/resources/auto-moderation#auto-moderation-action-object-action-metadata */ export interface DiscordAutoModerationActionMetadata { - /** The id of channel to which user content should be logged. Only in ActionType.SendAlertMessage */ + /** + * The id of channel to which user content should be logged. + * + * @remarks + * Only for actions of type {@link AutoModerationActionType.SendAlertMessage}. + */ channel_id?: string - /** Additional explanation that will be shown to members whenever their message is blocked. Maximum of 150 characters. Only supported for AutoModerationActionType.BlockMessage */ - custom_message?: string - /** Timeout duration in seconds maximum of 2419200 seconds (4 weeks). Only supported for TriggerType.Keyword && Only in ActionType.Timeout */ + /** + * Timeout duration in seconds. + * + * @remarks + * Only for actions of type {@link AutoModerationActionType.Timeout}. + * + * Maximum of 2419200 seconds (4 weeks). + */ duration_seconds?: number + /** + * Additional explanation that will be shown to members whenever their message is blocked. + * + * This may set to undefined if no custom message is provided. + * + * @remarks + * Only for actions of type {@link AutoModerationActionType.BlockMessage}. + * + * Maximum of 150 characters. + */ + custom_message?: string }