Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(GuildScheduledEvent): Use if...else pattern and handle partials #9802

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 77 additions & 32 deletions packages/discord.js/src/structures/GuildScheduledEvent.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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