Skip to content

Commit

Permalink
refactor: Strengthen channel typings in appropriate places (#6232)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiralite authored Jul 31, 2021
1 parent 82daee5 commit 65ae064
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/managers/GuildChannelManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class GuildChannelManager extends CachedManager {
* @property {boolean} [nsfw] Whether the new channel is nsfw
* @property {number} [bitrate] Bitrate of the new channel in bits (only voice)
* @property {number} [userLimit] Maximum amount of users allowed in the new channel (only voice)
* @property {ChannelResolvable} [parent] Parent of the new channel
* @property {CategoryChannelResolvable} [parent] Parent of the new channel
* @property {OverwriteResolvable[]|Collection<Snowflake, OverwriteResolvable>} [permissionOverwrites]
* Permission overwrites of the new channel
* @property {number} [position] Position of the new channel
Expand Down
2 changes: 1 addition & 1 deletion src/structures/CategoryChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CategoryChannel extends GuildChannel {
* @method setParent
* @memberof CategoryChannel
* @instance
* @param {?(GuildChannel|Snowflake)} channel The channel to set as parent
* @param {?CategoryChannelResolvable} channel The channel to set as parent
* @param {SetParentOptions} [options={}] The options for setting the parent
* @returns {Promise<GuildChannel>}
*/
Expand Down
40 changes: 31 additions & 9 deletions src/structures/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -776,8 +776,8 @@ class Guild extends AnonymousGuild {
* @property {string} [name] The name of the guild
* @property {VerificationLevel|number} [verificationLevel] The verification level of the guild
* @property {ExplicitContentFilterLevel|number} [explicitContentFilter] The level of the explicit content filter
* @property {ChannelResolvable} [afkChannel] The AFK channel of the guild
* @property {ChannelResolvable} [systemChannel] The system channel of the guild
* @property {VoiceChannelResolvable} [afkChannel] The AFK channel of the guild
* @property {TextChannelResolvable} [systemChannel] The system channel of the guild
* @property {number} [afkTimeout] The AFK timeout of the guild
* @property {Base64Resolvable} [icon] The icon of the guild
* @property {GuildMemberResolvable} [owner] The owner of the guild
Expand All @@ -787,13 +787,27 @@ class Guild extends AnonymousGuild {
* @property {DefaultMessageNotificationLevel|number} [defaultMessageNotifications] The default message notification
* level of the guild
* @property {SystemChannelFlagsResolvable} [systemChannelFlags] The system channel flags of the guild
* @property {ChannelResolvable} [rulesChannel] The rules channel of the guild
* @property {ChannelResolvable} [publicUpdatesChannel] The community updates channel of the guild
* @property {TextChannelResolvable} [rulesChannel] The rules channel of the guild
* @property {TextChannelResolvable} [publicUpdatesChannel] The community updates channel of the guild
* @property {string} [preferredLocale] The preferred locale of the guild
* @property {string} [description] The discovery description of the guild
* @property {Features[]} [features] The features of the guild
*/

/**
* Data that can be resolved to a Text Channel object. This can be:
* * A TextChannel
* * A Snowflake
* @typedef {TextChannel|Snowflake} TextChannelResolvable
*/

/**
* Data that can be resolved to a Voice Channel object. This can be:
* * A VoiceChannel
* * A Snowflake
* @typedef {VoiceChannel|Snowflake} VoiceChannelResolvable
*/

/**
* Updates the guild with new information - e.g. a new name.
* @param {GuildEditData} data The data to update the guild with
Expand Down Expand Up @@ -886,6 +900,14 @@ class Guild extends AnonymousGuild {
* @typedef {TextChannel|NewsChannel|Snowflake} GuildTextChannelResolvable
*/

/**
* Data that can be resolved to a GuildVoiceChannel object. This can be:
* * A VoiceChannel
* * A StageChannel
* * A Snowflake
* @typedef {VoiceChannel|StageChannel|Snowflake} GuildVoiceChannelResolvable
*/

/**
* Updates the guild's welcome screen
* @param {WelcomeScreenEditData} data Data to edit the welcome screen with
Expand Down Expand Up @@ -988,7 +1010,7 @@ class Guild extends AnonymousGuild {

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

/**
* Edits the system channel of the guild.
* @param {ChannelResolvable} systemChannel The new system channel
* @param {TextChannelResolvable} systemChannel The new system channel
* @param {string} [reason] Reason for changing the guild's system channel
* @returns {Promise<Guild>}
* @example
Expand Down Expand Up @@ -1107,7 +1129,7 @@ class Guild extends AnonymousGuild {

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

/**
* Edits the community updates channel of the guild.
* @param {ChannelResolvable} publicUpdatesChannel The new community updates channel
* @param {TextChannelResolvable} publicUpdatesChannel The new community updates channel
* @param {string} [reason] Reason for changing the guild's community updates channel
* @returns {Promise<Guild>}
* @example
Expand Down Expand Up @@ -1160,7 +1182,7 @@ class Guild extends AnonymousGuild {
/**
* The data needed for updating a channel's position.
* @typedef {Object} ChannelPosition
* @property {ChannelResolvable} channel Channel to update
* @property {GuildChannel|Snowflake} channel Channel to update
* @property {number} [position] New position for the channel
* @property {CategoryChannelResolvable} [parent] Parent channel for this channel
* @property {boolean} [lockPermissions] If the overwrites should be locked to the parents overwrites
Expand Down
14 changes: 8 additions & 6 deletions src/structures/GuildChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ class GuildChannel extends Channel {
* @property {boolean} [nsfw] Whether the channel is NSFW
* @property {number} [bitrate] The bitrate of the voice channel
* @property {number} [userLimit] The user limit of the voice channel
* @property {?Snowflake} [parentId] The parent's id of the channel
* @property {?CategoryChannelResolvable} [parent] The parent of the channel
* @property {boolean} [lockPermissions]
* Lock the permissions of the channel to what the parent's permissions are
* @property {OverwriteResolvable[]|Collection<Snowflake, OverwriteResolvable>} [permissionOverwrites]
Expand All @@ -300,6 +300,8 @@ class GuildChannel extends Channel {
* .catch(console.error);
*/
async edit(data, reason) {
if (data.parent) data.parent = this.client.channels.resolveId(data.parent);

if (typeof data.position !== 'undefined') {
await Util.setPosition(
this,
Expand All @@ -323,8 +325,8 @@ class GuildChannel extends Channel {
}

if (data.lockPermissions) {
if (data.parentId) {
const newParent = this.guild.channels.resolve(data.parentId);
if (data.parent) {
const newParent = this.guild.channels.resolve(data.parent);
if (newParent?.type === 'GUILD_CATEGORY') {
permission_overwrites = newParent.permissionOverwrites.cache.map(o =>
PermissionOverwrites.resolve(o, this.guild),
Expand All @@ -346,7 +348,7 @@ class GuildChannel extends Channel {
bitrate: data.bitrate ?? this.bitrate,
user_limit: data.userLimit ?? this.userLimit,
rtc_region: data.rtcRegion ?? this.rtcRegion,
parent_id: data.parentId,
parent_id: data.parent,
lock_permissions: data.lockPermissions,
rate_limit_per_user: data.rateLimitPerUser,
default_auto_archive_duration: data.defaultAutoArchiveDuration,
Expand Down Expand Up @@ -382,7 +384,7 @@ class GuildChannel extends Channel {

/**
* Sets the parent of this channel.
* @param {?(CategoryChannel|Snowflake)} channel The category channel to set as parent
* @param {?CategoryChannelResolvable} channel The category channel to set as the parent
* @param {SetParentOptions} [options={}] The options for setting the parent
* @returns {Promise<GuildChannel>}
* @example
Expand All @@ -395,7 +397,7 @@ class GuildChannel extends Channel {
return this.edit(
{
// eslint-disable-next-line no-prototype-builtins
parentId: channel?.id ?? channel ?? null,
parent: channel ?? null,
lockPermissions,
},
reason,
Expand Down
4 changes: 2 additions & 2 deletions src/structures/GuildMember.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ class GuildMember extends Base {
* @property {Collection<Snowflake, Role>|RoleResolvable[]} [roles] The roles or role ids to apply
* @property {boolean} [mute] Whether or not the member should be muted
* @property {boolean} [deaf] Whether or not the member should be deafened
* @property {ChannelResolvable|null} [channel] Channel to move member to (if they are connected to voice), or `null`
* if you want to kick them from voice
* @property {GuildVoiceChannelResolvable|null} [channel] Channel to move member to (if they are connected to voice),
* or `null` if you want to disconnect them from voice
*/

/**
Expand Down
2 changes: 1 addition & 1 deletion src/structures/Role.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class Role extends Base {
/**
* Returns `channel.permissionsFor(role)`. Returns permissions for a role in a guild channel,
* taking into account permission overwrites.
* @param {ChannelResolvable} channel The guild channel to use as context
* @param {GuildChannel|Snowflake} channel The guild channel to use as context
* @returns {Readonly<Permissions>}
*/
permissionsIn(channel) {
Expand Down
4 changes: 2 additions & 2 deletions src/structures/VoiceState.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ class VoiceState extends Base {

/**
* Moves the member to a different channel, or disconnects them from the one they're in.
* @param {ChannelResolvable|null} channel Channel to move the member to, or `null` if you want to disconnect them
* from voice.
* @param {VoiceChannelResolvable|null} channel Channel to move the member to, or `null` if you want to disconnect
* them from voice.
* @param {string} [reason] Reason for moving member to another channel or disconnecting
* @returns {Promise<GuildMember>}
*/
Expand Down
5 changes: 2 additions & 3 deletions src/structures/Webhook.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const Channel = require('./Channel');
const MessagePayload = require('./MessagePayload');
const { Error } = require('../errors');
const { WebhookTypes } = require('../util/Constants');
Expand Down Expand Up @@ -214,7 +213,7 @@ class Webhook {
* @typedef {Object} WebhookEditData
* @property {string} [name=this.name] The new name for the webhook
* @property {BufferResolvable} [avatar] The new avatar for the webhook
* @property {ChannelResolvable} [channel] The new channel for the webhook
* @property {GuildTextChannelResolvable} [channel] The new channel for the webhook
*/

/**
Expand All @@ -227,7 +226,7 @@ class Webhook {
if (avatar && !(typeof avatar === 'string' && avatar.startsWith('data:'))) {
avatar = await DataResolver.resolveImage(avatar);
}
if (channel) channel = channel instanceof Channel ? channel.id : channel;
if (channel) channel = channel?.id ?? channel;
const data = await this.client.api.webhooks(this.id, channel ? undefined : this.token).patch({
data: { name, avatar, channel_id: channel },
reason,
Expand Down
38 changes: 22 additions & 16 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ export class Guild extends AnonymousGuild {
public fetchWidget(): Promise<Widget>;
public fetchWidgetSettings(): Promise<GuildWidgetSettings>;
public leave(): Promise<Guild>;
public setAFKChannel(afkChannel: ChannelResolvable | null, reason?: string): Promise<Guild>;
public setAFKChannel(afkChannel: VoiceChannelResolvable | null, reason?: string): Promise<Guild>;
public setAFKTimeout(afkTimeout: number, reason?: string): Promise<Guild>;
public setBanner(banner: Base64Resolvable | null, reason?: string): Promise<Guild>;
public setChannelPositions(channelPositions: readonly ChannelPosition[]): Promise<Guild>;
Expand All @@ -610,11 +610,11 @@ export class Guild extends AnonymousGuild {
public setName(name: string, reason?: string): Promise<Guild>;
public setOwner(owner: GuildMemberResolvable, reason?: string): Promise<Guild>;
public setPreferredLocale(preferredLocale: string, reason?: string): Promise<Guild>;
public setPublicUpdatesChannel(publicUpdatesChannel: ChannelResolvable | null, reason?: string): Promise<Guild>;
public setPublicUpdatesChannel(publicUpdatesChannel: TextChannelResolvable | null, reason?: string): Promise<Guild>;
public setRolePositions(rolePositions: readonly RolePosition[]): Promise<Guild>;
public setRulesChannel(rulesChannel: ChannelResolvable | null, reason?: string): Promise<Guild>;
public setRulesChannel(rulesChannel: TextChannelResolvable | null, reason?: string): Promise<Guild>;
public setSplash(splash: Base64Resolvable | null, reason?: string): Promise<Guild>;
public setSystemChannel(systemChannel: ChannelResolvable | null, reason?: string): Promise<Guild>;
public setSystemChannel(systemChannel: TextChannelResolvable | null, reason?: string): Promise<Guild>;
public setSystemChannelFlags(systemChannelFlags: SystemChannelFlagsResolvable, reason?: string): Promise<Guild>;
public setVerificationLevel(verificationLevel: VerificationLevel | number, reason?: string): Promise<Guild>;
public setWidgetSettings(settings: GuildWidgetSettingsData, reason?: string): Promise<Guild>;
Expand Down Expand Up @@ -705,7 +705,7 @@ export class GuildChannel extends Channel {
public permissionsFor(memberOrRole: GuildMember | Role): Readonly<Permissions>;
public permissionsFor(memberOrRole: GuildMemberResolvable | RoleResolvable): Readonly<Permissions> | null;
public setName(name: string, reason?: string): Promise<this>;
public setParent(channel: CategoryChannel | Snowflake | null, options?: SetParentOptions): Promise<this>;
public setParent(channel: CategoryChannelResolvable | null, options?: SetParentOptions): Promise<this>;
public setPosition(position: number, options?: SetChannelPositionOptions): Promise<this>;
public setTopic(topic: string | null, reason?: string): Promise<this>;
public isText(): this is TextChannel | NewsChannel;
Expand Down Expand Up @@ -1415,7 +1415,7 @@ export class Role extends Base {
public delete(reason?: string): Promise<Role>;
public edit(data: RoleData, reason?: string): Promise<Role>;
public equals(role: Role): boolean;
public permissionsIn(channel: ChannelResolvable): Readonly<Permissions>;
public permissionsIn(channel: GuildChannel | Snowflake): Readonly<Permissions>;
public setColor(color: ColorResolvable, reason?: string): Promise<Role>;
public setHoist(hoist: boolean, reason?: string): Promise<Role>;
public setMentionable(mentionable: boolean, reason?: string): Promise<Role>;
Expand Down Expand Up @@ -1885,7 +1885,7 @@ export class VoiceState extends Base {
public setDeaf(deaf: boolean, reason?: string): Promise<GuildMember>;
public setMute(mute: boolean, reason?: string): Promise<GuildMember>;
public kick(reason?: string): Promise<GuildMember>;
public setChannel(channel: ChannelResolvable | null, reason?: string): Promise<GuildMember>;
public setChannel(channel: VoiceChannelResolvable | null, reason?: string): Promise<GuildMember>;
public setRequestToSpeak(request: boolean): Promise<void>;
public setSuppressed(suppressed: boolean): Promise<void>;
}
Expand Down Expand Up @@ -2901,7 +2901,7 @@ export interface ChannelData {
nsfw?: boolean;
bitrate?: number;
userLimit?: number;
parentId?: Snowflake | null;
parent?: CategoryChannelResolvable | null;
rateLimitPerUser?: number;
lockPermissions?: boolean;
permissionOverwrites?: readonly OverwriteResolvable[] | Collection<Snowflake, OverwriteResolvable>;
Expand All @@ -2919,7 +2919,7 @@ export interface ChannelLogsQueryOptions {
export type ChannelMention = `<#${Snowflake}>`;

export interface ChannelPosition {
channel: ChannelResolvable;
channel: GuildChannel | Snowflake;
lockPermissions?: boolean;
parent?: CategoryChannelResolvable | null;
position?: number;
Expand Down Expand Up @@ -3517,7 +3517,7 @@ export interface GuildChannelCreateOptions {
| ChannelTypes.GUILD_PRIVATE_THREAD
>;
nsfw?: boolean;
parent?: ChannelResolvable;
parent?: CategoryChannelResolvable;
bitrate?: number;
userLimit?: number;
rateLimitPerUser?: number;
Expand Down Expand Up @@ -3557,17 +3557,17 @@ export interface GuildEditData {
verificationLevel?: VerificationLevel | number;
explicitContentFilter?: ExplicitContentFilterLevel | number;
defaultMessageNotifications?: DefaultMessageNotificationLevel | number;
afkChannel?: ChannelResolvable;
systemChannel?: ChannelResolvable;
afkChannel?: VoiceChannelResolvable;
systemChannel?: TextChannelResolvable;
systemChannelFlags?: SystemChannelFlagsResolvable;
afkTimeout?: number;
icon?: Base64Resolvable;
owner?: GuildMemberResolvable;
splash?: Base64Resolvable;
discoverySplash?: Base64Resolvable;
banner?: Base64Resolvable;
rulesChannel?: ChannelResolvable;
publicUpdatesChannel?: ChannelResolvable;
rulesChannel?: TextChannelResolvable;
publicUpdatesChannel?: TextChannelResolvable;
preferredLocale?: string;
description?: string | null;
features?: GuildFeatures[];
Expand Down Expand Up @@ -3623,7 +3623,7 @@ export interface GuildMemberEditData {
roles?: Collection<Snowflake, Role> | readonly RoleResolvable[];
mute?: boolean;
deaf?: boolean;
channel?: ChannelResolvable | null;
channel?: GuildVoiceChannelResolvable | null;
}

export type GuildMemberResolvable = GuildMember | UserResolvable;
Expand Down Expand Up @@ -3651,6 +3651,8 @@ export interface GuildSearchMembersOptions {

export type GuildTemplateResolvable = string;

export type GuildVoiceChannelResolvable = VoiceChannel | StageChannel | Snowflake;

export type HexColorString = `#${string}`;

export interface HTTPAttachmentData {
Expand Down Expand Up @@ -4310,6 +4312,8 @@ export type TextBasedChannelTypes =
| 'GUILD_PUBLIC_THREAD'
| 'GUILD_PRIVATE_THREAD';

export type TextChannelResolvable = Snowflake | TextChannel;

export type ThreadAutoArchiveDuration = 60 | 1440 | 4320 | 10080;

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

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

export type VoiceChannelResolvable = Snowflake | VoiceChannel;

export type WebhookClientData = WebhookClientDataIdWithToken | WebhookClientDataURL;

export interface WebhookClientDataIdWithToken {
Expand All @@ -4383,7 +4389,7 @@ export type WebhookClientOptions = Pick<
export interface WebhookEditData {
name?: string;
avatar?: BufferResolvable;
channel?: ChannelResolvable;
channel?: GuildTextChannelResolvable;
}

export type WebhookEditMessageOptions = Pick<
Expand Down

0 comments on commit 65ae064

Please sign in to comment.