-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
68d5169
commit 38f345e
Showing
10 changed files
with
244 additions
and
78 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
76 changes: 76 additions & 0 deletions
76
packages/discord.js/src/managers/GuildForumThreadManager.js
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,76 @@ | ||
'use strict'; | ||
|
||
const { Routes } = require('discord-api-types/v10'); | ||
const ThreadManager = require('./ThreadManager'); | ||
const { TypeError } = require('../errors'); | ||
const MessagePayload = require('../structures/MessagePayload'); | ||
const { resolveAutoArchiveMaxLimit } = require('../util/Util'); | ||
|
||
class GuildForumThreadManager extends ThreadManager { | ||
/** | ||
* Options for creating a thread. <warn>Only one of `startMessage` or `type` can be defined.</warn> | ||
* @typedef {StartThreadOptions} GuildForumThreadCreateOptions | ||
* @property {MessageOptions|MessagePayload} message The message associated with the thread post | ||
*/ | ||
|
||
/** | ||
* Creates a new thread in the channel. | ||
* @param {GuildForumThreadCreateOptions} [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: ChannelType.GuildPrivateThread, | ||
* reason: 'Needed a separate thread for moderation', | ||
* }) | ||
* .then(threadChannel => console.log(threadChannel)) | ||
* .catch(console.error); | ||
*/ | ||
async create({ | ||
name, | ||
autoArchiveDuration = this.channel.defaultAutoArchiveDuration, | ||
message, | ||
reason, | ||
rateLimitPerUser, | ||
} = {}) { | ||
if (!message) { | ||
throw new TypeError('GUILD_FORUM_MESSAGE_REQUIRED'); | ||
} | ||
|
||
const messagePayload = | ||
message instanceof MessagePayload ? message.resolveBody() : MessagePayload.create(this, message); | ||
|
||
const { body, files } = messagePayload.resolveFiles(); | ||
|
||
if (autoArchiveDuration === 'MAX') autoArchiveDuration = resolveAutoArchiveMaxLimit(this.channel.guild); | ||
|
||
const data = await this.client.rest.post(Routes.threads(this.channel.id), { | ||
body: { | ||
name, | ||
auto_archive_duration: autoArchiveDuration, | ||
rate_limit_per_user: rateLimitPerUser, | ||
...body, | ||
}, | ||
files, | ||
reason, | ||
}); | ||
|
||
// TODO: Posts will most likely need to be serialized differently than regular threads. | ||
return this.client.actions.ThreadCreate.handle(data).thread; | ||
} | ||
} | ||
|
||
module.exports = GuildForumThreadManager; |
91 changes: 91 additions & 0 deletions
91
packages/discord.js/src/managers/GuildTextThreadManager.js
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,91 @@ | ||
'use strict'; | ||
|
||
const { ChannelType, Routes } = require('discord-api-types/v10'); | ||
const ThreadManager = require('./ThreadManager'); | ||
const { TypeError } = require('../errors'); | ||
const { resolveAutoArchiveMaxLimit } = require('../util/Util'); | ||
|
||
/** | ||
* Manages API methods for {@link ThreadChannel} objects and stores their cache. | ||
* @extends {CachedManager} | ||
*/ | ||
class GuildTextThreadManager extends ThreadManager { | ||
/** | ||
* Options for creating a thread. <warn>Only one of `startMessage` or `type` can be defined.</warn> | ||
* @typedef {StartThreadOptions} ThreadCreateOptions | ||
* @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 {@link ChannelType.GuildPublicThread} if created in a {@link TextChannel} | ||
* <warn>When creating threads in a {@link NewsChannel} this is ignored and is always | ||
* {@link ChannelType.GuildNewsThread}</warn> | ||
* @property {boolean} [invitable] Whether non-moderators can add other non-moderators to the thread | ||
* <info>Can only be set when type will be {@link ChannelType.GuildPrivateThread}</info> | ||
*/ | ||
|
||
/** | ||
* Creates a new thread in the channel. | ||
* @param {ThreadCreateOptions} [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: ChannelType.GuildPrivateThread, | ||
* 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, | ||
} = {}) { | ||
if (type && typeof type !== 'string' && typeof type !== 'number') { | ||
throw new TypeError('INVALID_TYPE', 'type', 'ThreadChannelType or Number'); | ||
} | ||
let resolvedType = | ||
this.channel.type === ChannelType.GuildNews ? ChannelType.GuildNewsThread : ChannelType.GuildPublicThread; | ||
let startMessageId; | ||
if (startMessage) { | ||
startMessageId = this.channel.messages.resolveId(startMessage); | ||
if (!startMessageId) throw new TypeError('INVALID_TYPE', 'startMessage', 'MessageResolvable'); | ||
} else if (this.channel.type !== ChannelType.GuildNews) { | ||
resolvedType = type ?? resolvedType; | ||
} | ||
|
||
if (autoArchiveDuration === 'MAX') autoArchiveDuration = resolveAutoArchiveMaxLimit(this.channel.guild); | ||
|
||
const data = await this.client.rest.post(Routes.threads(this.channel.id, startMessageId), { | ||
body: { | ||
name, | ||
auto_archive_duration: autoArchiveDuration, | ||
type: resolvedType, | ||
invitable: resolvedType === ChannelType.GuildPrivateThread ? invitable : undefined, | ||
rate_limit_per_user: rateLimitPerUser, | ||
}, | ||
reason, | ||
}); | ||
|
||
return this.client.actions.ThreadCreate.handle(data).thread; | ||
} | ||
} | ||
|
||
module.exports = GuildTextThreadManager; |
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,22 @@ | ||
'use strict'; | ||
|
||
const GuildChannel = require('./GuildChannel'); | ||
const GuildForumThreadManager = require('../managers/GuildForumThreadManager'); | ||
|
||
/** | ||
* Represents a channel that only contains threads | ||
* @extends {GuildChannel} | ||
*/ | ||
class GuildForumChannel extends GuildChannel { | ||
constructor(guild, data, client) { | ||
super(guild, data, client, false); | ||
|
||
/** | ||
* A manager of the threads belonging to this channel | ||
* @type {GuildForumThreadManager} | ||
*/ | ||
this.threads = new GuildForumThreadManager(this); | ||
} | ||
} | ||
|
||
module.exports = GuildForumChannel; |
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
Oops, something went wrong.