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

feat(ThreadChannel): add a helper for pin and unpin #8786

Merged
merged 12 commits into from
Nov 19, 2022
4 changes: 4 additions & 0 deletions packages/discord.js/src/errors/ErrorCodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@
* @property {'StageChannelResolve'} StageChannelResolve
* @property {'GuildScheduledEventResolve'} GuildScheduledEventResolve
* @property {'FetchOwnerId'} FetchOwnerId
* @property {'CannotPinThreadOutOfForumChannel'} CannotPinThreadOutOfForumChannel
* @property {'CannotUnpinThreadOutOfForumChannel'} CannotUnpinThreadOutOfForumChannel

* @property {'InvalidType'} InvalidType
* @property {'InvalidElement'} InvalidElement
Expand Down Expand Up @@ -236,6 +238,8 @@ const keys = [
'StageChannelResolve',
'GuildScheduledEventResolve',
'FetchOwnerId',
'CannotPinThreadOutOfForumChannel',
'CannotUnpinThreadOutOfForumChannel',

'InvalidType',
'InvalidElement',
Expand Down
2 changes: 2 additions & 0 deletions packages/discord.js/src/errors/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ const Messages = {
[DjsErrorCodes.StageChannelResolve]: 'Could not resolve channel to a stage channel.',
[DjsErrorCodes.GuildScheduledEventResolve]: 'Could not resolve the guild scheduled event.',
[DjsErrorCodes.FetchOwnerId]: "Couldn't resolve the guild ownerId to fetch the member.",
[DjsErrorCodes.CannotPinThreadOutOfForumChannel]: 'Cannot pin a thread which is not in a ForumChannel.',
[DjsErrorCodes.CannotUnpinThreadOutOfForumChannel]: 'Cannot unpin a thread which is not in a ForumChannel.',

[DjsErrorCodes.InvalidType]: (name, expected, an = false) => `Supplied ${name} is not a${an ? 'n' : ''} ${expected}.`,
[DjsErrorCodes.InvalidElement]: (type, name, elem) => `Supplied ${type} ${name} includes an invalid element: ${elem}`,
Expand Down
23 changes: 22 additions & 1 deletion packages/discord.js/src/structures/ThreadChannel.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { ChannelType, PermissionFlagsBits, Routes } = require('discord-api-types/v10');
const { ChannelType, PermissionFlagsBits, Routes, ChannelFlags } = require('discord-api-types/v10');
const { BaseChannel } = require('./BaseChannel');
const TextBasedChannel = require('./interfaces/TextBasedChannel');
const { DiscordjsRangeError, ErrorCodes } = require('../errors');
Expand Down Expand Up @@ -456,6 +456,27 @@ class ThreadChannel extends BaseChannel {
setAppliedTags(appliedTags, reason) {
return this.edit({ appliedTags, reason });
}

/**
* Pins this thread channel from the forum channel.
Idris1401 marked this conversation as resolved.
Show resolved Hide resolved
* @param {string} [reason] Reason for pinning
* @returns {Promise<ThreadChannel>}
*/
pin(reason) {
if (this.parent.type !== ChannelType.ForumChannel) throw new DiscordjsError(ErrorCodes.CannotPinThreadOutOfForumChannel);
Idris1401 marked this conversation as resolved.
Show resolved Hide resolved
return this.edit({ flags: [ChannelFlags.Pinned], reason });
Idris1401 marked this conversation as resolved.
Show resolved Hide resolved
Idris1401 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Unpins this thread channel from the forum channel.
Idris1401 marked this conversation as resolved.
Show resolved Hide resolved
* @param {string} [reason] Reason for unpinning
* @returns {Promise<ThreadChannel>}
*/
unpin(reason) {
if (this.parent.type !== ChannelType.ForumChannel) throw new DiscordjsError(ErrorCodes.CannotUnpinThreadOutOfForumChannel);
Idris1401 marked this conversation as resolved.
Show resolved Hide resolved
const flags = [this.flags.remove(ChannelFlags.Pinned)];
return this.edit({ flags, reason });
Idris1401 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Whether the client user is a member of the thread.
Expand Down
2 changes: 2 additions & 0 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2640,6 +2640,8 @@ export class ThreadChannel<Forum extends boolean = boolean> extends TextBasedCha
public setLocked(locked?: boolean, reason?: string): Promise<AnyThreadChannel>;
public setName(name: string, reason?: string): Promise<AnyThreadChannel>;
public setAppliedTags(appliedTags: Snowflake[], reason?: string): Promise<ThreadChannel<true>>;
public pin(reason?: string): Promise<ThreadChannel<true>>;
public unpin(reason?: string): Promise<ThreadChannel<true>>;
Idris1401 marked this conversation as resolved.
Show resolved Hide resolved
public toString(): ChannelMention;
}

Expand Down