Skip to content

Commit

Permalink
refactor: use consistent naming for options (#8901)
Browse files Browse the repository at this point in the history
* refactor: use consistent naming for options

* chore: update param names in typings

* chore: update forgotten `data` param

* Update packages/discord.js/src/structures/Guild.js

Co-authored-by: SpaceEEC <spaceeec@yahoo.com>

Co-authored-by: SpaceEEC <spaceeec@yahoo.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Dec 14, 2022
1 parent 1b151db commit a7b55c1
Show file tree
Hide file tree
Showing 27 changed files with 223 additions and 221 deletions.
4 changes: 2 additions & 2 deletions packages/discord.js/src/client/WebhookClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class WebhookClient extends BaseClient {
/* eslint-disable no-empty-function, valid-jsdoc */
/**
* Sends a message with this webhook.
* @param {string|MessagePayload|WebhookCreateMessageOptions} options The content for the reply
* @param {string|MessagePayload|WebhookMessageCreateOptions} options The content for the reply
* @returns {Promise<APIMessage>}
*/
send() {}
Expand All @@ -84,7 +84,7 @@ class WebhookClient extends BaseClient {
/**
* Edits a message that was sent by this webhook.
* @param {MessageResolvable} message The message to edit
* @param {string|MessagePayload|WebhookEditMessageOptions} options The options to provide
* @param {string|MessagePayload|WebhookMessageEditOptions} options The options to provide
* @returns {Promise<APIMessage>} Returns the message edited by this webhook
*/
editMessage() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ class ApplicationCommandPermissionsManager extends BaseManager {
* Options used to set permissions for one or more Application Commands in a guild
* <warn>Omitting the `command` parameter edits the guild wide permissions
* when the manager's `commandId` is `null`</warn>
* @typedef {BaseApplicationCommandPermissionsOptions} EditApplicationCommandPermissionsOptions
* @typedef {BaseApplicationCommandPermissionsOptions} ApplicationCommandPermissionsEditOptions
* @property {ApplicationCommandPermissions[]} permissions The new permissions for the guild or overwrite
* @property {string} token The bearer token to use that authorizes the permission edit
*/

/**
* Sets the permissions for the guild or a command overwrite.
* @param {EditApplicationCommandPermissionsOptions} options Options used to set permissions
* @param {ApplicationCommandPermissionsEditOptions} options Options used to set permissions
* @returns {Promise<ApplicationCommandPermissions[]|Collection<Snowflake, ApplicationCommandPermissions[]>>}
* @example
* // Set a permission overwrite for a command
Expand Down Expand Up @@ -179,7 +179,7 @@ class ApplicationCommandPermissionsManager extends BaseManager {

/**
* Add permissions to a command.
* @param {EditApplicationCommandPermissionsOptions} options Options used to add permissions
* @param {ApplicationCommandPermissionsEditOptions} options Options used to add permissions
* @returns {Promise<ApplicationCommandPermissions[]>}
* @example
* // Add a rule to block a role from using a command
Expand Down
49 changes: 25 additions & 24 deletions packages/discord.js/src/managers/GuildChannelManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,27 +258,27 @@ class GuildChannelManager extends CachedManager {
/**
* Edits the channel.
* @param {GuildChannelResolvable} channel The channel to edit
* @param {GuildChannelEditOptions} data Options for editing the channel
* @param {GuildChannelEditOptions} options Options for editing the channel
* @returns {Promise<GuildChannel>}
* @example
* // Edit a channel
* guild.channels.edit('222197033908436994', { name: 'new-channel' })
* .then(console.log)
* .catch(console.error);
*/
async edit(channel, data) {
async edit(channel, options) {
channel = this.resolve(channel);
if (!channel) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'channel', 'GuildChannelResolvable');

const parent = data.parent && this.client.channels.resolveId(data.parent);
const parent = options.parent && this.client.channels.resolveId(options.parent);

if (typeof data.position !== 'undefined') {
await this.setPosition(channel, data.position, { position: data.position, reason: data.reason });
if (typeof options.position !== 'undefined') {
await this.setPosition(channel, options.position, { position: options.position, reason: options.reason });
}

let permission_overwrites = data.permissionOverwrites?.map(o => PermissionOverwrites.resolve(o, this.guild));
let permission_overwrites = options.permissionOverwrites?.map(o => PermissionOverwrites.resolve(o, this.guild));

if (data.lockPermissions) {
if (options.lockPermissions) {
if (parent) {
const newParent = this.guild.channels.resolve(parent);
if (newParent?.type === ChannelType.GuildCategory) {
Expand All @@ -295,26 +295,27 @@ class GuildChannelManager extends CachedManager {

const newData = await this.client.rest.patch(Routes.channel(channel.id), {
body: {
name: (data.name ?? channel.name).trim(),
type: data.type,
topic: data.topic,
nsfw: data.nsfw,
bitrate: data.bitrate ?? channel.bitrate,
user_limit: data.userLimit ?? channel.userLimit,
rtc_region: 'rtcRegion' in data ? data.rtcRegion : channel.rtcRegion,
video_quality_mode: data.videoQualityMode,
name: (options.name ?? channel.name).trim(),
type: options.type,
topic: options.topic,
nsfw: options.nsfw,
bitrate: options.bitrate ?? channel.bitrate,
user_limit: options.userLimit ?? channel.userLimit,
rtc_region: 'rtcRegion' in options ? options.rtcRegion : channel.rtcRegion,
video_quality_mode: options.videoQualityMode,
parent_id: parent,
lock_permissions: data.lockPermissions,
rate_limit_per_user: data.rateLimitPerUser,
default_auto_archive_duration: data.defaultAutoArchiveDuration,
lock_permissions: options.lockPermissions,
rate_limit_per_user: options.rateLimitPerUser,
default_auto_archive_duration: options.defaultAutoArchiveDuration,
permission_overwrites,
available_tags: data.availableTags?.map(availableTag => transformGuildForumTag(availableTag)),
default_reaction_emoji: data.defaultReactionEmoji && transformGuildDefaultReaction(data.defaultReactionEmoji),
default_thread_rate_limit_per_user: data.defaultThreadRateLimitPerUser,
flags: 'flags' in data ? ChannelFlagsBitField.resolve(data.flags) : undefined,
default_sort_order: data.defaultSortOrder,
available_tags: options.availableTags?.map(availableTag => transformGuildForumTag(availableTag)),
default_reaction_emoji:
options.defaultReactionEmoji && transformGuildDefaultReaction(options.defaultReactionEmoji),
default_thread_rate_limit_per_user: options.defaultThreadRateLimitPerUser,
flags: 'flags' in options ? ChannelFlagsBitField.resolve(options.flags) : undefined,
default_sort_order: options.defaultSortOrder,
},
reason: data.reason,
reason: options.reason,
});

return this.client.actions.ChannelUpdate.handle(newData).updated;
Expand Down
10 changes: 5 additions & 5 deletions packages/discord.js/src/managers/GuildEmojiManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,19 @@ class GuildEmojiManager extends BaseGuildEmojiManager {
/**
* Edits an emoji.
* @param {EmojiResolvable} emoji The Emoji resolvable to edit
* @param {GuildEmojiEditData} data The new data for the emoji
* @param {GuildEmojiEditOptions} options The options to provide
* @returns {Promise<GuildEmoji>}
*/
async edit(emoji, data) {
async edit(emoji, options) {
const id = this.resolveId(emoji);
if (!id) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'emoji', 'EmojiResolvable', true);
const roles = data.roles?.map(r => this.guild.roles.resolveId(r));
const roles = options.roles?.map(r => this.guild.roles.resolveId(r));
const newData = await this.client.rest.patch(Routes.guildEmoji(this.guild.id, id), {
body: {
name: data.name,
name: options.name,
roles,
},
reason: data.reason,
reason: options.reason,
});
const existing = this.cache.get(id);
if (existing) {
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/src/managers/GuildInviteManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class GuildInviteManager extends CachedManager {
/**
* Create an invite to the guild from the provided channel.
* @param {GuildInvitableChannelResolvable} channel The options for creating the invite from a channel.
* @param {CreateInviteOptions} [options={}] The options for creating the invite from a channel.
* @param {InviteCreateOptions} [options={}] The options for creating the invite from a channel.
* @returns {Promise<Invite>}
* @example
* // Create an invite to a selected channel
Expand Down
38 changes: 19 additions & 19 deletions packages/discord.js/src/managers/GuildMemberManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class GuildMemberManager extends CachedManager {

/**
* The data for editing a guild member.
* @typedef {Object} GuildMemberEditData
* @typedef {Object} GuildMemberEditOptions
* @property {?string} [nick] The nickname to set for the member
* @property {Collection<Snowflake, Role>|RoleResolvable[]} [roles] The roles or role ids to apply
* @property {boolean} [mute] Whether or not the member should be muted
Expand All @@ -286,43 +286,43 @@ class GuildMemberManager extends CachedManager {
* Edits a member of the guild.
* <info>The user must be a member of the guild</info>
* @param {UserResolvable} user The member to edit
* @param {GuildMemberEditData} data The data to edit the member with
* @param {GuildMemberEditOptions} options The options to provide
* @returns {Promise<GuildMember>}
*/
async edit(user, { reason, ...data }) {
async edit(user, { reason, ...options }) {
const id = this.client.users.resolveId(user);
if (!id) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'user', 'UserResolvable');

if (data.channel) {
data.channel = this.guild.channels.resolve(data.channel);
if (!(data.channel instanceof BaseGuildVoiceChannel)) {
if (options.channel) {
options.channel = this.guild.channels.resolve(options.channel);
if (!(options.channel instanceof BaseGuildVoiceChannel)) {
throw new DiscordjsError(ErrorCodes.GuildVoiceChannelResolve);
}
data.channel_id = data.channel.id;
data.channel = undefined;
} else if (data.channel === null) {
data.channel_id = null;
data.channel = undefined;
options.channel_id = options.channel.id;
options.channel = undefined;
} else if (options.channel === null) {
options.channel_id = null;
options.channel = undefined;
}
data.roles &&= data.roles.map(role => (role instanceof Role ? role.id : role));
options.roles &&= options.roles.map(role => (role instanceof Role ? role.id : role));

if (typeof data.communicationDisabledUntil !== 'undefined') {
data.communication_disabled_until =
if (typeof options.communicationDisabledUntil !== 'undefined') {
options.communication_disabled_until =
// eslint-disable-next-line eqeqeq
data.communicationDisabledUntil != null
? new Date(data.communicationDisabledUntil).toISOString()
: data.communicationDisabledUntil;
options.communicationDisabledUntil != null
? new Date(options.communicationDisabledUntil).toISOString()
: options.communicationDisabledUntil;
}

let endpoint;
if (id === this.client.user.id) {
const keys = Object.keys(data);
const keys = Object.keys(options);
if (keys.length === 1 && keys[0] === 'nick') endpoint = Routes.guildMember(this.guild.id);
else endpoint = Routes.guildMember(this.guild.id, id);
} else {
endpoint = Routes.guildMember(this.guild.id, id);
}
const d = await this.client.rest.patch(endpoint, { body: data, reason });
const d = await this.client.rest.patch(endpoint, { body: options, reason });

const clone = this.cache.get(id)?._clone();
clone?._patch(d);
Expand Down
8 changes: 4 additions & 4 deletions packages/discord.js/src/managers/GuildStickerManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,16 @@ class GuildStickerManager extends CachedManager {
/**
* Edits a sticker.
* @param {StickerResolvable} sticker The sticker to edit
* @param {GuildStickerEditData} [data={}] The new data for the sticker
* @param {GuildStickerEditOptions} [options={}] The new data for the sticker
* @returns {Promise<Sticker>}
*/
async edit(sticker, data = {}) {
async edit(sticker, options = {}) {
const stickerId = this.resolveId(sticker);
if (!stickerId) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'sticker', 'StickerResolvable');

const d = await this.client.rest.patch(Routes.guildSticker(this.guild.id, stickerId), {
body: data,
reason: data.reason,
body: options,
reason: options.reason,
});

const existing = this.cache.get(stickerId);
Expand Down
31 changes: 16 additions & 15 deletions packages/discord.js/src/managers/RoleManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class RoleManager extends CachedManager {

/**
* Options used to create a new role.
* @typedef {Object} CreateRoleOptions
* @typedef {Object} RoleCreateOptions
* @property {string} [name] The name of the new role
* @property {ColorResolvable} [color] The data to create the role with
* @property {boolean} [hoist] Whether or not the new role should be hoisted
Expand All @@ -117,7 +117,7 @@ class RoleManager extends CachedManager {
/**
* Creates a new role in the guild with given information.
* <warn>The position will silently reset to 1 if an invalid one is provided, or none.</warn>
* @param {CreateRoleOptions} [options] Options for creating the new role
* @param {RoleCreateOptions} [options] Options for creating the new role
* @returns {Promise<Role>}
* @example
* // Create a new role
Expand Down Expand Up @@ -166,47 +166,48 @@ class RoleManager extends CachedManager {

/**
* Options for editing a role
* @typedef {RoleData} EditRoleOptions
* @typedef {RoleData} RoleEditOptions
* @property {string} [reason] The reason for editing this role
*/

/**
* Edits a role of the guild.
* @param {RoleResolvable} role The role to edit
* @param {EditRoleOptions} data The new data for the role
* @param {RoleEditOptions} options The options to provide
* @returns {Promise<Role>}
* @example
* // Edit a role
* guild.roles.edit('222079219327434752', { name: 'buddies' })
* .then(updated => console.log(`Edited role name to ${updated.name}`))
* .catch(console.error);
*/
async edit(role, data) {
async edit(role, options) {
role = this.resolve(role);
if (!role) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'role', 'RoleResolvable');

if (typeof data.position === 'number') {
await this.setPosition(role, data.position, { reason: data.reason });
if (typeof options.position === 'number') {
await this.setPosition(role, options.position, { reason: options.reason });
}

let icon = data.icon;
let icon = options.icon;
if (icon) {
const guildEmojiURL = this.guild.emojis.resolve(icon)?.url;
icon = guildEmojiURL ? await DataResolver.resolveImage(guildEmojiURL) : await DataResolver.resolveImage(icon);
if (typeof icon !== 'string') icon = undefined;
}

const body = {
name: data.name,
color: typeof data.color === 'undefined' ? undefined : resolveColor(data.color),
hoist: data.hoist,
permissions: typeof data.permissions === 'undefined' ? undefined : new PermissionsBitField(data.permissions),
mentionable: data.mentionable,
name: options.name,
color: typeof options.color === 'undefined' ? undefined : resolveColor(options.color),
hoist: options.hoist,
permissions:
typeof options.permissions === 'undefined' ? undefined : new PermissionsBitField(options.permissions),
mentionable: options.mentionable,
icon,
unicode_emoji: data.unicodeEmoji,
unicode_emoji: options.unicodeEmoji,
};

const d = await this.client.rest.patch(Routes.guildRole(this.guild.id, role.id), { body, reason: data.reason });
const d = await this.client.rest.patch(Routes.guildRole(this.guild.id, role.id), { body, reason: options.reason });

const clone = role._clone();
clone._patch(d);
Expand Down
4 changes: 2 additions & 2 deletions packages/discord.js/src/structures/BaseGuildTextChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class BaseGuildTextChannel extends GuildChannel {

/**
* Options used to create an invite to a guild channel.
* @typedef {Object} CreateInviteOptions
* @typedef {Object} InviteCreateOptions
* @property {boolean} [temporary] Whether members that joined via the invite should be automatically
* kicked after 24 hours if they have not yet received a role
* @property {number} [maxAge] How long the invite should last (in seconds, 0 for forever)
Expand All @@ -142,7 +142,7 @@ class BaseGuildTextChannel extends GuildChannel {

/**
* Creates an invite to this guild channel.
* @param {CreateInviteOptions} [options={}] The options for creating the invite
* @param {InviteCreateOptions} [options={}] The options for creating the invite
* @returns {Promise<Invite>}
* @example
* // Create an invite to a channel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class BaseGuildVoiceChannel extends GuildChannel {

/**
* Creates an invite to this guild channel.
* @param {CreateInviteOptions} [options={}] The options for creating the invite
* @param {InviteCreateOptions} [options={}] The options for creating the invite
* @returns {Promise<Invite>}
* @example
* // Create an invite to a channel
Expand Down
10 changes: 5 additions & 5 deletions packages/discord.js/src/structures/ClientUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ class ClientUser extends User {

/**
* Data used to edit the logged in client
* @typedef {Object} ClientUserEditData
* @typedef {Object} ClientUserEditOptions
* @property {string} [username] The new username
* @property {?(BufferResolvable|Base64Resolvable)} [avatar] The new avatar
*/

/**
* Edits the logged in client.
* @param {ClientUserEditData} data The new data
* @param {ClientUserEditOptions} options The options to provide
* @returns {Promise<ClientUser>}
*/
async edit(data) {
if (typeof data.avatar !== 'undefined') data.avatar = await DataResolver.resolveImage(data.avatar);
const newData = await this.client.rest.patch(Routes.user(), { body: data });
async edit(options) {
if (typeof options.avatar !== 'undefined') options.avatar = await DataResolver.resolveImage(options.avatar);
const newData = await this.client.rest.patch(Routes.user(), { body: options });
this.client.token = newData.token;
this.client.rest.setToken(newData.token);
const { updated } = this.client.actions.UserUpdate.handle(newData);
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/src/structures/ForumChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class ForumChannel extends GuildChannel {

/**
* Creates an invite to this guild channel.
* @param {CreateInviteOptions} [options={}] The options for creating the invite
* @param {InviteCreateOptions} [options={}] The options for creating the invite
* @returns {Promise<Invite>}
* @example
* // Create an invite to a channel
Expand Down

0 comments on commit a7b55c1

Please sign in to comment.