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

feat(ThreadChannel): Backport creation timestamp #7559

Merged
merged 2 commits into from Mar 2, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/structures/ThreadChannel.js
Expand Up @@ -100,6 +100,11 @@ class ThreadChannel extends Channel {
* @type {?number}
*/
this.archiveTimestamp = new Date(data.thread_metadata.archive_timestamp).getTime();

if ('create_timestamp' in data.thread_metadata) {
// Note: this is needed because we can't assign directly to getters
this._createdTimestamp = Date.parse(data.thread_metadata.create_timestamp);
}
} else {
this.locked ??= null;
this.archived ??= null;
Expand All @@ -108,6 +113,8 @@ class ThreadChannel extends Channel {
this.invitable ??= null;
}

this._createdTimestamp ??= this.type === 'GUILD_PRIVATE_THREAD' ? super.createdTimestamp : null;

if ('owner_id' in data) {
/**
* The id of the member who created this thread
Expand Down Expand Up @@ -176,6 +183,16 @@ class ThreadChannel extends Channel {
if (data.messages) for (const message of data.messages) this.messages._add(message);
}

/**
* The timestamp when this thread was created. This isn't available for threads
* created before 2022-01-09
* @type {?number}
* @readonly
*/
get createdTimestamp() {
return this._createdTimestamp;
}

/**
* A collection of associated guild member objects of this thread's members
* @type {Collection<Snowflake, GuildMember>}
Expand All @@ -196,6 +213,15 @@ class ThreadChannel extends Channel {
return new Date(this.archiveTimestamp);
}

/**
* The time the thread was created at
* @type {?Date}
* @readonly
*/
get createdAt() {
return this.createdTimestamp && new Date(this.createdTimestamp);
}

/**
* The parent channel of this thread
* @type {?(NewsChannel|TextChannel)}
Expand Down Expand Up @@ -490,6 +516,14 @@ class ThreadChannel extends Channel {
return this.archived && (this.locked ? this.manageable : this.sendable);
}

/**
* Whether this thread is a private thread
* @returns {boolean}
*/
isPrivate() {
return this.type === 'GUILD_PRIVATE_THREAD';
}

/**
* Deletes this thread.
* @param {string} [reason] Reason for deleting this thread
Expand Down
15 changes: 12 additions & 3 deletions typings/index.d.ts
Expand Up @@ -516,8 +516,8 @@ export type CategoryChannelResolvable = Snowflake | CategoryChannel;

export abstract class Channel extends Base {
public constructor(client: Client, data?: RawChannelData, immediatePatch?: boolean);
public readonly createdAt: Date;
public readonly createdTimestamp: number;
public readonly createdAt: Date | null;
public readonly createdTimestamp: number | null;
/** @deprecated This will be removed in the next major version, see https://github.com/discordjs/discord.js/issues/7091 */
public deleted: boolean;
public id: Snowflake;
Expand Down Expand Up @@ -1042,7 +1042,8 @@ export abstract class GuildChannel extends Channel {
public constructor(guild: Guild, data?: RawGuildChannelData, client?: Client, immediatePatch?: boolean);
private memberPermissions(member: GuildMember, checkAdmin: boolean): Readonly<Permissions>;
private rolePermissions(role: Role, checkAdmin: boolean): Readonly<Permissions>;

public readonly createdAt: Date;
public readonly createdTimestamp: number;
public readonly calculatedPosition: number;
public readonly deletable: boolean;
public guild: Guild;
Expand Down Expand Up @@ -2304,6 +2305,9 @@ export class ThreadChannel extends TextBasedChannelMixin(Channel) {
public archived: boolean | null;
public readonly archivedAt: Date | null;
public archiveTimestamp: number | null;
public readonly createdAt: Date | null;
private _createdTimestamp: number | null;
public readonly createdTimestamp: number | null;
public autoArchiveDuration: ThreadAutoArchiveDuration | null;
public readonly editable: boolean;
public guild: Guild;
Expand All @@ -2327,6 +2331,11 @@ export class ThreadChannel extends TextBasedChannelMixin(Channel) {
public rateLimitPerUser: number | null;
public type: ThreadChannelTypes;
public readonly unarchivable: boolean;
public isPrivate(): this is this & {
readonly createdTimestamp: number;
readonly createdAt: Date;
type: 'GUILD_PRIVATE_THREAD';
};
public delete(reason?: string): Promise<this>;
public edit(data: ThreadEditData, reason?: string): Promise<ThreadChannel>;
public join(): Promise<ThreadChannel>;
Expand Down