From 0af5d55ef8c6567d706f664a8e2899ce0cd589da Mon Sep 17 00:00:00 2001 From: Vlad Frangu Date: Sun, 5 Jan 2025 23:33:00 +0200 Subject: [PATCH 1/4] types: fix recurrence rule types --- packages/discord.js/typings/index.d.ts | 39 ++++++--- packages/discord.js/typings/index.test-d.ts | 90 +++++++++++++++++++++ 2 files changed, 119 insertions(+), 10 deletions(-) diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 0224e4b22aaf..cae5909bd162 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -6406,18 +6406,37 @@ export interface GuildScheduledEventCreateOptions { recurrenceRule?: GuildScheduledEventRecurrenceRuleOptions; } -export interface GuildScheduledEventRecurrenceRuleOptions { +export type GuildScheduledEventRecurrenceRuleOptions = + | BaseGuildScheduledEventRecurrenceRuleOptions< + GuildScheduledEventRecurrenceRuleFrequency.Yearly, + { + byMonth: readonly GuildScheduledEventRecurrenceRuleMonth[]; + byMonthDay: readonly number[]; + } + > + | BaseGuildScheduledEventRecurrenceRuleOptions< + GuildScheduledEventRecurrenceRuleFrequency.Monthly, + { + byNWeekday: readonly GuildScheduledEventRecurrenceRuleNWeekday[]; + } + > + | BaseGuildScheduledEventRecurrenceRuleOptions< + GuildScheduledEventRecurrenceRuleFrequency.Weekly | GuildScheduledEventRecurrenceRuleFrequency.Daily, + { + byWeekday: readonly GuildScheduledEventRecurrenceRuleWeekday[]; + } + >; + +type BaseGuildScheduledEventRecurrenceRuleOptions< + Frequency extends GuildScheduledEventRecurrenceRuleFrequency, + Extra extends {}, +> = { startAt: DateResolvable; - endAt: DateResolvable; - frequency: GuildScheduledEventRecurrenceRuleFrequency; + endsAt?: DateResolvable; + count?: number; interval: number; - byWeekday: readonly GuildScheduledEventRecurrenceRuleWeekday[]; - byNWeekday: readonly GuildScheduledEventRecurrenceRuleNWeekday[]; - byMonth: readonly GuildScheduledEventRecurrenceRuleMonth[]; - byMonthDay: readonly number[]; - byYearDay: readonly number[]; - count: number; -} + frequency: Frequency; +} & Extra; export interface GuildScheduledEventEditOptions< Status extends GuildScheduledEventStatus, diff --git a/packages/discord.js/typings/index.test-d.ts b/packages/discord.js/typings/index.test-d.ts index c21271cf664d..a88f8be66bed 100644 --- a/packages/discord.js/typings/index.test-d.ts +++ b/packages/discord.js/typings/index.test-d.ts @@ -33,6 +33,9 @@ import { APIMentionableSelectComponent, APIModalInteractionResponseCallbackData, WebhookType, + GuildScheduledEventRecurrenceRuleFrequency, + GuildScheduledEventRecurrenceRuleMonth, + GuildScheduledEventRecurrenceRuleWeekday, } from 'discord-api-types/v10'; import { ApplicationCommand, @@ -214,6 +217,7 @@ import { PollData, UserManager, InteractionCallbackResponse, + GuildScheduledEventRecurrenceRuleOptions, } from '.'; import { expectAssignable, @@ -2679,3 +2683,89 @@ client.on('interactionCreate', interaction => { declare const guildScheduledEventManager: GuildScheduledEventManager; await guildScheduledEventManager.edit(snowflake, { recurrenceRule: null }); + +{ + expectNotAssignable({ + startAt: new Date(), + frequency: GuildScheduledEventRecurrenceRuleFrequency.Yearly, + interval: 1, + byMonth: [GuildScheduledEventRecurrenceRuleMonth.May], + byMonthDay: [4], + // Invalid property + byWeekday: [GuildScheduledEventRecurrenceRuleWeekday.Monday], + }); + + expectNotAssignable({ + startAt: new Date(), + frequency: GuildScheduledEventRecurrenceRuleFrequency.Yearly, + interval: 1, + byMonth: [GuildScheduledEventRecurrenceRuleMonth.May], + byMonthDay: [4], + // Invalid property + byNWeekday: [{ n: 1, day: GuildScheduledEventRecurrenceRuleWeekday.Monday }], + }); + + expectAssignable({ + startAt: new Date(), + frequency: GuildScheduledEventRecurrenceRuleFrequency.Yearly, + interval: 1, + byMonth: [GuildScheduledEventRecurrenceRuleMonth.May], + byMonthDay: [4], + }); +} + +{ + expectAssignable({ + startAt: new Date(), + frequency: GuildScheduledEventRecurrenceRuleFrequency.Monthly, + interval: 1, + byNWeekday: [{ n: 1, day: GuildScheduledEventRecurrenceRuleWeekday.Monday }], + }); + + expectNotAssignable({ + startAt: new Date(), + frequency: GuildScheduledEventRecurrenceRuleFrequency.Monthly, + interval: 1, + byNWeekday: [{ n: 1, day: GuildScheduledEventRecurrenceRuleWeekday.Monday }], + // Invalid property + byWeekday: [GuildScheduledEventRecurrenceRuleWeekday.Monday], + }); +} + +{ + expectAssignable({ + startAt: new Date(), + frequency: GuildScheduledEventRecurrenceRuleFrequency.Weekly, + interval: 1, + byWeekday: [GuildScheduledEventRecurrenceRuleWeekday.Monday], + }); + + expectNotAssignable({ + startAt: new Date(), + frequency: GuildScheduledEventRecurrenceRuleFrequency.Weekly, + interval: 1, + byWeekday: [GuildScheduledEventRecurrenceRuleWeekday.Monday], + // Invalid property + byNWeekday: [{ n: 1, day: GuildScheduledEventRecurrenceRuleWeekday.Monday }], + }); +} + +{ + expectNotAssignable({ + startAt: new Date(), + frequency: GuildScheduledEventRecurrenceRuleFrequency.Daily, + interval: 1, + byWeekday: [GuildScheduledEventRecurrenceRuleWeekday.Monday], + // Invalid property + byNWeekday: [{ n: 1, day: GuildScheduledEventRecurrenceRuleWeekday.Monday }], + }); + + expectNotAssignable({ + startAt: new Date(), + frequency: GuildScheduledEventRecurrenceRuleFrequency.Daily, + interval: 1, + byWeekday: [GuildScheduledEventRecurrenceRuleWeekday.Monday], + // Invalid property + byMonth: [GuildScheduledEventRecurrenceRuleMonth.May], + }); +} From 11080ac0fa9b379309774e8e98d8042b7f9101d5 Mon Sep 17 00:00:00 2001 From: Vlad Frangu Date: Mon, 6 Jan 2025 00:56:13 +0200 Subject: [PATCH 2/4] fix: endAt not endsAt --- packages/discord.js/typings/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index cae5909bd162..450fa0a42bb1 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -6432,7 +6432,7 @@ type BaseGuildScheduledEventRecurrenceRuleOptions< Extra extends {}, > = { startAt: DateResolvable; - endsAt?: DateResolvable; + endAt?: DateResolvable; count?: number; interval: number; frequency: Frequency; From f1a3ebfbeee2eee7e9a98a1c8db537341afaba98 Mon Sep 17 00:00:00 2001 From: Vlad Frangu Date: Sun, 19 Jan 2025 00:56:00 +0200 Subject: [PATCH 3/4] types: remove fields that cannot be set by the client --- packages/discord.js/typings/index.d.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 6f802a8be148..5e0e71abbf95 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -6398,8 +6398,6 @@ type BaseGuildScheduledEventRecurrenceRuleOptions< Extra extends {}, > = { startAt: DateResolvable; - endAt?: DateResolvable; - count?: number; interval: number; frequency: Frequency; } & Extra; From f172e1ee3743374af0596f0d8c23560b8e62fbba Mon Sep 17 00:00:00 2001 From: Vlad Frangu Date: Sun, 19 Jan 2025 01:43:01 +0200 Subject: [PATCH 4/4] chore: cleanup JS lands too --- packages/discord.js/src/managers/GuildScheduledEventManager.js | 2 -- packages/discord.js/src/util/Transformers.js | 3 --- 2 files changed, 5 deletions(-) diff --git a/packages/discord.js/src/managers/GuildScheduledEventManager.js b/packages/discord.js/src/managers/GuildScheduledEventManager.js index 02e14c06259b..ccfa03c974c7 100644 --- a/packages/discord.js/src/managers/GuildScheduledEventManager.js +++ b/packages/discord.js/src/managers/GuildScheduledEventManager.js @@ -41,7 +41,6 @@ class GuildScheduledEventManager extends CachedManager { * Options for setting a recurrence rule for a guild scheduled event. * @typedef {Object} GuildScheduledEventRecurrenceRuleOptions * @property {DateResolvable} startAt The time the recurrence rule interval starts at - * @property {?DateResolvable} endAt The time the recurrence rule interval ends at * @property {GuildScheduledEventRecurrenceRuleFrequency} frequency How often the event occurs * @property {number} interval The spacing between the events * @property {?GuildScheduledEventRecurrenceRuleWeekday[]} byWeekday The days within a week to recur on @@ -49,7 +48,6 @@ class GuildScheduledEventManager extends CachedManager { * @property {?GuildScheduledEventRecurrenceRuleMonth[]} byMonth The months to recur on * @property {?number[]} byMonthDay The days within a month to recur on * @property {?number[]} byYearDay The days within a year to recur on - * @property {?number} count The total amount of times the event is allowed to recur before stopping */ /** diff --git a/packages/discord.js/src/util/Transformers.js b/packages/discord.js/src/util/Transformers.js index e37fb456c65a..29ba224a9496 100644 --- a/packages/discord.js/src/util/Transformers.js +++ b/packages/discord.js/src/util/Transformers.js @@ -63,8 +63,6 @@ function _transformAPIMessageInteractionMetadata(client, messageInteractionMetad function _transformGuildScheduledEventRecurrenceRule(recurrenceRule) { return { start: new Date(recurrenceRule.startAt).toISOString(), - // eslint-disable-next-line eqeqeq - end: recurrenceRule.endAt != null ? new Date(recurrenceRule.endAt).toISOString() : recurrenceRule.endAt, frequency: recurrenceRule.frequency, interval: recurrenceRule.interval, by_weekday: recurrenceRule.byWeekday, @@ -72,7 +70,6 @@ function _transformGuildScheduledEventRecurrenceRule(recurrenceRule) { by_month: recurrenceRule.byMonth, by_month_day: recurrenceRule.byMonthDay, by_year_day: recurrenceRule.byYearDay, - count: recurrenceRule.count, }; }