feat(GuildChannel): support conversion between text and news (#5022)

* feat(GuildChannel): support conversion between text and news

* fix(Typings): add type to ChannelData

* fix(GuildChannel): use ChannelUpdate action handler to change class type

* Update src/structures/TextChannel.js

Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com>

* fix(Typings): re-use ChannelType from GuildCreateChannelOptions

* fix(Typings): only allow text-news conversion

* fix(Typings): exclude -> pick (vlads suggestion)

* fix(Typings): pick -> exclude in two other spots

Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com>
This commit is contained in:
monbrey
2021-01-23 04:03:02 +11:00
committed by GitHub
parent 98b1c58218
commit 5ac3b57f9b
3 changed files with 17 additions and 3 deletions

View File

@@ -6,6 +6,7 @@ const PermissionOverwrites = require('./PermissionOverwrites');
const Role = require('./Role'); const Role = require('./Role');
const { Error, TypeError } = require('../errors'); const { Error, TypeError } = require('../errors');
const Collection = require('../util/Collection'); const Collection = require('../util/Collection');
const { ChannelTypes } = require('../util/Constants');
const Permissions = require('../util/Permissions'); const Permissions = require('../util/Permissions');
const Util = require('../util/Util'); const Util = require('../util/Util');
@@ -294,6 +295,7 @@ class GuildChannel extends Channel {
* The data for a guild channel. * The data for a guild channel.
* @typedef {Object} ChannelData * @typedef {Object} ChannelData
* @property {string} [name] The name of the channel * @property {string} [name] The name of the channel
* @property {string} [type] The type of the the channel (only conversion between text and news is supported)
* @property {number} [position] The position of the channel * @property {number} [position] The position of the channel
* @property {string} [topic] The topic of the text channel * @property {string} [topic] The topic of the text channel
* @property {boolean} [nsfw] Whether the channel is NSFW * @property {boolean} [nsfw] Whether the channel is NSFW
@@ -355,6 +357,7 @@ class GuildChannel extends Channel {
const newData = await this.client.api.channels(this.id).patch({ const newData = await this.client.api.channels(this.id).patch({
data: { data: {
name: (data.name || this.name).trim(), name: (data.name || this.name).trim(),
type: data.type ? ChannelTypes[data.type.toUpperCase()] : this.type,
topic: data.topic, topic: data.topic,
nsfw: data.nsfw, nsfw: data.nsfw,
bitrate: data.bitrate || this.bitrate, bitrate: data.bitrate || this.bitrate,
@@ -367,9 +370,7 @@ class GuildChannel extends Channel {
reason, reason,
}); });
const clone = this._clone(); return this.client.actions.ChannelUpdate.handle(newData).updated;
clone._patch(newData);
return clone;
} }
/** /**

View File

@@ -86,6 +86,16 @@ class TextChannel extends GuildChannel {
return this.edit({ nsfw }, reason); return this.edit({ nsfw }, reason);
} }
/**
* Sets the type of this channel (only conversion between text and news is supported)
* @param {string} type The new channel type
* @param {string} [reason] Reason for changing the channel's type
* @returns {Promise<GuildChannel>}
*/
setType(type, reason) {
return this.edit({ type }, reason);
}
/** /**
* Fetches all webhooks for the channel. * Fetches all webhooks for the channel.
* @returns {Promise<Collection<Snowflake, Webhook>>} * @returns {Promise<Collection<Snowflake, Webhook>>}

3
typings/index.d.ts vendored
View File

@@ -1162,6 +1162,7 @@ declare module 'discord.js' {
options?: { avatar?: BufferResolvable | Base64Resolvable; reason?: string }, options?: { avatar?: BufferResolvable | Base64Resolvable; reason?: string },
): Promise<Webhook>; ): Promise<Webhook>;
public setNSFW(nsfw: boolean, reason?: string): Promise<NewsChannel>; public setNSFW(nsfw: boolean, reason?: string): Promise<NewsChannel>;
public setType(type: Pick<typeof ChannelType, 'text' | 'news'>, reason?: string): Promise<GuildChannel>;
public fetchWebhooks(): Promise<Collection<Snowflake, Webhook>>; public fetchWebhooks(): Promise<Collection<Snowflake, Webhook>>;
public addFollower(channel: GuildChannelResolvable, reason?: string): Promise<NewsChannel>; public addFollower(channel: GuildChannelResolvable, reason?: string): Promise<NewsChannel>;
} }
@@ -1513,6 +1514,7 @@ declare module 'discord.js' {
): Promise<Webhook>; ): Promise<Webhook>;
public setNSFW(nsfw: boolean, reason?: string): Promise<TextChannel>; public setNSFW(nsfw: boolean, reason?: string): Promise<TextChannel>;
public setRateLimitPerUser(rateLimitPerUser: number, reason?: string): Promise<TextChannel>; public setRateLimitPerUser(rateLimitPerUser: number, reason?: string): Promise<TextChannel>;
public setType(type: Pick<typeof ChannelType, 'text' | 'news'>, reason?: string): Promise<GuildChannel>;
public fetchWebhooks(): Promise<Collection<Snowflake, Webhook>>; public fetchWebhooks(): Promise<Collection<Snowflake, Webhook>>;
} }
@@ -2339,6 +2341,7 @@ declare module 'discord.js' {
interface ChannelData { interface ChannelData {
name?: string; name?: string;
type?: Pick<typeof ChannelType, 'text' | 'news'>;
position?: number; position?: number;
topic?: string; topic?: string;
nsfw?: boolean; nsfw?: boolean;