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(ThreadChannel): better property handling #6172

Merged
merged 3 commits into from Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions src/managers/ChannelManager.js
Expand Up @@ -19,18 +19,18 @@ class ChannelManager extends CachedManager {
* @name ChannelManager#cache
*/

_add(data, guild, cache = true, allowUnknownGuild = false) {
_add(data, guild, cache = true, allowUnknownGuild = false, fromInteraction = false) {
ckohen marked this conversation as resolved.
Show resolved Hide resolved
const existing = this.cache.get(data.id);
if (existing) {
if (cache) existing._patch(data);
if (cache) existing._patch(data, fromInteraction);
guild?.channels?._add(existing);
if (ThreadChannelTypes.includes(existing.type)) {
existing.parent?.threads?._add(existing);
}
return existing;
}

const channel = Channel.create(this.client, data, guild, allowUnknownGuild);
const channel = Channel.create(this.client, data, guild, allowUnknownGuild, fromInteraction);

if (!channel) {
this.client.emit(Events.DEBUG, `Failed to find guild, or unknown type for channel ${data.id} ${data.type}`);
Expand Down
4 changes: 2 additions & 2 deletions src/structures/Channel.js
Expand Up @@ -126,7 +126,7 @@ class Channel extends Base {
return ThreadChannelTypes.includes(this.type);
}

static create(client, data, guild, allowUnknownGuild) {
static create(client, data, guild, allowUnknownGuild, fromInteraction) {
if (!CategoryChannel) CategoryChannel = require('./CategoryChannel');
if (!DMChannel) DMChannel = require('./DMChannel');
if (!NewsChannel) NewsChannel = require('./NewsChannel');
Expand Down Expand Up @@ -176,7 +176,7 @@ class Channel extends Base {
case ChannelTypes.GUILD_NEWS_THREAD:
case ChannelTypes.GUILD_PUBLIC_THREAD:
case ChannelTypes.GUILD_PRIVATE_THREAD: {
channel = new ThreadChannel(guild, data, client);
channel = new ThreadChannel(guild, data, client, fromInteraction);
if (!allowUnknownGuild) channel.parent?.threads.cache.set(channel.id, channel);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/structures/CommandInteraction.js
Expand Up @@ -121,7 +121,7 @@ class CommandInteraction extends Interaction {
if (member) result.member = this.guild?.members._add({ user, ...member }) ?? member;

const channel = resolved.channels?.[option.value];
if (channel) result.channel = this.client.channels._add(channel, this.guild) ?? channel;
if (channel) result.channel = this.client.channels._add(channel, this.guild, true, false, true) ?? channel;

const role = resolved.roles?.[option.value];
if (role) result.role = this.guild?.roles._add(role) ?? role;
Expand Down
28 changes: 15 additions & 13 deletions src/structures/ThreadChannel.js
Expand Up @@ -16,8 +16,9 @@ class ThreadChannel extends Channel {
* @param {Guild} guild The guild the thread channel is part of
* @param {APIChannel} data The data for the thread channel
* @param {Client} [client] A safety parameter for the client that instantiated this
* @param {boolean} [fromInteraction=false] Whether the data was from an interaction (partial)
*/
constructor(guild, data, client) {
constructor(guild, data, client, fromInteraction = false) {
super(guild?.client ?? client, data, false);

/**
Expand All @@ -43,10 +44,10 @@ class ThreadChannel extends Channel {
* @type {ThreadMemberManager}
*/
this.members = new ThreadMemberManager(this);
if (data) this._patch(data);
if (data) this._patch(data, fromInteraction);
}

_patch(data) {
_patch(data, partial = false) {
super._patch(data);

/**
Expand All @@ -62,35 +63,35 @@ class ThreadChannel extends Channel {
if ('parent_id' in data) {
/**
* The id of the parent channel of this thread
* @type {Snowflake}
* @type {?Snowflake}
*/
this.parentId = data.parent_id;
}

if ('thread_metadata' in data) {
/**
* Whether the thread is locked
* @type {boolean}
* @type {?boolean}
ckohen marked this conversation as resolved.
Show resolved Hide resolved
*/
this.locked = data.thread_metadata.locked ?? false;

/**
* Whether the thread is archived
* @type {boolean}
* @type {?boolean}
*/
this.archived = data.thread_metadata.archived;

/**
* The amount of time (in minutes) after which the thread will automatically archive in case of no recent activity
* @type {number}
* @type {?number}
*/
this.autoArchiveDuration = data.thread_metadata.auto_archive_duration;

/**
* The timestamp when the thread's archive status was last changed
* <info>If the thread was never archived or unarchived, this is the timestamp at which the thread was
* created</info>
* @type {number}
* @type {?number}
*/
this.archiveTimestamp = new Date(data.thread_metadata.archive_timestamp).getTime();
}
Expand Down Expand Up @@ -119,10 +120,10 @@ class ThreadChannel extends Channel {
this.lastPinTimestamp = data.last_pin_timestamp ? new Date(data.last_pin_timestamp).getTime() : null;
}

if ('rate_limit_per_user' in data) {
if ('rate_limit_per_user' in data || !partial) {
/**
* The ratelimit per user for this thread (in seconds)
* @type {number}
* @type {?number}
*/
this.rateLimitPerUser = data.rate_limit_per_user ?? 0;
}
Expand All @@ -132,7 +133,7 @@ class ThreadChannel extends Channel {
* The approximate count of messages in this thread
* <info>This stops counting at 50. If you need an approximate value higher than that, use
* `ThreadChannel#messages.cache.size`</info>
* @type {number}
* @type {?number}
*/
this.messageCount = data.message_count;
}
Expand All @@ -142,7 +143,7 @@ class ThreadChannel extends Channel {
* The approximate count of users in this thread
* <info>This stops counting at 50. If you need an approximate value higher than that, use
* `ThreadChannel#members.cache.size`</info>
* @type {number}
* @type {?number}
*/
this.memberCount = data.member_count;
}
Expand All @@ -163,10 +164,11 @@ class ThreadChannel extends Channel {
/**
* The time at which this thread's archive status was last changed
* <info>If the thread was never archived or unarchived, this is the time at which the thread was created</info>
* @type {Date}
* @type {?Date}
* @readonly
*/
get archivedAt() {
if (!this.archiveTimestamp) return null;
return new Date(this.archiveTimestamp);
}

Expand Down
22 changes: 11 additions & 11 deletions typings/index.d.ts
Expand Up @@ -1660,29 +1660,29 @@ export class TextChannel extends TextBasedChannel(GuildChannel) {
}

export class ThreadChannel extends TextBasedChannel(Channel) {
public constructor(guild: Guild, data?: object, client?: Client);
public archived: boolean;
public readonly archivedAt: Date;
public archiveTimestamp: number;
public autoArchiveDuration: ThreadAutoArchiveDuration;
public constructor(guild: Guild, data?: object, client?: Client, fromInteraction?: boolean);
public archived?: boolean;
public readonly archivedAt: Date | null;
public archiveTimestamp?: number;
public autoArchiveDuration?: ThreadAutoArchiveDuration;
public readonly editable: boolean;
public guild: Guild;
public guildId: Snowflake;
public readonly guildMembers: Collection<Snowflake, GuildMember>;
public readonly joinable: boolean;
public readonly joined: boolean;
public locked: boolean;
public locked?: boolean;
public readonly manageable: boolean;
public readonly sendable: boolean;
public memberCount: number | null;
public messageCount: number | null;
public memberCount?: number | null;
public messageCount?: number | null;
public messages: MessageManager;
public members: ThreadMemberManager;
public name: string;
public ownerId: Snowflake;
public ownerId?: Snowflake;
public readonly parent: TextChannel | NewsChannel | null;
public parentId: Snowflake;
public rateLimitPerUser: number;
public parentId?: Snowflake;
public rateLimitPerUser?: number;
public type: ThreadChannelTypes;
public readonly unarchivable: boolean;
public delete(reason?: string): Promise<ThreadChannel>;
Expand Down