Skip to content

Commit 65ae064

Browse files
authored
refactor: Strengthen channel typings in appropriate places (#6232)
1 parent 82daee5 commit 65ae064

File tree

9 files changed

+70
-41
lines changed

9 files changed

+70
-41
lines changed

src/managers/GuildChannelManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class GuildChannelManager extends CachedManager {
8686
* @property {boolean} [nsfw] Whether the new channel is nsfw
8787
* @property {number} [bitrate] Bitrate of the new channel in bits (only voice)
8888
* @property {number} [userLimit] Maximum amount of users allowed in the new channel (only voice)
89-
* @property {ChannelResolvable} [parent] Parent of the new channel
89+
* @property {CategoryChannelResolvable} [parent] Parent of the new channel
9090
* @property {OverwriteResolvable[]|Collection<Snowflake, OverwriteResolvable>} [permissionOverwrites]
9191
* Permission overwrites of the new channel
9292
* @property {number} [position] Position of the new channel

src/structures/CategoryChannel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class CategoryChannel extends GuildChannel {
2222
* @method setParent
2323
* @memberof CategoryChannel
2424
* @instance
25-
* @param {?(GuildChannel|Snowflake)} channel The channel to set as parent
25+
* @param {?CategoryChannelResolvable} channel The channel to set as parent
2626
* @param {SetParentOptions} [options={}] The options for setting the parent
2727
* @returns {Promise<GuildChannel>}
2828
*/

src/structures/Guild.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -776,8 +776,8 @@ class Guild extends AnonymousGuild {
776776
* @property {string} [name] The name of the guild
777777
* @property {VerificationLevel|number} [verificationLevel] The verification level of the guild
778778
* @property {ExplicitContentFilterLevel|number} [explicitContentFilter] The level of the explicit content filter
779-
* @property {ChannelResolvable} [afkChannel] The AFK channel of the guild
780-
* @property {ChannelResolvable} [systemChannel] The system channel of the guild
779+
* @property {VoiceChannelResolvable} [afkChannel] The AFK channel of the guild
780+
* @property {TextChannelResolvable} [systemChannel] The system channel of the guild
781781
* @property {number} [afkTimeout] The AFK timeout of the guild
782782
* @property {Base64Resolvable} [icon] The icon of the guild
783783
* @property {GuildMemberResolvable} [owner] The owner of the guild
@@ -787,13 +787,27 @@ class Guild extends AnonymousGuild {
787787
* @property {DefaultMessageNotificationLevel|number} [defaultMessageNotifications] The default message notification
788788
* level of the guild
789789
* @property {SystemChannelFlagsResolvable} [systemChannelFlags] The system channel flags of the guild
790-
* @property {ChannelResolvable} [rulesChannel] The rules channel of the guild
791-
* @property {ChannelResolvable} [publicUpdatesChannel] The community updates channel of the guild
790+
* @property {TextChannelResolvable} [rulesChannel] The rules channel of the guild
791+
* @property {TextChannelResolvable} [publicUpdatesChannel] The community updates channel of the guild
792792
* @property {string} [preferredLocale] The preferred locale of the guild
793793
* @property {string} [description] The discovery description of the guild
794794
* @property {Features[]} [features] The features of the guild
795795
*/
796796

797+
/**
798+
* Data that can be resolved to a Text Channel object. This can be:
799+
* * A TextChannel
800+
* * A Snowflake
801+
* @typedef {TextChannel|Snowflake} TextChannelResolvable
802+
*/
803+
804+
/**
805+
* Data that can be resolved to a Voice Channel object. This can be:
806+
* * A VoiceChannel
807+
* * A Snowflake
808+
* @typedef {VoiceChannel|Snowflake} VoiceChannelResolvable
809+
*/
810+
797811
/**
798812
* Updates the guild with new information - e.g. a new name.
799813
* @param {GuildEditData} data The data to update the guild with
@@ -886,6 +900,14 @@ class Guild extends AnonymousGuild {
886900
* @typedef {TextChannel|NewsChannel|Snowflake} GuildTextChannelResolvable
887901
*/
888902

903+
/**
904+
* Data that can be resolved to a GuildVoiceChannel object. This can be:
905+
* * A VoiceChannel
906+
* * A StageChannel
907+
* * A Snowflake
908+
* @typedef {VoiceChannel|StageChannel|Snowflake} GuildVoiceChannelResolvable
909+
*/
910+
889911
/**
890912
* Updates the guild's welcome screen
891913
* @param {WelcomeScreenEditData} data Data to edit the welcome screen with
@@ -988,7 +1010,7 @@ class Guild extends AnonymousGuild {
9881010

9891011
/**
9901012
* Edits the AFK channel of the guild.
991-
* @param {ChannelResolvable} afkChannel The new AFK channel
1013+
* @param {VoiceChannelResolvable} afkChannel The new AFK channel
9921014
* @param {string} [reason] Reason for changing the guild's AFK channel
9931015
* @returns {Promise<Guild>}
9941016
* @example
@@ -1003,7 +1025,7 @@ class Guild extends AnonymousGuild {
10031025

10041026
/**
10051027
* Edits the system channel of the guild.
1006-
* @param {ChannelResolvable} systemChannel The new system channel
1028+
* @param {TextChannelResolvable} systemChannel The new system channel
10071029
* @param {string} [reason] Reason for changing the guild's system channel
10081030
* @returns {Promise<Guild>}
10091031
* @example
@@ -1107,7 +1129,7 @@ class Guild extends AnonymousGuild {
11071129

11081130
/**
11091131
* Edits the rules channel of the guild.
1110-
* @param {ChannelResolvable} rulesChannel The new rules channel
1132+
* @param {TextChannelResolvable} rulesChannel The new rules channel
11111133
* @param {string} [reason] Reason for changing the guild's rules channel
11121134
* @returns {Promise<Guild>}
11131135
* @example
@@ -1122,7 +1144,7 @@ class Guild extends AnonymousGuild {
11221144

11231145
/**
11241146
* Edits the community updates channel of the guild.
1125-
* @param {ChannelResolvable} publicUpdatesChannel The new community updates channel
1147+
* @param {TextChannelResolvable} publicUpdatesChannel The new community updates channel
11261148
* @param {string} [reason] Reason for changing the guild's community updates channel
11271149
* @returns {Promise<Guild>}
11281150
* @example
@@ -1160,7 +1182,7 @@ class Guild extends AnonymousGuild {
11601182
/**
11611183
* The data needed for updating a channel's position.
11621184
* @typedef {Object} ChannelPosition
1163-
* @property {ChannelResolvable} channel Channel to update
1185+
* @property {GuildChannel|Snowflake} channel Channel to update
11641186
* @property {number} [position] New position for the channel
11651187
* @property {CategoryChannelResolvable} [parent] Parent channel for this channel
11661188
* @property {boolean} [lockPermissions] If the overwrites should be locked to the parents overwrites

src/structures/GuildChannel.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ class GuildChannel extends Channel {
277277
* @property {boolean} [nsfw] Whether the channel is NSFW
278278
* @property {number} [bitrate] The bitrate of the voice channel
279279
* @property {number} [userLimit] The user limit of the voice channel
280-
* @property {?Snowflake} [parentId] The parent's id of the channel
280+
* @property {?CategoryChannelResolvable} [parent] The parent of the channel
281281
* @property {boolean} [lockPermissions]
282282
* Lock the permissions of the channel to what the parent's permissions are
283283
* @property {OverwriteResolvable[]|Collection<Snowflake, OverwriteResolvable>} [permissionOverwrites]
@@ -300,6 +300,8 @@ class GuildChannel extends Channel {
300300
* .catch(console.error);
301301
*/
302302
async edit(data, reason) {
303+
if (data.parent) data.parent = this.client.channels.resolveId(data.parent);
304+
303305
if (typeof data.position !== 'undefined') {
304306
await Util.setPosition(
305307
this,
@@ -323,8 +325,8 @@ class GuildChannel extends Channel {
323325
}
324326

325327
if (data.lockPermissions) {
326-
if (data.parentId) {
327-
const newParent = this.guild.channels.resolve(data.parentId);
328+
if (data.parent) {
329+
const newParent = this.guild.channels.resolve(data.parent);
328330
if (newParent?.type === 'GUILD_CATEGORY') {
329331
permission_overwrites = newParent.permissionOverwrites.cache.map(o =>
330332
PermissionOverwrites.resolve(o, this.guild),
@@ -346,7 +348,7 @@ class GuildChannel extends Channel {
346348
bitrate: data.bitrate ?? this.bitrate,
347349
user_limit: data.userLimit ?? this.userLimit,
348350
rtc_region: data.rtcRegion ?? this.rtcRegion,
349-
parent_id: data.parentId,
351+
parent_id: data.parent,
350352
lock_permissions: data.lockPermissions,
351353
rate_limit_per_user: data.rateLimitPerUser,
352354
default_auto_archive_duration: data.defaultAutoArchiveDuration,
@@ -382,7 +384,7 @@ class GuildChannel extends Channel {
382384

383385
/**
384386
* Sets the parent of this channel.
385-
* @param {?(CategoryChannel|Snowflake)} channel The category channel to set as parent
387+
* @param {?CategoryChannelResolvable} channel The category channel to set as the parent
386388
* @param {SetParentOptions} [options={}] The options for setting the parent
387389
* @returns {Promise<GuildChannel>}
388390
* @example
@@ -395,7 +397,7 @@ class GuildChannel extends Channel {
395397
return this.edit(
396398
{
397399
// eslint-disable-next-line no-prototype-builtins
398-
parentId: channel?.id ?? channel ?? null,
400+
parent: channel ?? null,
399401
lockPermissions,
400402
},
401403
reason,

src/structures/GuildMember.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ class GuildMember extends Base {
236236
* @property {Collection<Snowflake, Role>|RoleResolvable[]} [roles] The roles or role ids to apply
237237
* @property {boolean} [mute] Whether or not the member should be muted
238238
* @property {boolean} [deaf] Whether or not the member should be deafened
239-
* @property {ChannelResolvable|null} [channel] Channel to move member to (if they are connected to voice), or `null`
240-
* if you want to kick them from voice
239+
* @property {GuildVoiceChannelResolvable|null} [channel] Channel to move member to (if they are connected to voice),
240+
* or `null` if you want to disconnect them from voice
241241
*/
242242

243243
/**

src/structures/Role.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ class Role extends Base {
203203
/**
204204
* Returns `channel.permissionsFor(role)`. Returns permissions for a role in a guild channel,
205205
* taking into account permission overwrites.
206-
* @param {ChannelResolvable} channel The guild channel to use as context
206+
* @param {GuildChannel|Snowflake} channel The guild channel to use as context
207207
* @returns {Readonly<Permissions>}
208208
*/
209209
permissionsIn(channel) {

src/structures/VoiceState.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ class VoiceState extends Base {
149149

150150
/**
151151
* Moves the member to a different channel, or disconnects them from the one they're in.
152-
* @param {ChannelResolvable|null} channel Channel to move the member to, or `null` if you want to disconnect them
153-
* from voice.
152+
* @param {VoiceChannelResolvable|null} channel Channel to move the member to, or `null` if you want to disconnect
153+
* them from voice.
154154
* @param {string} [reason] Reason for moving member to another channel or disconnecting
155155
* @returns {Promise<GuildMember>}
156156
*/

src/structures/Webhook.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
const Channel = require('./Channel');
43
const MessagePayload = require('./MessagePayload');
54
const { Error } = require('../errors');
65
const { WebhookTypes } = require('../util/Constants');
@@ -214,7 +213,7 @@ class Webhook {
214213
* @typedef {Object} WebhookEditData
215214
* @property {string} [name=this.name] The new name for the webhook
216215
* @property {BufferResolvable} [avatar] The new avatar for the webhook
217-
* @property {ChannelResolvable} [channel] The new channel for the webhook
216+
* @property {GuildTextChannelResolvable} [channel] The new channel for the webhook
218217
*/
219218

220219
/**
@@ -227,7 +226,7 @@ class Webhook {
227226
if (avatar && !(typeof avatar === 'string' && avatar.startsWith('data:'))) {
228227
avatar = await DataResolver.resolveImage(avatar);
229228
}
230-
if (channel) channel = channel instanceof Channel ? channel.id : channel;
229+
if (channel) channel = channel?.id ?? channel;
231230
const data = await this.client.api.webhooks(this.id, channel ? undefined : this.token).patch({
232231
data: { name, avatar, channel_id: channel },
233232
reason,

typings/index.d.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ export class Guild extends AnonymousGuild {
593593
public fetchWidget(): Promise<Widget>;
594594
public fetchWidgetSettings(): Promise<GuildWidgetSettings>;
595595
public leave(): Promise<Guild>;
596-
public setAFKChannel(afkChannel: ChannelResolvable | null, reason?: string): Promise<Guild>;
596+
public setAFKChannel(afkChannel: VoiceChannelResolvable | null, reason?: string): Promise<Guild>;
597597
public setAFKTimeout(afkTimeout: number, reason?: string): Promise<Guild>;
598598
public setBanner(banner: Base64Resolvable | null, reason?: string): Promise<Guild>;
599599
public setChannelPositions(channelPositions: readonly ChannelPosition[]): Promise<Guild>;
@@ -610,11 +610,11 @@ export class Guild extends AnonymousGuild {
610610
public setName(name: string, reason?: string): Promise<Guild>;
611611
public setOwner(owner: GuildMemberResolvable, reason?: string): Promise<Guild>;
612612
public setPreferredLocale(preferredLocale: string, reason?: string): Promise<Guild>;
613-
public setPublicUpdatesChannel(publicUpdatesChannel: ChannelResolvable | null, reason?: string): Promise<Guild>;
613+
public setPublicUpdatesChannel(publicUpdatesChannel: TextChannelResolvable | null, reason?: string): Promise<Guild>;
614614
public setRolePositions(rolePositions: readonly RolePosition[]): Promise<Guild>;
615-
public setRulesChannel(rulesChannel: ChannelResolvable | null, reason?: string): Promise<Guild>;
615+
public setRulesChannel(rulesChannel: TextChannelResolvable | null, reason?: string): Promise<Guild>;
616616
public setSplash(splash: Base64Resolvable | null, reason?: string): Promise<Guild>;
617-
public setSystemChannel(systemChannel: ChannelResolvable | null, reason?: string): Promise<Guild>;
617+
public setSystemChannel(systemChannel: TextChannelResolvable | null, reason?: string): Promise<Guild>;
618618
public setSystemChannelFlags(systemChannelFlags: SystemChannelFlagsResolvable, reason?: string): Promise<Guild>;
619619
public setVerificationLevel(verificationLevel: VerificationLevel | number, reason?: string): Promise<Guild>;
620620
public setWidgetSettings(settings: GuildWidgetSettingsData, reason?: string): Promise<Guild>;
@@ -705,7 +705,7 @@ export class GuildChannel extends Channel {
705705
public permissionsFor(memberOrRole: GuildMember | Role): Readonly<Permissions>;
706706
public permissionsFor(memberOrRole: GuildMemberResolvable | RoleResolvable): Readonly<Permissions> | null;
707707
public setName(name: string, reason?: string): Promise<this>;
708-
public setParent(channel: CategoryChannel | Snowflake | null, options?: SetParentOptions): Promise<this>;
708+
public setParent(channel: CategoryChannelResolvable | null, options?: SetParentOptions): Promise<this>;
709709
public setPosition(position: number, options?: SetChannelPositionOptions): Promise<this>;
710710
public setTopic(topic: string | null, reason?: string): Promise<this>;
711711
public isText(): this is TextChannel | NewsChannel;
@@ -1415,7 +1415,7 @@ export class Role extends Base {
14151415
public delete(reason?: string): Promise<Role>;
14161416
public edit(data: RoleData, reason?: string): Promise<Role>;
14171417
public equals(role: Role): boolean;
1418-
public permissionsIn(channel: ChannelResolvable): Readonly<Permissions>;
1418+
public permissionsIn(channel: GuildChannel | Snowflake): Readonly<Permissions>;
14191419
public setColor(color: ColorResolvable, reason?: string): Promise<Role>;
14201420
public setHoist(hoist: boolean, reason?: string): Promise<Role>;
14211421
public setMentionable(mentionable: boolean, reason?: string): Promise<Role>;
@@ -1885,7 +1885,7 @@ export class VoiceState extends Base {
18851885
public setDeaf(deaf: boolean, reason?: string): Promise<GuildMember>;
18861886
public setMute(mute: boolean, reason?: string): Promise<GuildMember>;
18871887
public kick(reason?: string): Promise<GuildMember>;
1888-
public setChannel(channel: ChannelResolvable | null, reason?: string): Promise<GuildMember>;
1888+
public setChannel(channel: VoiceChannelResolvable | null, reason?: string): Promise<GuildMember>;
18891889
public setRequestToSpeak(request: boolean): Promise<void>;
18901890
public setSuppressed(suppressed: boolean): Promise<void>;
18911891
}
@@ -2901,7 +2901,7 @@ export interface ChannelData {
29012901
nsfw?: boolean;
29022902
bitrate?: number;
29032903
userLimit?: number;
2904-
parentId?: Snowflake | null;
2904+
parent?: CategoryChannelResolvable | null;
29052905
rateLimitPerUser?: number;
29062906
lockPermissions?: boolean;
29072907
permissionOverwrites?: readonly OverwriteResolvable[] | Collection<Snowflake, OverwriteResolvable>;
@@ -2919,7 +2919,7 @@ export interface ChannelLogsQueryOptions {
29192919
export type ChannelMention = `<#${Snowflake}>`;
29202920

29212921
export interface ChannelPosition {
2922-
channel: ChannelResolvable;
2922+
channel: GuildChannel | Snowflake;
29232923
lockPermissions?: boolean;
29242924
parent?: CategoryChannelResolvable | null;
29252925
position?: number;
@@ -3517,7 +3517,7 @@ export interface GuildChannelCreateOptions {
35173517
| ChannelTypes.GUILD_PRIVATE_THREAD
35183518
>;
35193519
nsfw?: boolean;
3520-
parent?: ChannelResolvable;
3520+
parent?: CategoryChannelResolvable;
35213521
bitrate?: number;
35223522
userLimit?: number;
35233523
rateLimitPerUser?: number;
@@ -3557,17 +3557,17 @@ export interface GuildEditData {
35573557
verificationLevel?: VerificationLevel | number;
35583558
explicitContentFilter?: ExplicitContentFilterLevel | number;
35593559
defaultMessageNotifications?: DefaultMessageNotificationLevel | number;
3560-
afkChannel?: ChannelResolvable;
3561-
systemChannel?: ChannelResolvable;
3560+
afkChannel?: VoiceChannelResolvable;
3561+
systemChannel?: TextChannelResolvable;
35623562
systemChannelFlags?: SystemChannelFlagsResolvable;
35633563
afkTimeout?: number;
35643564
icon?: Base64Resolvable;
35653565
owner?: GuildMemberResolvable;
35663566
splash?: Base64Resolvable;
35673567
discoverySplash?: Base64Resolvable;
35683568
banner?: Base64Resolvable;
3569-
rulesChannel?: ChannelResolvable;
3570-
publicUpdatesChannel?: ChannelResolvable;
3569+
rulesChannel?: TextChannelResolvable;
3570+
publicUpdatesChannel?: TextChannelResolvable;
35713571
preferredLocale?: string;
35723572
description?: string | null;
35733573
features?: GuildFeatures[];
@@ -3623,7 +3623,7 @@ export interface GuildMemberEditData {
36233623
roles?: Collection<Snowflake, Role> | readonly RoleResolvable[];
36243624
mute?: boolean;
36253625
deaf?: boolean;
3626-
channel?: ChannelResolvable | null;
3626+
channel?: GuildVoiceChannelResolvable | null;
36273627
}
36283628

36293629
export type GuildMemberResolvable = GuildMember | UserResolvable;
@@ -3651,6 +3651,8 @@ export interface GuildSearchMembersOptions {
36513651

36523652
export type GuildTemplateResolvable = string;
36533653

3654+
export type GuildVoiceChannelResolvable = VoiceChannel | StageChannel | Snowflake;
3655+
36543656
export type HexColorString = `#${string}`;
36553657

36563658
export interface HTTPAttachmentData {
@@ -4310,6 +4312,8 @@ export type TextBasedChannelTypes =
43104312
| 'GUILD_PUBLIC_THREAD'
43114313
| 'GUILD_PRIVATE_THREAD';
43124314

4315+
export type TextChannelResolvable = Snowflake | TextChannel;
4316+
43134317
export type ThreadAutoArchiveDuration = 60 | 1440 | 4320 | 10080;
43144318

43154319
export type ThreadChannelResolvable = ThreadChannel | Snowflake;
@@ -4364,6 +4368,8 @@ export type VerificationLevel = keyof typeof VerificationLevels;
43644368

43654369
export type VoiceBasedChannelTypes = 'GUILD_VOICE' | 'GUILD_STAGE_VOICE';
43664370

4371+
export type VoiceChannelResolvable = Snowflake | VoiceChannel;
4372+
43674373
export type WebhookClientData = WebhookClientDataIdWithToken | WebhookClientDataURL;
43684374

43694375
export interface WebhookClientDataIdWithToken {
@@ -4383,7 +4389,7 @@ export type WebhookClientOptions = Pick<
43834389
export interface WebhookEditData {
43844390
name?: string;
43854391
avatar?: BufferResolvable;
4386-
channel?: ChannelResolvable;
4392+
channel?: GuildTextChannelResolvable;
43874393
}
43884394

43894395
export type WebhookEditMessageOptions = Pick<

0 commit comments

Comments
 (0)