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

types: Fix auto archive duration type (v13) #7787

Merged
merged 1 commit into from Apr 14, 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
6 changes: 5 additions & 1 deletion src/managers/GuildChannelManager.js
Expand Up @@ -12,6 +12,7 @@ const Webhook = require('../structures/Webhook');
const { ThreadChannelTypes, ChannelTypes } = require('../util/Constants');
const DataResolver = require('../util/DataResolver');
const Util = require('../util/Util');
const { resolveAutoArchiveMaxLimit } = require('../util/Util');

let cacheWarningEmitted = false;
let storeChannelDeprecationEmitted = false;
Expand Down Expand Up @@ -261,6 +262,9 @@ class GuildChannelManager extends CachedManager {
}
}

let defaultAutoArchiveDuration = data.defaultAutoArchiveDuration;
if (defaultAutoArchiveDuration === 'MAX') defaultAutoArchiveDuration = resolveAutoArchiveMaxLimit(this.guild);

const newData = await this.client.api.channels(channel.id).patch({
data: {
name: (data.name ?? channel.name).trim(),
Expand All @@ -273,7 +277,7 @@ class GuildChannelManager extends CachedManager {
parent_id: parent,
lock_permissions: data.lockPermissions,
rate_limit_per_user: data.rateLimitPerUser,
default_auto_archive_duration: data.defaultAutoArchiveDuration,
default_auto_archive_duration: defaultAutoArchiveDuration,
permission_overwrites,
},
reason,
Expand Down
11 changes: 3 additions & 8 deletions src/managers/ThreadManager.js
Expand Up @@ -5,6 +5,7 @@ const CachedManager = require('./CachedManager');
const { TypeError } = require('../errors');
const ThreadChannel = require('../structures/ThreadChannel');
const { ChannelTypes } = require('../util/Constants');
const { resolveAutoArchiveMaxLimit } = require('../util/Util');

/**
* Manages API methods for {@link ThreadChannel} objects and stores their cache.
Expand Down Expand Up @@ -120,14 +121,8 @@ class ThreadManager extends CachedManager {
} else if (this.channel.type !== 'GUILD_NEWS') {
resolvedType = typeof type === 'string' ? ChannelTypes[type] : type ?? resolvedType;
}
if (autoArchiveDuration === 'MAX') {
autoArchiveDuration = 1440;
if (this.channel.guild.features.includes('SEVEN_DAY_THREAD_ARCHIVE')) {
autoArchiveDuration = 10080;
} else if (this.channel.guild.features.includes('THREE_DAY_THREAD_ARCHIVE')) {
autoArchiveDuration = 4320;
}
}

if (autoArchiveDuration === 'MAX') autoArchiveDuration = resolveAutoArchiveMaxLimit(this.channel.guild);

const data = await path.threads.post({
data: {
Expand Down
2 changes: 1 addition & 1 deletion src/structures/BaseGuildTextChannel.js
Expand Up @@ -69,7 +69,7 @@ class BaseGuildTextChannel extends GuildChannel {
if ('default_auto_archive_duration' in data) {
/**
* The default auto archive duration for newly created threads in this channel
* @type {?ThreadAutoArchiveDuration}
* @type {?number}
*/
this.defaultAutoArchiveDuration = data.default_auto_archive_duration;
}
Expand Down
11 changes: 3 additions & 8 deletions src/structures/ThreadChannel.js
Expand Up @@ -6,6 +6,7 @@ const { RangeError } = require('../errors');
const MessageManager = require('../managers/MessageManager');
const ThreadMemberManager = require('../managers/ThreadMemberManager');
const Permissions = require('../util/Permissions');
const { resolveAutoArchiveMaxLimit } = require('../util/Util');

/**
* Represents a thread channel on Discord.
Expand Down Expand Up @@ -314,14 +315,8 @@ class ThreadChannel extends Channel {
*/
async edit(data, reason) {
let autoArchiveDuration = data.autoArchiveDuration;
if (data.autoArchiveDuration === 'MAX') {
autoArchiveDuration = 1440;
if (this.guild.features.includes('SEVEN_DAY_THREAD_ARCHIVE')) {
autoArchiveDuration = 10080;
} else if (this.guild.features.includes('THREE_DAY_THREAD_ARCHIVE')) {
autoArchiveDuration = 4320;
}
}
if (autoArchiveDuration === 'MAX') autoArchiveDuration = resolveAutoArchiveMaxLimit(this.guild);

const newData = await this.client.api.channels(this.id).patch({
data: {
name: (data.name ?? this.name).trim(),
Expand Down
11 changes: 11 additions & 0 deletions src/util/Util.js
Expand Up @@ -603,6 +603,17 @@ class Util extends null {
filter.isDefault = true;
return filter;
}

/**
* Resolves the maximum time a guild's thread channels should automatcally archive in case of no recent activity.
* @param {Guild} guild The guild to resolve this limit from.
* @returns {number}
*/
static resolveAutoArchiveMaxLimit({ features }) {
if (features.includes('SEVEN_DAY_THREAD_ARCHIVE')) return 10080;
if (features.includes('THREE_DAY_THREAD_ARCHIVE')) return 4320;
return 1440;
}
}

module.exports = Util;
Expand Down
9 changes: 5 additions & 4 deletions typings/index.d.ts
Expand Up @@ -430,7 +430,7 @@ export class BaseGuildTextChannel extends TextBasedChannelMixin(GuildChannel) {
public createWebhook(name: string, options?: ChannelWebhookCreateOptions): Promise<Webhook>;
public fetchInvites(cache?: boolean): Promise<Collection<string, Invite>>;
public setDefaultAutoArchiveDuration(
defaultAutoArchiveDuration: ThreadAutoArchiveDuration,
defaultAutoArchiveDuration: ThreadAutoArchiveDuration | 'MAX',
reason?: string,
): Promise<this>;
public setNSFW(nsfw?: boolean, reason?: string): Promise<this>;
Expand Down Expand Up @@ -2484,7 +2484,7 @@ export class ThreadChannel extends TextBasedChannelMixin(Channel) {
public fetchStarterMessage(options?: BaseFetchOptions): Promise<Message>;
public setArchived(archived?: boolean, reason?: string): Promise<ThreadChannel>;
public setAutoArchiveDuration(
autoArchiveDuration: ThreadAutoArchiveDuration,
autoArchiveDuration: ThreadAutoArchiveDuration | 'MAX',
reason?: string,
): Promise<ThreadChannel>;
public setInvitable(invitable?: boolean, reason?: string): Promise<ThreadChannel>;
Expand Down Expand Up @@ -2610,6 +2610,7 @@ export class Util extends null {
reason?: string,
): Promise<{ id: Snowflake; position: number }[]>;
public static splitMessage(text: string, options?: SplitOptions): string[];
public static resolveAutoArchiveMaxLimit(guild: Guild): number;
}

export class Formatters extends null {
Expand Down Expand Up @@ -4046,7 +4047,7 @@ export interface ChannelData {
rateLimitPerUser?: number;
lockPermissions?: boolean;
permissionOverwrites?: readonly OverwriteResolvable[] | Collection<Snowflake, OverwriteResolvable>;
defaultAutoArchiveDuration?: ThreadAutoArchiveDuration;
defaultAutoArchiveDuration?: ThreadAutoArchiveDuration | 'MAX';
rtcRegion?: string | null;
}

Expand Down Expand Up @@ -5837,7 +5838,7 @@ export type TextChannelResolvable = Snowflake | TextChannel;

export type TextBasedChannelResolvable = Snowflake | TextBasedChannel;

export type ThreadAutoArchiveDuration = 60 | 1440 | 4320 | 10080 | 'MAX';
export type ThreadAutoArchiveDuration = 60 | 1440 | 4320 | 10080;

export type ThreadChannelResolvable = ThreadChannel | Snowflake;

Expand Down