From b7ee17ef38c5a4cbb864e76fd07c67d17bb3bd22 Mon Sep 17 00:00:00 2001 From: almeidx Date: Thu, 22 Dec 2022 01:23:15 +0000 Subject: [PATCH 1/4] feat(Guild): add safety alerts channel and mention raid protection --- .../src/managers/AutoModerationRuleManager.js | 3 ++ .../src/structures/AutoModerationRule.js | 13 +++++++ packages/discord.js/src/structures/Guild.js | 36 +++++++++++++++++++ packages/discord.js/typings/index.d.ts | 9 +++++ 4 files changed, 61 insertions(+) diff --git a/packages/discord.js/src/managers/AutoModerationRuleManager.js b/packages/discord.js/src/managers/AutoModerationRuleManager.js index b212babb064f..c6f6aa55c211 100644 --- a/packages/discord.js/src/managers/AutoModerationRuleManager.js +++ b/packages/discord.js/src/managers/AutoModerationRuleManager.js @@ -59,6 +59,7 @@ class AutoModerationRuleManager extends CachedManager { * @property {string[]} [allowList] The substrings that will be exempt from triggering * {@link AutoModerationRuleTriggerType.Keyword} and {@link AutoModerationRuleTriggerType.KeywordPreset} * @property {?number} [mentionTotalLimit] The total number of role & user mentions allowed per message + * @property {boolean} [mentionRaidProtectionEnabled] Whether to automatically detect mention raids */ /** @@ -124,6 +125,7 @@ class AutoModerationRuleManager extends CachedManager { presets: triggerMetadata.presets, allow_list: triggerMetadata.allowList, mention_total_limit: triggerMetadata.mentionTotalLimit, + mention_raid_protection_enabled: triggerMetadata.mentionRaidProtectionEnabled, }, actions: actions.map(action => ({ type: action.type, @@ -180,6 +182,7 @@ class AutoModerationRuleManager extends CachedManager { presets: triggerMetadata.presets, allow_list: triggerMetadata.allowList, mention_total_limit: triggerMetadata.mentionTotalLimit, + mention_raid_protection_enabled: triggerMetadata.mentionRaidProtectionEnabled, }, actions: actions?.map(action => ({ type: action.type, diff --git a/packages/discord.js/src/structures/AutoModerationRule.js b/packages/discord.js/src/structures/AutoModerationRule.js index 4f981b18b01f..d9143e031452 100644 --- a/packages/discord.js/src/structures/AutoModerationRule.js +++ b/packages/discord.js/src/structures/AutoModerationRule.js @@ -67,6 +67,7 @@ class AutoModerationRule extends Base { * @property {string[]} allowList The substrings that will be exempt from triggering * {@link AutoModerationRuleTriggerType.Keyword} and {@link AutoModerationRuleTriggerType.KeywordPreset} * @property {?number} mentionTotalLimit The total number of role & user mentions allowed per message + * @property {boolean} mentionRaidProtectionEnabled Whether mention raid protection is enabled */ /** @@ -79,6 +80,7 @@ class AutoModerationRule extends Base { presets: data.trigger_metadata.presets ?? [], allowList: data.trigger_metadata.allow_list ?? [], mentionTotalLimit: data.trigger_metadata.mention_total_limit ?? null, + mentionRaidProtectionEnabled: data.trigger_metadata.mention_raid_protection_enabled ?? false, }; } @@ -228,6 +230,17 @@ class AutoModerationRule extends Base { return this.edit({ triggerMetadata: { ...this.triggerMetadata, mentionTotalLimit }, reason }); } + /** + * Sets whether to enable mention raid protection for this auto moderation rule. + * @param {boolean} mentionRaidProtectionEnabled + * Whether to enable mention raid protection for this auto moderation rule + * @param {string} [reason] The reason for changing the mention raid protection of this auto moderation rule + * @returns {Promise} + */ + setMentionRaidProtectionEnabled(mentionRaidProtectionEnabled, reason) { + return this.edit({ triggerMetadata: { mentionRaidProtectionEnabled }, reason }); + } + /** * Sets the actions for this auto moderation rule. * @param {AutoModerationActionOptions[]} actions The actions of this auto moderation rule diff --git a/packages/discord.js/src/structures/Guild.js b/packages/discord.js/src/structures/Guild.js index e0e5d1c6ddef..fbbc21a99773 100644 --- a/packages/discord.js/src/structures/Guild.js +++ b/packages/discord.js/src/structures/Guild.js @@ -361,6 +361,16 @@ class Guild extends AnonymousGuild { this.preferredLocale = data.preferred_locale; } + if ('safety_alerts_channel_id' in data) { + /** + * The safety alerts channel's id for the guild + * @type {?Snowflake} + */ + this.safetyAlertsChannelId = data.safety_alerts_channel_id; + } else { + this.safetyAlertsChannelId ??= null; + } + if (data.channels) { this.channels.cache.clear(); for (const rawChannel of data.channels) { @@ -525,6 +535,15 @@ class Guild extends AnonymousGuild { return this.client.channels.resolve(this.publicUpdatesChannelId); } + /** + * Safety alerts channel for this guild + * @type {?TextChannel} + * @readonly + */ + get safetyAlertsChannel() { + return this.client.channels.resolve(this.safetyAlertsChannelId); + } + /** * The maximum bitrate available for this guild * @type {number} @@ -754,6 +773,7 @@ class Guild extends AnonymousGuild { * @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 {?TextChannelResolvable} [safetyAlertsChannel] The safety alerts channel of the guild * @property {?string} [preferredLocale] The preferred locale of the guild * @property {GuildFeature[]} [features] The features of the guild * @property {?string} [description] The discovery description of the guild @@ -804,6 +824,7 @@ class Guild extends AnonymousGuild { publicUpdatesChannel, preferredLocale, premiumProgressBarEnabled, + safetyAlertsChannel, ...options }) { const data = await this.client.rest.patch(Routes.guild(this.id), { @@ -828,6 +849,7 @@ class Guild extends AnonymousGuild { public_updates_channel_id: publicUpdatesChannel && this.client.channels.resolveId(publicUpdatesChannel), preferred_locale: preferredLocale, premium_progress_bar_enabled: premiumProgressBarEnabled, + safety_alerts_channel_id: safetyAlertsChannel && this.client.channels.resolveId(safetyAlertsChannel), }, reason: options.reason, }); @@ -1141,6 +1163,20 @@ class Guild extends AnonymousGuild { return this.edit({ premiumProgressBarEnabled: enabled, reason }); } + /** + * Edits the safety alerts channel of the guild. + * @param {?TextChannelResolvable} safetyAlertsChannel The new safety alerts channel + * @param {string} [reason] Reason for changing the guild's safety alerts channel + * @returns {Promise} + * // Edit the guild safety alerts channel + * guild.setSafetyAlertsChannel(channel) + * .then(updated => console.log(`Updated guild safety alerts channel to ${guild.safetyAlertsChannel.name}`)) + * .catch(console.error); + */ + setSafetyAlertsChannel(safetyAlertsChannel, reason) { + return this.edit({ safetyAlertsChannel, reason }); + } + /** * Edits the guild's widget settings. * @param {GuildWidgetSettingsData} settings The widget settings for the guild diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 985fe851c45a..4721eb149109 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -370,6 +370,10 @@ export class AutoModerationRule extends Base { public setPresets(presets: AutoModerationRuleKeywordPresetType[], reason?: string): Promise; public setAllowList(allowList: string[], reason?: string): Promise; public setMentionTotalLimit(mentionTotalLimit: number, reason?: string): Promise; + public setMentionRaidProtectionEnabled( + mentionRaidProtectionEnabled: boolean, + reason?: string, + ): Promise; public setActions(actions: AutoModerationActionOptions[], reason?: string): Promise; public setEnabled(enabled?: boolean, reason?: string): Promise; public setExemptRoles( @@ -1298,6 +1302,8 @@ export class Guild extends AnonymousGuild { public roles: RoleManager; public get rulesChannel(): TextChannel | null; public rulesChannelId: Snowflake | null; + public get safetyAlertsChannel(): TextChannel | null; + public safetyAlertsChannelId: Snowflake | null; public scheduledEvents: GuildScheduledEventManager; public get shard(): WebSocketShard; public shardId: number; @@ -1354,6 +1360,7 @@ export class Guild extends AnonymousGuild { public setPreferredLocale(preferredLocale: Locale | null, reason?: string): Promise; public setPublicUpdatesChannel(publicUpdatesChannel: TextChannelResolvable | null, reason?: string): Promise; public setRulesChannel(rulesChannel: TextChannelResolvable | null, reason?: string): Promise; + public setSafetyAlertsChannel(safetyAlertsChannel: TextChannelResolvable | null, reason?: string): Promise; public setSplash(splash: BufferResolvable | Base64Resolvable | null, reason?: string): Promise; public setSystemChannel(systemChannel: TextChannelResolvable | null, reason?: string): Promise; public setSystemChannelFlags(systemChannelFlags: SystemChannelFlagsResolvable, reason?: string): Promise; @@ -4531,6 +4538,7 @@ export interface AutoModerationTriggerMetadata { presets: AutoModerationRuleKeywordPresetType[]; allowList: string[]; mentionTotalLimit: number | null; + mentionRaidProtectionEnabled: boolean; } export type AwaitMessageComponentOptions = Omit< @@ -5443,6 +5451,7 @@ export interface GuildEditOptions { systemChannelFlags?: SystemChannelFlagsResolvable; rulesChannel?: TextChannelResolvable | null; publicUpdatesChannel?: TextChannelResolvable | null; + safetyAlertsChannel?: TextChannelResolvable | null; preferredLocale?: Locale | null; features?: `${GuildFeature}`[]; description?: string | null; From f2d3abe44c53a6486cbe05fd820bea5c616909d4 Mon Sep 17 00:00:00 2001 From: almeidx Date: Fri, 23 Dec 2022 21:37:49 +0000 Subject: [PATCH 2/4] docs: add missing tag --- packages/discord.js/src/structures/Guild.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/discord.js/src/structures/Guild.js b/packages/discord.js/src/structures/Guild.js index fbbc21a99773..f33f47545fb3 100644 --- a/packages/discord.js/src/structures/Guild.js +++ b/packages/discord.js/src/structures/Guild.js @@ -1168,6 +1168,7 @@ class Guild extends AnonymousGuild { * @param {?TextChannelResolvable} safetyAlertsChannel The new safety alerts channel * @param {string} [reason] Reason for changing the guild's safety alerts channel * @returns {Promise} + * @example * // Edit the guild safety alerts channel * guild.setSafetyAlertsChannel(channel) * .then(updated => console.log(`Updated guild safety alerts channel to ${guild.safetyAlertsChannel.name}`)) From 5af5731aa415b2a8b6c2678af9cfa833a4c7a4b8 Mon Sep 17 00:00:00 2001 From: almeidx Date: Sat, 31 Dec 2022 00:30:02 +0000 Subject: [PATCH 3/4] fix: keep other properties in triggerMetadata --- packages/discord.js/src/structures/AutoModerationRule.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/discord.js/src/structures/AutoModerationRule.js b/packages/discord.js/src/structures/AutoModerationRule.js index d9143e031452..8dc768d0209a 100644 --- a/packages/discord.js/src/structures/AutoModerationRule.js +++ b/packages/discord.js/src/structures/AutoModerationRule.js @@ -238,7 +238,7 @@ class AutoModerationRule extends Base { * @returns {Promise} */ setMentionRaidProtectionEnabled(mentionRaidProtectionEnabled, reason) { - return this.edit({ triggerMetadata: { mentionRaidProtectionEnabled }, reason }); + return this.edit({ triggerMetadata: { ...this.triggerMetadata, mentionRaidProtectionEnabled }, reason }); } /** From 400355f6e551b527a5fb00d709b517025f2dbbd9 Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Fri, 5 May 2023 19:36:49 +0100 Subject: [PATCH 4/4] docs(Guild): update example usage --- packages/discord.js/src/structures/Guild.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/discord.js/src/structures/Guild.js b/packages/discord.js/src/structures/Guild.js index be1d1d15efc1..ce3405813f56 100644 --- a/packages/discord.js/src/structures/Guild.js +++ b/packages/discord.js/src/structures/Guild.js @@ -1175,7 +1175,7 @@ class Guild extends AnonymousGuild { * @example * // Edit the guild safety alerts channel * guild.setSafetyAlertsChannel(channel) - * .then(updated => console.log(`Updated guild safety alerts channel to ${guild.safetyAlertsChannel.name}`)) + * .then(updated => console.log(`Updated guild safety alerts channel to ${updated.safetyAlertsChannel.name}`)) * .catch(console.error); */ setSafetyAlertsChannel(safetyAlertsChannel, reason) {