-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: backport guild forum support to v13 (#8651)
Co-authored-by: Jaworek <jaworekwiadomosci@gmail.com> Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com>
- Loading branch information
1 parent
56e6718
commit 546ac43
Showing
24 changed files
with
903 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
'use strict'; | ||
|
||
const ThreadManager = require('./ThreadManager'); | ||
const { TypeError } = require('../errors'); | ||
const MessagePayload = require('../structures/MessagePayload'); | ||
const { resolveAutoArchiveMaxLimit } = require('../util/Util'); | ||
|
||
/** | ||
* Manages API methods for threads in forum channels and stores their cache. | ||
* @extends {ThreadManager} | ||
*/ | ||
class GuildForumThreadManager extends ThreadManager { | ||
/** | ||
* The channel this Manager belongs to | ||
* @name GuildForumThreadManager#channel | ||
* @type {ForumChannel} | ||
*/ | ||
|
||
/** | ||
* @typedef {BaseMessageOptions} GuildForumThreadMessageCreateOptions | ||
* @property {StickerResolvable} [stickers] The stickers to send with the message | ||
* @property {BitFieldResolvable} [flags] The flags to send with the message | ||
*/ | ||
|
||
/** | ||
* Options for creating a thread. | ||
* @typedef {StartThreadOptions} GuildForumThreadCreateOptions | ||
* @property {GuildForumThreadMessageCreateOptions|MessagePayload} message The message associated with the thread post | ||
* @property {Snowflake[]} [appliedTags] The tags to apply to the thread | ||
*/ | ||
|
||
/** | ||
* Creates a new thread in the channel. | ||
* @param {GuildForumThreadCreateOptions} [options] Options to create a new thread | ||
* @returns {Promise<ThreadChannel>} | ||
* @example | ||
* // Create a new forum post | ||
* forum.threads | ||
* .create({ | ||
* name: 'Food Talk', | ||
* autoArchiveDuration: 60, | ||
* message: { | ||
* content: 'Discuss your favorite food!', | ||
* }, | ||
* reason: 'Needed a separate thread for food', | ||
* }) | ||
* .then(threadChannel => console.log(threadChannel)) | ||
* .catch(console.error); | ||
*/ | ||
async create({ | ||
name, | ||
autoArchiveDuration = this.channel.defaultAutoArchiveDuration, | ||
message, | ||
reason, | ||
rateLimitPerUser, | ||
appliedTags, | ||
} = {}) { | ||
if (!message) { | ||
throw new TypeError('GUILD_FORUM_MESSAGE_REQUIRED'); | ||
} | ||
|
||
let messagePayload; | ||
|
||
if (message instanceof MessagePayload) { | ||
messagePayload = message.resolveData(); | ||
} else { | ||
messagePayload = MessagePayload.create(this, message).resolveData(); | ||
} | ||
|
||
const { data: body, files } = await messagePayload.resolveFiles(); | ||
|
||
if (autoArchiveDuration === 'MAX') autoArchiveDuration = resolveAutoArchiveMaxLimit(this.channel.guild); | ||
|
||
const data = await this.client.api.channels(this.channel.id).threads.post({ | ||
data: { | ||
name, | ||
auto_archive_duration: autoArchiveDuration, | ||
rate_limit_per_user: rateLimitPerUser, | ||
applied_tags: appliedTags, | ||
message: body, | ||
}, | ||
files, | ||
reason, | ||
}); | ||
|
||
return this.client.actions.ThreadCreate.handle(data).thread; | ||
} | ||
} | ||
|
||
module.exports = GuildForumThreadManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
'use strict'; | ||
|
||
const ThreadManager = require('./ThreadManager'); | ||
const { TypeError } = require('../errors'); | ||
const { ChannelTypes } = require('../util/Constants'); | ||
const { resolveAutoArchiveMaxLimit } = require('../util/Util'); | ||
|
||
/** | ||
* Manages API methods for {@link ThreadChannel} objects and stores their cache. | ||
* @extends {ThreadManager} | ||
*/ | ||
class GuildTextThreadManager extends ThreadManager { | ||
/** | ||
* The channel this Manager belongs to | ||
* @name GuildTextThreadManager#channel | ||
* @type {TextChannel|NewsChannel} | ||
*/ | ||
|
||
/** | ||
* Options for creating a thread. <warn>Only one of `startMessage` or `type` can be defined.</warn> | ||
* @typedef {StartThreadOptions} GuildTextThreadCreateOptions | ||
* @property {MessageResolvable} [startMessage] The message to start a thread from. <warn>If this is defined then type | ||
* of thread gets automatically defined and cannot be changed. The provided `type` field will be ignored</warn> | ||
* @property {ThreadChannelTypes|number} [type] The type of thread to create. Defaults to `GUILD_PUBLIC_THREAD` if | ||
* created in a {@link TextChannel} <warn>When creating threads in a {@link NewsChannel} this is ignored and is always | ||
* `GUILD_NEWS_THREAD`</warn> | ||
* @property {boolean} [invitable] Whether non-moderators can add other non-moderators to the thread | ||
* <info>Can only be set when type will be `GUILD_PRIVATE_THREAD`</info> | ||
* @property {number} [rateLimitPerUser] The rate limit per user (slowmode) for the new channel in seconds | ||
*/ | ||
|
||
/** | ||
* Creates a new thread in the channel. | ||
* @param {GuildTextThreadCreateOptions} [options] Options to create a new thread | ||
* @returns {Promise<ThreadChannel>} | ||
* @example | ||
* // Create a new public thread | ||
* channel.threads | ||
* .create({ | ||
* name: 'food-talk', | ||
* autoArchiveDuration: 60, | ||
* reason: 'Needed a separate thread for food', | ||
* }) | ||
* .then(threadChannel => console.log(threadChannel)) | ||
* .catch(console.error); | ||
* @example | ||
* // Create a new private thread | ||
* channel.threads | ||
* .create({ | ||
* name: 'mod-talk', | ||
* autoArchiveDuration: 60, | ||
* type: 'GUILD_PRIVATE_THREAD', | ||
* reason: 'Needed a separate thread for moderation', | ||
* }) | ||
* .then(threadChannel => console.log(threadChannel)) | ||
* .catch(console.error); | ||
*/ | ||
async create({ | ||
name, | ||
autoArchiveDuration = this.channel.defaultAutoArchiveDuration, | ||
startMessage, | ||
type, | ||
invitable, | ||
reason, | ||
rateLimitPerUser, | ||
} = {}) { | ||
let path = this.client.api.channels(this.channel.id); | ||
if (type && typeof type !== 'string' && typeof type !== 'number') { | ||
throw new TypeError('INVALID_TYPE', 'type', 'ThreadChannelType or Number'); | ||
} | ||
let resolvedType = | ||
this.channel.type === 'GUILD_NEWS' ? ChannelTypes.GUILD_NEWS_THREAD : ChannelTypes.GUILD_PUBLIC_THREAD; | ||
if (startMessage) { | ||
const startMessageId = this.channel.messages.resolveId(startMessage); | ||
if (!startMessageId) throw new TypeError('INVALID_TYPE', 'startMessage', 'MessageResolvable'); | ||
path = path.messages(startMessageId); | ||
} else if (this.channel.type !== 'GUILD_NEWS') { | ||
resolvedType = typeof type === 'string' ? ChannelTypes[type] : type ?? resolvedType; | ||
} | ||
|
||
if (autoArchiveDuration === 'MAX') autoArchiveDuration = resolveAutoArchiveMaxLimit(this.channel.guild); | ||
|
||
const data = await path.threads.post({ | ||
data: { | ||
name, | ||
auto_archive_duration: autoArchiveDuration, | ||
type: resolvedType, | ||
invitable: resolvedType === ChannelTypes.GUILD_PRIVATE_THREAD ? invitable : undefined, | ||
rate_limit_per_user: rateLimitPerUser, | ||
}, | ||
reason, | ||
}); | ||
|
||
return this.client.actions.ThreadCreate.handle(data).thread; | ||
} | ||
} | ||
|
||
module.exports = GuildTextThreadManager; |
Oops, something went wrong.