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

refactor(Guild): Destructure object in guild editing #8971

Merged
merged 6 commits into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
114 changes: 60 additions & 54 deletions packages/discord.js/src/structures/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -740,27 +740,26 @@ class Guild extends AnonymousGuild {
* @typedef {Object} GuildEditOptions
* @property {string} [name] The name of the guild
* @property {?GuildVerificationLevel} [verificationLevel] The verification level of the guild
* @property {?GuildDefaultMessageNotifications} [defaultMessageNotifications] The default message
* notification level of the guild
* @property {?GuildExplicitContentFilter} [explicitContentFilter] The level of the explicit content filter
* @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 {?(BufferResolvable|Base64Resolvable)} [icon] The icon of the guild
* @property {GuildMemberResolvable} [owner] The owner of the guild
* @property {?(BufferResolvable|Base64Resolvable)} [splash] The invite splash image of the guild
* @property {?(BufferResolvable|Base64Resolvable)} [discoverySplash] The discovery splash image of the guild
* @property {?(BufferResolvable|Base64Resolvable)} [banner] The banner of the guild
* @property {?GuildDefaultMessageNotifications} [defaultMessageNotifications] The default message
* notification level of the guild
* @property {?TextChannelResolvable} [systemChannel] The system channel of the guild
* @property {SystemChannelFlagsResolvable} [systemChannelFlags] The system channel flags 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 {boolean} [premiumProgressBarEnabled] Whether the guild's premium progress bar is enabled
* @property {?string} [description] The discovery description of the guild
* @property {GuildFeature[]} [features] The features of the guild
* @property {?string} [description] The discovery description of the guild
* @property {boolean} [premiumProgressBarEnabled] Whether the guild's premium progress bar is enabled
* @property {string} [reason] Reason for editing this guild
*/
/* eslint-enable max-len */

/**
* Data that can be resolved to a Text Channel object. This can be:
Expand Down Expand Up @@ -788,51 +787,59 @@ class Guild extends AnonymousGuild {
* .then(updated => console.log(`New guild name ${updated}`))
* .catch(console.error);
*/
async edit(options) {
const _data = {};
if (options.name) _data.name = options.name;
if (typeof options.verificationLevel !== 'undefined') {
_data.verification_level = options.verificationLevel;
}
if (typeof options.afkChannel !== 'undefined') {
_data.afk_channel_id = this.client.channels.resolveId(options.afkChannel);
}
if (typeof options.systemChannel !== 'undefined') {
_data.system_channel_id = this.client.channels.resolveId(options.systemChannel);
}
if (options.afkTimeout) _data.afk_timeout = Number(options.afkTimeout);
if (typeof options.icon !== 'undefined') _data.icon = await DataResolver.resolveImage(options.icon);
if (options.owner) _data.owner_id = this.client.users.resolveId(options.owner);
if (typeof options.splash !== 'undefined') _data.splash = await DataResolver.resolveImage(options.splash);
if (typeof options.discoverySplash !== 'undefined') {
_data.discovery_splash = await DataResolver.resolveImage(options.discoverySplash);
}
if (typeof options.banner !== 'undefined') _data.banner = await DataResolver.resolveImage(options.banner);
if (typeof options.explicitContentFilter !== 'undefined') {
_data.explicit_content_filter = options.explicitContentFilter;
}
if (typeof options.defaultMessageNotifications !== 'undefined') {
_data.default_message_notifications = options.defaultMessageNotifications;
}
if (typeof options.systemChannelFlags !== 'undefined') {
_data.system_channel_flags = SystemChannelFlagsBitField.resolve(options.systemChannelFlags);
}
if (typeof options.rulesChannel !== 'undefined') {
_data.rules_channel_id = this.client.channels.resolveId(options.rulesChannel);
}
if (typeof options.publicUpdatesChannel !== 'undefined') {
_data.public_updates_channel_id = this.client.channels.resolveId(options.publicUpdatesChannel);
}
if (typeof options.features !== 'undefined') {
_data.features = options.features;
}
if (typeof options.description !== 'undefined') {
_data.description = options.description;
}
if (typeof options.preferredLocale !== 'undefined') _data.preferred_locale = options.preferredLocale;
if ('premiumProgressBarEnabled' in options) _data.premium_progress_bar_enabled = options.premiumProgressBarEnabled;
const newData = await this.client.rest.patch(Routes.guild(this.id), { body: _data, reason: options.reason });
return this.client.actions.GuildUpdate.handle(newData).updated;
async edit({
name,
verificationLevel,
defaultMessageNotifications,
explicitContentFilter,
afkChannel,
afkTimeout,
icon,
owner,
splash,
discoverySplash,
banner,
systemChannel,
systemChannelFlags,
rulesChannel,
publicUpdatesChannel,
preferredLocale,
features,
description,
premiumProgressBarEnabled,
reason,
Jiralite marked this conversation as resolved.
Show resolved Hide resolved
}) {
const {
client: { actions, channels, rest, users },
id,
} = this;
Jiralite marked this conversation as resolved.
Show resolved Hide resolved

const newData = await rest.patch(Routes.guild(id), {
body: {
name,
verification_level: verificationLevel,
default_message_notifications: defaultMessageNotifications,
explicit_content_filter: explicitContentFilter,
afk_channel_id: afkChannel && channels.resolveId(afkChannel),
afk_timeout: afkTimeout,
icon: icon && (await DataResolver.resolveImage(icon)),
owner_id: owner && users.resolveId(owner),
splash: splash && (await DataResolver.resolveImage(splash)),
discovery_splash: discoverySplash && (await DataResolver.resolveImage(discoverySplash)),
banner: banner && (await DataResolver.resolveImage(banner)),
system_channel_id: systemChannel && channels.resolveId(systemChannel),
system_channel_flags: systemChannelFlags && SystemChannelFlagsBitField.resolve(systemChannelFlags),
rules_channel_id: rulesChannel && channels.resolveId(rulesChannel),
public_updates_channel_id: publicUpdatesChannel && channels.resolveId(publicUpdatesChannel),
preferred_locale: preferredLocale,
features,
description,
premium_progress_bar_enabled: premiumProgressBarEnabled,
},
reason,
});

return actions.GuildUpdate.handle(newData).updated;
}

/**
Expand Down Expand Up @@ -905,7 +912,6 @@ class Guild extends AnonymousGuild {
return new WelcomeScreen(this, patchData);
}

/* eslint-disable max-len */
/**
* Edits the level of the explicit content filter.
* @param {?GuildExplicitContentFilter} explicitContentFilter The new level of the explicit content filter
Expand All @@ -918,14 +924,14 @@ class Guild extends AnonymousGuild {

/**
* Edits the setting of the default message notifications of the guild.
* @param {?GuildDefaultMessageNotifications} defaultMessageNotifications The new default message notification level of the guild
* @param {?GuildDefaultMessageNotifications} defaultMessageNotifications
* The new default message notification level of the guild
* @param {string} [reason] Reason for changing the setting of the default message notifications
* @returns {Promise<Guild>}
*/
setDefaultMessageNotifications(defaultMessageNotifications, reason) {
return this.edit({ defaultMessageNotifications, reason });
}
/* eslint-enable max-len */

/**
* Edits the flags of the default message notifications of the guild.
Expand Down
12 changes: 6 additions & 6 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5423,23 +5423,23 @@ export interface GuildWidgetSettings {
export interface GuildEditOptions {
name?: string;
verificationLevel?: GuildVerificationLevel | null;
explicitContentFilter?: GuildExplicitContentFilter | null;
defaultMessageNotifications?: GuildDefaultMessageNotifications | null;
afkChannel?: VoiceChannelResolvable | null;
systemChannel?: TextChannelResolvable | null;
systemChannelFlags?: SystemChannelFlagsResolvable;
explicitContentFilter?: GuildExplicitContentFilter | null;
afkTimeout?: number;
afkChannel?: VoiceChannelResolvable | null;
icon?: BufferResolvable | Base64Resolvable | null;
owner?: GuildMemberResolvable;
splash?: BufferResolvable | Base64Resolvable | null;
discoverySplash?: BufferResolvable | Base64Resolvable | null;
banner?: BufferResolvable | Base64Resolvable | null;
systemChannel?: TextChannelResolvable | null;
systemChannelFlags?: SystemChannelFlagsResolvable;
rulesChannel?: TextChannelResolvable | null;
publicUpdatesChannel?: TextChannelResolvable | null;
preferredLocale?: Locale | null;
premiumProgressBarEnabled?: boolean;
description?: string | null;
features?: `${GuildFeature}`[];
description?: string | null;
premiumProgressBarEnabled?: boolean;
reason?: string;
}

Expand Down