Skip to content

Commit e1fe2d1

Browse files
committed
feat: KWARGS
1 parent fc27658 commit e1fe2d1

File tree

3 files changed

+46
-48
lines changed

3 files changed

+46
-48
lines changed

lib/discordrb/api/server.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -619,13 +619,13 @@ def get_automod_rule(token, server_id, rule_id)
619619

620620
# Create an auto-moderation rule in the server.
621621
# https://discord.com/developers/docs/resources/auto-moderation#create-auto-moderation-rule
622-
def create_automod_rule(token, server_id, name, event_type, trigger_type, trigger_metadata, actions, enabled, exempt_roles, exempt_channels, reason = nil)
622+
def create_automod_rule(token, server_id, name:, event_type:, trigger_type:, actions:, trigger_metadata: nil, enabled: nil, exempt_roles: nil, exempt_channels: nil, reason: nil)
623623
Discordrb::API.request(
624624
:guilds_sid_auto_moderation_rules,
625625
server_id,
626626
:post,
627627
"#{Discordrb::API.api_base}/guilds/#{server_id}/auto-moderation/rules",
628-
{ name: name, event_type: event_type, trigger_type: trigger_type, trigger_metadata: trigger_metadata, actions: actions, enabled: enabled, exempt_roles: exempt_roles, exempt_channels: exempt_channels }.to_json,
628+
{ name:, event_type:, trigger_type:, trigger_metadata:, actions:, enabled:, exempt_roles:, exempt_channels: }.compact.to_json,
629629
content_type: :json,
630630
Authorization: token,
631631
'X-Audit-Log-Reason': reason
@@ -634,13 +634,13 @@ def create_automod_rule(token, server_id, name, event_type, trigger_type, trigge
634634

635635
# Update an auto-moderation rule in the server.
636636
# https://discord.com/developers/docs/resources/auto-moderation#modify-auto-moderation-rule
637-
def update_automod_rule(token, server_id, rule_id, name = nil, event_type = nil, trigger_metadata = nil, actions = nil, enabled = nil, exempt_roles = nil, exempt_channels = nil, reason = nil)
637+
def update_automod_rule(token, server_id, rule_id, name: nil, event_type: nil, actions: nil, trigger_metadata: nil, enabled: nil, exempt_roles: nil, exempt_channels: nil, reason: nil)
638638
Discordrb::API.request(
639639
:guilds_sid_auto_moderation_rules_rid,
640640
server_id,
641641
:patch,
642642
"#{Discordrb::API.api_base}/guilds/#{server_id}/auto-moderation/rules/#{rule_id}",
643-
{ name: name, event_type: event_type, trigger_metadata: trigger_metadata, actions: actions, enabled: enabled, exempt_roles: exempt_roles, exempt_channels: exempt_channels }.compact.to_json,
643+
{ name:, event_type:, trigger_metadata:, actions:, enabled:, exempt_roles:, exempt_channels: }.compact.to_json,
644644
content_type: :json,
645645
Authorization: token,
646646
'X-Audit-Log-Reason': reason
@@ -649,7 +649,7 @@ def update_automod_rule(token, server_id, rule_id, name = nil, event_type = nil,
649649

650650
# Delete an auto-moderation rule in the server.
651651
# https://discord.com/developers/docs/resources/auto-moderation#delete-auto-moderation-rule
652-
def delete_automod_rule(token, server_id, rule_id, reason = nil)
652+
def delete_automod_rule(token, server_id, rule_id, reason: nil)
653653
Discordrb::API.request(
654654
:guilds_sid_auto_moderation_rules_rid,
655655
server_id,

lib/discordrb/data/auto_moderation.rb

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def exempt_channels=(exempt_channels)
113113
# @param reason [String, nil] the reason for deleting this automod rule.
114114
# @return [void]
115115
def delete(reason: nil)
116-
API::Server.delete_automod_rule(@bot.token, @server.id, @id, reason)
116+
API::Server.delete_automod_rule(@bot.token, @server.id, @id, reason: reason)
117117
server.delete_automod_rule(@id)
118118
end
119119

@@ -166,11 +166,7 @@ def from_other(new_data)
166166

167167
# @!visibility private
168168
def update_data(new_data)
169-
from_other(JSON.parse(API::Server.update_automod_rule(@bot.token, @server.id, @id,
170-
new_data[:name], new_data[:event_type],
171-
new_data[:trigger], new_data[:actions],
172-
new_data[:enabled], new_data[:exempt_roles],
173-
new_data[:exempt_channels], new_data[:reason])))
169+
from_other(JSON.parse(API::Server.update_automod_rule(@bot.token, @server.id, @id, **new_data)))
174170
end
175171

176172
# Information used to determine when an automod rule should be triggered.
@@ -235,39 +231,39 @@ def initialize(data, rule, bot)
235231
def exempt_keywords=(exempt_keywords)
236232
validate_trigger(field: :exempt_keywords)
237233

238-
@rule.update_data(trigger: to_h.merge({ allow_list: exempt_keywords }))
234+
@rule.update_data(trigger_metadata: to_h.merge({ allow_list: exempt_keywords }))
239235
end
240236

241237
# Set the regex patterns that can trigger the automod rule.
242238
# @param regex_patterns [Array<String>]
243239
def regex_patterns=(regex_patterns)
244240
validate_trigger(field: :regex_patterns)
245241

246-
@rule.update_data(trigger: to_h.merge({ regex_patterns: regex_patterns }))
242+
@rule.update_data(trigger_metadata: to_h.merge({ regex_patterns: regex_patterns }))
247243
end
248244

249245
# Set the substrings that can trigger the automod rule.
250246
# @param keyword_filter [Array<String>]
251247
def keyword_filter=(keyword_filter)
252248
validate_trigger(field: :keyword_filter)
253249

254-
@rule.update_data(trigger: to_h.merge({ keyword_filter: keyword_filter }))
250+
@rule.update_data(trigger_metadata: to_h.merge({ keyword_filter: keyword_filter }))
255251
end
256252

257253
# Set the maximum amount of unique mentions allowed for the rule.
258254
# @param mention_limit [Integer]
259255
def total_mention_limit=(mention_limit)
260256
validate_trigger(field: :total_mention_limit)
261257

262-
@rule.update_data(trigger: to_h.merge({ mention_total_limit: mention_limit }))
258+
@rule.update_data(trigger_metadata: to_h.merge({ mention_total_limit: mention_limit }))
263259
end
264260

265261
# Set whether mention raid protection is enabled for the rule or not.
266262
# @param raid_protection [true, false]
267263
def mention_raid_protection=(raid_protection)
268264
validate_trigger(field: :mention_raid_protection)
269265

270-
@rule.update_data(trigger: to_h.merge({ mention_raid_protection_enabled: raid_protection }))
266+
@rule.update_data(trigger_metadata: to_h.merge({ mention_raid_protection_enabled: raid_protection }))
271267
end
272268

273269
# Set the keyword presets for this rule.
@@ -277,7 +273,7 @@ def keyword_presets=(presets)
277273

278274
presets.map! { |type| PRESET_TYPES[type] || type }
279275

280-
@rule.update_data(trigger: to_h.merge({ presets: presets }))
276+
@rule.update_data(trigger_metadata: to_h.merge({ presets: presets }))
281277
end
282278

283279
# @!method keyword?
@@ -300,7 +296,7 @@ def keyword_presets=(presets)
300296

301297
# @!visibility private
302298
def validate_trigger(field:)
303-
value = case field
299+
state = case field
304300
when :total_mention_limit, :mention_raid_protection
305301
mention_spam?
306302
when :keyword_presets
@@ -311,7 +307,7 @@ def validate_trigger(field:)
311307
keyword? || keyword_preset? || member_profile?
312308
end
313309

314-
raise "Cannot set #{field} for trigger type #{TYPES.key(@type)}" unless value
310+
raise "Cannot set #{field} for trigger type #{TYPES.key(@type)}" unless state
315311
end
316312

317313
# @!visibility private

lib/discordrb/data/server.rb

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)