Skip to content

Commit

Permalink
feat: safety alerts channel and mention raid protection
Browse files Browse the repository at this point in the history
  • Loading branch information
jaw0r3k committed Jan 16, 2023
1 parent 4287983 commit 9d9fef1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/managers/AutoModerationRuleManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class AutoModerationRuleManager extends CachedManager {
* @property {string[]} [allowList] The substrings that will be exempt from triggering
* {@link AutoModerationRuleTriggerType.KEYWORD} and {@link AutoModerationRuleTriggerType.KEYWORD_PRESET}
* @property {?number} [mentionTotalLimit] The total number of role & user mentions allowed per message
* @property {boolean} [mentionRaidProtectionEnabled] Whether to automatically detect mention raids
*/

/**
Expand Down Expand Up @@ -125,6 +126,7 @@ class AutoModerationRuleManager extends CachedManager {
),
allow_list: triggerMetadata.allowList,
mention_total_limit: triggerMetadata.mentionTotalLimit,
mention_raid_protection_enabled: triggerMetadata.mentionRaidProtectionEnabled,
},
actions: actions.map(action => ({
type: typeof action.type === 'number' ? action.type : AutoModerationActionTypes[action.type],
Expand Down Expand Up @@ -186,6 +188,7 @@ class AutoModerationRuleManager extends CachedManager {
),
allow_list: triggerMetadata.allowList,
mention_total_limit: triggerMetadata.mentionTotalLimit,
mention_raid_protection_enabled: triggerMetadata.mentionRaidProtectionEnabled,
},
actions: actions?.map(action => ({
type: typeof action.type === 'number' ? action.type : AutoModerationActionTypes[action.type],
Expand Down
13 changes: 13 additions & 0 deletions src/structures/AutoModerationRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class AutoModerationRule extends Base {
* @property {string[]} allowList The substrings that will be exempt from triggering
* {@link AutoModerationRuleTriggerTypes.KEYWORD} and {@link AutoModerationRuleTriggerTypes.KEYWORD_PRESET}
* @property {?number} mentionTotalLimit The total number of role & user mentions allowed per message
* @property {boolean} mentionRaidProtectionEnabled Whether mention raid protection is enabled
*/

/**
Expand All @@ -85,6 +86,7 @@ class AutoModerationRule extends Base {
presets: data.trigger_metadata.presets?.map(preset => AutoModerationRuleKeywordPresetTypes[preset]) ?? [],
allowList: data.trigger_metadata.allow_list ?? [],
mentionTotalLimit: data.trigger_metadata.mention_total_limit ?? null,
mentionRaidProtectionEnabled: data.trigger_metadata.mention_raid_protection_enabled ?? false,
};
}

Expand Down Expand Up @@ -234,6 +236,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<AutoModerationRule>}
*/
setMentionRaidProtectionEnabled(mentionRaidProtectionEnabled, reason) {
return this.edit({ triggerMetadata: { ...this.triggerMetadata, mentionRaidProtectionEnabled }, reason });
}

/**
* Sets the actions for this auto moderation rule.
* @param {AutoModerationActionOptions[]} actions The actions of this auto moderation rule
Expand Down
38 changes: 38 additions & 0 deletions src/structures/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,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) {
Expand Down Expand Up @@ -568,6 +578,15 @@ class Guild extends AnonymousGuild {
return this.client.channels.resolve(this.systemChannelId);
}

/**
* Safety alerts channel for this guild
* @type {?TextChannel}
* @readonly
*/
get safetyAlertsChannel() {
return this.client.channels.resolve(this.safetyAlertsChannelId);
}

/**
* Widget channel for this guild
* @type {?(TextChannel|NewsChannel|VoiceChannel|StageChannel|ForumChannel)}
Expand Down Expand Up @@ -835,6 +854,7 @@ class Guild extends AnonymousGuild {
* @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 {?TextChannelResolvable} [safetyAlertsChannel] The safety alerts channel 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 {Features[]} [features] The features of the guild
Expand Down Expand Up @@ -918,6 +938,9 @@ class Guild extends AnonymousGuild {
_data.description = data.description;
}
if (typeof data.preferredLocale !== 'undefined') _data.preferred_locale = data.preferredLocale;
if (typeof data.safetyAlertsChannel !== 'undefined') {
_data.safety_alerts_channel_id = this.client.channels.resolveId(data.safetyAlertsChannel);
}
if ('premiumProgressBarEnabled' in data) _data.premium_progress_bar_enabled = data.premiumProgressBarEnabled;
const newData = await this.client.api.guilds(this.id).patch({ data: _data, reason });
return this.client.actions.GuildUpdate.handle(newData).updated;
Expand Down Expand Up @@ -1220,6 +1243,21 @@ class Guild extends AnonymousGuild {
return this.edit({ preferredLocale }, 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<Guild>}
* @example
* // 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 enabled state of the guild's premium progress bar
* @param {boolean} [enabled=true] The new enabled state of the guild's premium progress bar
Expand Down
9 changes: 9 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,8 @@ export class Guild extends AnonymousGuild {
public roles: RoleManager;
public readonly rulesChannel: TextChannel | null;
public rulesChannelId: Snowflake | null;
public readonly safetyAlertsChannel: TextChannel | null;
public safetyAlertsChannelId: Snowflake | null;
public scheduledEvents: GuildScheduledEventManager;
public readonly shard: WebSocketShard;
public shardId: number;
Expand Down Expand Up @@ -1047,6 +1049,7 @@ export class Guild extends AnonymousGuild {
/** @deprecated Use {@link RoleManager.setPositions} instead */
public setRolePositions(rolePositions: readonly RolePosition[]): Promise<Guild>;
public setRulesChannel(rulesChannel: TextChannelResolvable | null, reason?: string): Promise<Guild>;
public setSafetyAlertsChannel(safetyAlertsChannel: TextChannelResolvable | null, reason?: string): Promise<Guild>;
public setSplash(splash: BufferResolvable | Base64Resolvable | null, reason?: string): Promise<Guild>;
public setSystemChannel(systemChannel: TextChannelResolvable | null, reason?: string): Promise<Guild>;
public setSystemChannelFlags(systemChannelFlags: SystemChannelFlagsResolvable, reason?: string): Promise<Guild>;
Expand Down Expand Up @@ -4142,6 +4145,10 @@ export class AutoModerationRule extends Base {
public setRegexPatterns(regexPatterns: string[], reason?: string): Promise<AutoModerationRule>;
public setPresets(presets: AutoModerationRuleKeywordPresetType[], reason?: string): Promise<AutoModerationRule>;
public setAllowList(allowList: string[], reason?: string): Promise<AutoModerationRule>;
public setMentionRaidProtectionEnabled(
mentionRaidProtectionEnabled: boolean,
reason?: string,
): Promise<AutoModerationRule>;
public setMentionTotalLimit(mentionTotalLimit: number, reason?: string): Promise<AutoModerationRule>;
public setActions(actions: AutoModerationActionOptions[], reason?: string): Promise<AutoModerationRule>;
public setEnabled(enabled?: boolean, reason?: string): Promise<AutoModerationRule>;
Expand Down Expand Up @@ -4211,6 +4218,7 @@ export interface AutoModerationTriggerMetadata {
regexPatterns: string[];
presets: (AutoModerationRuleKeywordPresetType | AutoModerationRuleKeywordPresetTypes)[];
allowList: string[];
mentionRaidProtectionEnabled: boolean;
mentionTotalLimit: number | null;
}

Expand Down Expand Up @@ -5283,6 +5291,7 @@ export interface GuildEditData {
rulesChannel?: TextChannelResolvable | null;
publicUpdatesChannel?: TextChannelResolvable | null;
preferredLocale?: string | null;
safetyAlertsChannel?: TextChannelResolvable | null;
premiumProgressBarEnabled?: boolean;
description?: string | null;
features?: GuildFeatures[];
Expand Down

0 comments on commit 9d9fef1

Please sign in to comment.