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 #7688

Merged
merged 5 commits into from
Mar 26, 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 packages/discord.js/src/managers/GuildChannelManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const Webhook = require('../structures/Webhook');
const { ThreadChannelTypes } = 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.rest.patch(Routes.channel(channel.id), {
body: {
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 packages/discord.js/src/managers/ThreadManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { ChannelType, Routes } = require('discord-api-types/v10');
const CachedManager = require('./CachedManager');
const { TypeError } = require('../errors');
const ThreadChannel = require('../structures/ThreadChannel');
const { resolveAutoArchiveMaxLimit } = require('../util/Util');

/**
* Manages API methods for {@link ThreadChannel} objects and stores their cache.
Expand Down Expand Up @@ -119,14 +120,8 @@ class ThreadManager extends CachedManager {
} else if (this.channel.type !== ChannelType.GuildNews) {
resolvedType = 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 this.client.rest.post(Routes.threads(this.channel.id, startMessageId), {
body: {
Expand Down
Original file line number Diff line number Diff line change
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 packages/discord.js/src/structures/ThreadChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const TextBasedChannel = require('./interfaces/TextBasedChannel');
const { RangeError } = require('../errors');
const MessageManager = require('../managers/MessageManager');
const ThreadMemberManager = require('../managers/ThreadMemberManager');
const { resolveAutoArchiveMaxLimit } = require('../util/Util');

/**
* Represents a thread channel on Discord.
Expand Down Expand Up @@ -313,14 +314,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.rest.patch(Routes.channel(this.id), {
body: {
name: (data.name ?? this.name).trim(),
Expand Down
13 changes: 12 additions & 1 deletion packages/discord.js/src/util/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { parse } = require('node:path');
const { Collection } = require('@discordjs/collection');
const { ChannelType, RouteBases, Routes } = require('discord-api-types/v10');
const { ChannelType, RouteBases, Routes, GuildFeature } = require('discord-api-types/v10');
const { fetch } = require('undici');
const Colors = require('./Colors');
const { Error: DiscordError, RangeError, TypeError } = require('../errors');
Expand Down Expand Up @@ -561,6 +561,17 @@ class Util extends null {
static cleanCodeBlockContent(text) {
return text.replaceAll('```', '`\u200b``');
}

/**
* 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(GuildFeature.SevenDayThreadArchive)) return 10080;
if (features.includes(GuildFeature.ThreeDayThreadArchive)) return 4320;
return 1440;
}
}

module.exports = Util;
Expand Down
8 changes: 4 additions & 4 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,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 @@ -2393,7 +2393,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 @@ -3672,7 +3672,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 @@ -5139,7 +5139,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