@@ -653,45 +653,47 @@ def automod_rules(bypass_cache: false)
653653
654654 # Create an automod rule on this server. Requires the `manage_server` permission.
655655 # @param name [String] The name of the automod rule to create.
656- # @param event_type [Integer, Symbol] The event type of the automod rule. See {AutoModRule::EVENT_TYPES}.
657- # @param trigger_type [Integer, Symbol] The type of the automod rule's trigger. See {AutoModRule::Trigger::TYPES}.
658- # @param enabled [true, false] Whether the automod rule should be enabled. False by default.
659- # @param exempt_roles [Array<Integer, String, Role>] The roles that should be ignored by the automod rule. Max of 20.
660- # @param exempt_channels [Array<Integer, String, Channel>] The channels that should be ignored by the automod rule. Max of 50.
661- # @param keyword_filter [Array<String>, nil] The substrings that should trigger the automod rule.
662- # @param regex_patterns [Array<String>, nil] Rust flavoured regex patterns that when matched can trigger the automod rule.
663- # @param keyword_presets [Array<Integer, Symbol>, nil] Set of word types that can trigger the automod rule. See {AutoModRule::Trigger::PRESET_TYPES}.
664- # @param exempt_keywords [Array<String>, nil] Substrings that should not trigger the automod rule.
665- # @param mention_limit [Integer, nil] The total number of unique role and user mentions allowed per message.
666- # @param mention_raid_protection [true, false, nil] Whether the automod rule should automatically detect mention raids.
656+ # @param event_type [Integer, Symbol] The event type of the automod rule to create.
657+ # @param trigger_type [Integer, Symbol] The trigger type of the automod rule to create.
658+ # @param actions [Array<#to_h>] The actions of the automod rule to create.
659+ # @param enabled [true, false] Whether to enabled the automod rule to create.
660+ # @param exempt_roles [Array<#resolve_id>] The exempt roles of the automod rule to create (max 20).
661+ # @param exempt_channels [Array<#resolve_id>] The exempt channels of the automod rule to create (max 50).
662+ # @param keyword_filter [Array<String>] The substrings that should trigger the automod rule to create.
663+ # @param regex_patterns [Array<String>] The Rust regex patterns that should trigger the automod rule to create.
664+ # @param keyword_presets [Array<Integer, Symbol>] The of word types that can trigger the automod rule to create.
665+ # @param exempt_keywords [Array<String>] The substrings that should not trigger the automod rule to create.
666+ # @param mention_limit [Integer] The number of unique mentions that should trigger the automod rule to create.
667+ # @param mention_raid_protection [true, false] If mention raids should be auto-detected by the automod rule to create.
667668 # @param reason [String, nil] The reason for creating the automod rule.
668- # @yieldparam action_builder [AutoModRule::ActionBuilder] The action builder allows you to add actions that should execute when the rule is triggered .
669+ # @yieldparam action_builder [AutoModRule::ActionBuilder] An optional actions builder .
669670 # @return [AutoModRule] the newly created automod rule.
670- # @note Arguments that default to `nil` are not required to be passed. The `trigger_type` field is what determines which values should be set.
671- # To understand which `trigger_type` values require which fields to be set, please refer to:
672- # https://discord.com/developers/docs/resources/auto-moderation#auto-moderation-rule-object-trigger-metadata
673- def create_automod_rule ( name :, event_type :, trigger_type :, enabled : false , exempt_roles : [ ] , exempt_channels : [ ] ,
674- keyword_filter : nil , regex_patterns : nil , keyword_presets : nil , exempt_keywords : nil ,
675- mention_limit : nil , mention_raid_protection : nil , reason : nil )
676- metadata = {
671+ def create_automod_rule ( name :, event_type :, trigger_type :, actions : [ ] , enabled : nil , exempt_roles : nil , exempt_channels : nil , keyword_filter : nil , regex_patterns : nil , keyword_presets : nil , exempt_keywords : nil , mention_limit : nil , mention_raid_protection : nil , reason : nil )
672+ yield ( ( builder = AutoModRule ::ActionBuilder . new ) ) if block_given?
673+
674+ trigger = {
675+ allow_list : exempt_keywords ,
677676 keyword_filter : keyword_filter ,
678677 regex_patterns : regex_patterns ,
679- allow_list : exempt_keywords ,
680678 mention_total_limit : mention_limit ,
681679 mention_raid_protection_enabled : mention_raid_protection ,
682680 presets : keyword_presets &.map { |type | AutoModRule ::Trigger ::PRESET_TYPES [ type ] || type }
683681 } . compact
684682
685- builder = AutoModRule ::ActionBuilder . new
686- yield builder if block_given?
687-
688- event_type = AutoModRule ::EVENT_TYPES [ event_type ] || event_type
689- trigger_type = AutoModRule ::Trigger ::TYPES [ trigger_type ] || trigger_type
683+ options = {
684+ name : name ,
685+ enabled : enabled ,
686+ exempt_roles : exempt_roles &.map ( &:resolve_id ) ,
687+ trigger_metadata : trigger . empty? ? nil : trigger ,
688+ exempt_channels : exempt_channels &.map ( &:resolve_id ) ,
689+ actions : block_given? ? builder &.to_a : actions . map ( &:to_h ) ,
690+ event_type : AutoModRule ::EVENT_TYPES [ event_type ] || event_type ,
691+ trigger_type : AutoModRule ::Trigger ::TYPES [ trigger_type ] || trigger_type
692+ }
690693
691- response = API ::Server . create_automod_rule ( @bot . token , @id , name , event_type , trigger_type , metadata , builder . to_a , enabled ,
692- exempt_roles . map ( &:resolve_id ) , exempt_channels . map ( &:resolve_id ) , reason )
693- rule = AutoModRule . new ( JSON . parse ( response ) , self , @bot )
694- @automod_rules [ rule . id ] = rule
694+ rule = JSON . parse ( API ::Server . create_automod_rule ( @bot . token , @id , **options , reason : reason ) )
695+ automod_rule = AutoModRule . new ( rule , self , @bot )
696+ @automod_rules [ automod_rule . id ] = automod_rule
695697 end
696698
697699 # Searches a server for members that matches a username or a nickname.
0 commit comments