Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(GuildScheduledEvent): Use if...else pattern and handle partials (
…#9802)

fix: handle partial case

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
Jiralite and kodiakhq[bot] committed Sep 18, 2023
1 parent 4588e07 commit 32d614c
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 37 deletions.
109 changes: 77 additions & 32 deletions packages/discord.js/src/structures/GuildScheduledEvent.js
Expand Up @@ -50,11 +50,16 @@ class GuildScheduledEvent extends Base {
this.creatorId ??= null;
}

/**
* The name of the guild scheduled event
* @type {string}
*/
this.name = data.name;
if ('name' in data) {
/**
* The name of the guild scheduled event
* @type {?string}
*/
this.name = data.name;
} else {
// Only if partial.
this.name ??= null;
}

if ('description' in data) {
/**
Expand All @@ -66,37 +71,59 @@ class GuildScheduledEvent extends Base {
this.description ??= null;
}

/**
* The timestamp the guild scheduled event will start at
* <info>This can be potentially `null` only when it's an {@link AuditLogEntryTarget}</info>
* @type {?number}
*/
this.scheduledStartTimestamp = data.scheduled_start_time ? Date.parse(data.scheduled_start_time) : null;
if ('scheduled_start_time' in data) {
/**
* The timestamp the guild scheduled event will start at
* @type {?number}
*/
this.scheduledStartTimestamp = Date.parse(data.scheduled_start_time);
} else {
this.scheduledStartTimestamp ??= null;
}

/**
* The timestamp the guild scheduled event will end at,
* or `null` if the event does not have a scheduled time to end
* @type {?number}
*/
this.scheduledEndTimestamp = data.scheduled_end_time ? Date.parse(data.scheduled_end_time) : null;
if ('scheduled_end_time' in data) {
/**
* The timestamp the guild scheduled event will end at
* or `null` if the event does not have a scheduled time to end
* @type {?number}
*/
this.scheduledEndTimestamp = data.scheduled_end_time ? Date.parse(data.scheduled_end_time) : null;
} else {
this.scheduledEndTimestamp ??= null;
}

/**
* The privacy level of the guild scheduled event
* @type {GuildScheduledEventPrivacyLevel}
*/
this.privacyLevel = data.privacy_level;
if ('privacy_level' in data) {
/**
* The privacy level of the guild scheduled event
* @type {?GuildScheduledEventPrivacyLevel}
*/
this.privacyLevel = data.privacy_level;
} else {
// Only if partial.
this.privacyLevel ??= null;
}

/**
* The status of the guild scheduled event
* @type {GuildScheduledEventStatus}
*/
this.status = data.status;
if ('status' in data) {
/**
* The status of the guild scheduled event
* @type {?GuildScheduledEventStatus}
*/
this.status = data.status;
} else {
// Only if partial.
this.status ??= null;
}

/**
* The type of hosting entity associated with the scheduled event
* @type {GuildScheduledEventEntityType}
*/
this.entityType = data.entity_type;
if ('entity_type' in data) {
/**
* The type of hosting entity associated with the scheduled event
* @type {?GuildScheduledEventEntityType}
*/
this.entityType = data.entity_type;
} else {
// Only if partial.
this.entityType ??= null;
}

if ('entity_id' in data) {
/**
Expand Down Expand Up @@ -164,6 +191,15 @@ class GuildScheduledEvent extends Base {
}
}

/**
* Whether this guild scheduled event is partial.
* @type {boolean}
* @readonly
*/
get partial() {
return this.name === null;
}

/**
* The URL of this scheduled event's cover image
* @param {BaseImageURLOptions} [options={}] Options for image URL
Expand Down Expand Up @@ -276,6 +312,15 @@ class GuildScheduledEvent extends Base {
return this.guild.scheduledEvents.edit(this.id, options);
}

/**
* Fetches this guild scheduled event.
* @param {boolean} [force=true] Whether to skip the cache check and request the API
* @returns {Promise<GuildScheduledEvent>}
*/
fetch(force = true) {
return this.guild.scheduledEvents.fetch({ guildScheduledEvent: this.id, force });
}

/**
* Deletes this guild scheduled event.
* @returns {Promise<GuildScheduledEvent>}
Expand Down
22 changes: 17 additions & 5 deletions packages/discord.js/typings/index.d.ts
Expand Up @@ -1670,11 +1670,13 @@ export class GuildScheduledEvent<S extends GuildScheduledEventStatus = GuildSche
public get guild(): Guild | null;
public get url(): string;
public image: string | null;
public get partial(): false;
public coverImageURL(options?: Readonly<BaseImageURLOptions>): string | null;
public createInviteURL(options?: GuildScheduledEventInviteURLCreateOptions): Promise<string>;
public edit<T extends GuildScheduledEventSetStatusArg<S>>(
options: GuildScheduledEventEditOptions<S, T>,
): Promise<GuildScheduledEvent<T>>;
public fetch(force?: boolean): Promise<GuildScheduledEvent<S>>;
public delete(): Promise<GuildScheduledEvent<S>>;
public setName(name: string, reason?: string): Promise<GuildScheduledEvent<S>>;
public setScheduledStartTime(scheduledStartTime: DateResolvable, reason?: string): Promise<GuildScheduledEvent<S>>;
Expand Down Expand Up @@ -4359,7 +4361,14 @@ export interface AddGuildMemberOptions {
fetchWhenExisting?: boolean;
}

export type AllowedPartial = User | Channel | GuildMember | Message | MessageReaction | ThreadMember;
export type AllowedPartial =
| User
| Channel
| GuildMember
| Message
| MessageReaction
| GuildScheduledEvent
| ThreadMember;

export type AllowedThreadTypeForNewsChannel = ChannelType.AnnouncementThread;

Expand Down Expand Up @@ -4929,12 +4938,12 @@ export interface ClientEvents {
stickerUpdate: [oldSticker: Sticker, newSticker: Sticker];
guildScheduledEventCreate: [guildScheduledEvent: GuildScheduledEvent];
guildScheduledEventUpdate: [
oldGuildScheduledEvent: GuildScheduledEvent | null,
oldGuildScheduledEvent: GuildScheduledEvent | PartialGuildScheduledEvent | null,
newGuildScheduledEvent: GuildScheduledEvent,
];
guildScheduledEventDelete: [guildScheduledEvent: GuildScheduledEvent];
guildScheduledEventUserAdd: [guildScheduledEvent: GuildScheduledEvent, user: User];
guildScheduledEventUserRemove: [guildScheduledEvent: GuildScheduledEvent, user: User];
guildScheduledEventDelete: [guildScheduledEvent: GuildScheduledEvent | PartialGuildScheduledEvent];
guildScheduledEventUserAdd: [guildScheduledEvent: GuildScheduledEvent | PartialGuildScheduledEvent, user: User];
guildScheduledEventUserRemove: [guildScheduledEvent: GuildScheduledEvent | PartialGuildScheduledEvent, user: User];
}

export interface ClientFetchInviteOptions {
Expand Down Expand Up @@ -6149,6 +6158,9 @@ export interface PartialMessage

export interface PartialMessageReaction extends Partialize<MessageReaction, 'count'> {}

export interface PartialGuildScheduledEvent
extends Partialize<GuildScheduledEvent, 'userCount', 'status' | 'privacyLevel' | 'name' | 'entityType'> {}

export interface PartialThreadMember extends Partialize<ThreadMember, 'flags' | 'joinedAt' | 'joinedTimestamp'> {}

export interface PartialOverwriteData {
Expand Down

0 comments on commit 32d614c

Please sign in to comment.