Skip to content

Commit fc3989d

Browse files
committed
feat: auto moderation
1 parent 608ac2d commit fc3989d

File tree

9 files changed

+945
-2
lines changed

9 files changed

+945
-2
lines changed

lib/discordrb.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ module Discordrb
3232
server_message_typing: 1 << 11,
3333
direct_messages: 1 << 12,
3434
direct_message_reactions: 1 << 13,
35-
direct_message_typing: 1 << 14
35+
direct_message_typing: 1 << 14,
36+
server_automod: 1 << 20,
37+
server_automod_execution: 1 << 21
3638
}.freeze
3739

3840
# All available intents

lib/discordrb/api/server.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,4 +598,71 @@ def bulk_ban(token, server_id, users, message_seconds, reason = nil)
598598
'X-Audit-Log-Reason': reason
599599
)
600600
end
601+
602+
# Get a list of all the configured auto-moderation rules in the server.
603+
# https://discord.com/developers/docs/resources/auto-moderation#list-auto-moderation-rules-for-guild
604+
def list_automod_rules(token, server_id)
605+
Discordrb::API.request(
606+
:guilds_sid_auto_moderation_rules,
607+
server_id,
608+
:get,
609+
"#{Discordrb::API.api_base}/guilds/#{server_id}/auto-moderation/rules",
610+
Authorization: token
611+
)
612+
end
613+
614+
# Get a single auto-moderation rule in the server.
615+
# https://discord.com/developers/docs/resources/auto-moderation#get-auto-moderation-rule
616+
def get_automod_rule(token, server_id, rule_id)
617+
Discordrb::API.request(
618+
:guilds_sid_auto_moderation_rules_rid,
619+
server_id,
620+
:get,
621+
"#{Discordrb::API.api_base}/guilds/#{server_id}/auto-moderation/rules/#{rule_id}",
622+
Authorization: token
623+
)
624+
end
625+
626+
# Create an auto-moderation rule in the server.
627+
# https://discord.com/developers/docs/resources/auto-moderation#create-auto-moderation-rule
628+
def create_automod_rule(token, server_id, name, event_type, trigger_type, trigger_metadata, actions, enabled, exempt_roles, exempt_channels, reason = nil)
629+
Discordrb::API.request(
630+
:guilds_sid_auto_moderation_rules,
631+
server_id,
632+
:post,
633+
"#{Discordrb::API.api_base}/guilds/#{server_id}/auto-moderation/rules",
634+
{ 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,
635+
content_type: :json,
636+
Authorization: token,
637+
'X-Audit-Log-Reason': reason
638+
)
639+
end
640+
641+
# Update an auto-moderation rule in the server.
642+
# https://discord.com/developers/docs/resources/auto-moderation#modify-auto-moderation-rule
643+
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)
644+
Discordrb::API.request(
645+
:guilds_sid_auto_moderation_rules_rid,
646+
server_id,
647+
:patch,
648+
"#{Discordrb::API.api_base}/guilds/#{server_id}/auto-moderation/rules/#{rule_id}",
649+
{ 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,
650+
content_type: :json,
651+
Authorization: token,
652+
'X-Audit-Log-Reason': reason
653+
)
654+
end
655+
656+
# Delete an auto-moderation rule in the server.
657+
# https://discord.com/developers/docs/resources/auto-moderation#delete-auto-moderation-rule
658+
def delete_automod_rule(token, server_id, rule_id, reason = nil)
659+
Discordrb::API.request(
660+
:guilds_sid_auto_moderation_rules_rid,
661+
server_id,
662+
:delete,
663+
"#{Discordrb::API.api_base}/guilds/#{server_id}/auto-moderation/rules/#{rule_id}",
664+
Authorization: token,
665+
'X-Audit-Log-Reason': reason
666+
)
667+
end
601668
end

lib/discordrb/bot.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
require 'discordrb/events/invites'
2121
require 'discordrb/events/interactions'
2222
require 'discordrb/events/threads'
23+
require 'discordrb/events/auto_moderation'
2324

2425
require 'discordrb/api'
2526
require 'discordrb/api/channel'
@@ -1167,6 +1168,18 @@ def update_guild_emoji(data)
11671168
server.update_emoji_data(data)
11681169
end
11691170

1171+
# Internal handler for AUTO_MODERATION_RULE_UPDATE and AUTO_MODERATION_RULE_CREATE
1172+
def update_guild_automod_rule(data)
1173+
server = self.server(data['guild_id'].to_i)
1174+
rule = server&.automod_rule(data['id'].to_i, request: false)
1175+
1176+
if rule
1177+
rule.from_other(data)
1178+
else
1179+
server&.cache_automod_rule(AutoModRule.new(data, self, server))
1180+
end
1181+
end
1182+
11701183
# Internal handler for MESSAGE_CREATE
11711184
def create_message(data); end
11721185

@@ -1669,6 +1682,24 @@ def handle_dispatch(type, data)
16691682

16701683
event = ThreadMembersUpdateEvent.new(data, self)
16711684
raise_event(event)
1685+
when :AUTO_MODERATION_RULE_CREATE
1686+
update_guild_automod_rule(data)
1687+
1688+
event = AutoModRuleCreateEvent.new(data, self)
1689+
raise_event(event)
1690+
when :AUTO_MODERATION_RULE_UPDATE
1691+
update_guild_automod_rule(data)
1692+
1693+
event = AutoModRuleUpdateEvent.new(data, self)
1694+
raise_event(event)
1695+
when :AUTO_MODERATION_RULE_DELETE
1696+
self.server(data['guild_id'].to_i).delete_automod_rule(data['id'].to_i)
1697+
1698+
event = AutoModRuleDeleteEvent.new(data, self)
1699+
raise_event(event)
1700+
when :AUTO_MODERATION_ACTION_EXECUTION
1701+
event = AutoModActionEvent.new(data, self)
1702+
raise_event(event)
16721703
else
16731704
# another event that we don't support yet
16741705
debug "Event #{type} has been received but is unsupported. Raising UnknownEvent"

lib/discordrb/container.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,74 @@ def application_command_permissions_update(attributes = {}, &block)
673673
register_event(ApplicationCommandPermissionsUpdateEvent, attributes, block)
674674
end
675675

676+
# This **event** is raised whenever an automod rule is deleted.
677+
# @param attributes [Hash] The event's attributes.
678+
# @option attributes [String, Integer, Server] :server A server to match against.
679+
# @option attributes [String, Regexp] :name A name to match against.
680+
# @option attributes [String, Integer, AutoModRule] :automod_rule An automod rule to match against.
681+
# @option attributes [String, Integer, User, Member] :creator A creator to match against.
682+
# @option attributes [Symbol, Integer] :event_type An event type to match against.
683+
# @option attributes [Symbol, Integer] :trigger_type A trigger type to match against.
684+
# @yield The block is executed when the event is raised.
685+
# @yieldparam event [AutoModRuleDeleteEvent] The event that was raised.
686+
# @return [AutoModRuleDeleteEventHandler] The event handler that was registered.
687+
# @note This event requires the `:server_automod` intent in order to be received.
688+
def automod_rule_create(attributes = {}, &block)
689+
register_event(AutoModRuleCreateEvent, attributes, block)
690+
end
691+
692+
# This **event** is raised whenever an automod rule is deleted.
693+
# @param attributes [Hash] The event's attributes.
694+
# @option attributes [String, Integer, Server] :server A server to match against.
695+
# @option attributes [String, Regexp] :name A name to match against.
696+
# @option attributes [String, Integer, AutoModRule] :automod_rule An automod rule to match against.
697+
# @option attributes [String, Integer, User, Member] :creator A creator to match against.
698+
# @option attributes [Symbol, Integer] :event_type An event type to match against.
699+
# @option attributes [Symbol, Integer] :trigger_type A trigger type to match against.
700+
# @yield The block is executed when the event is raised.
701+
# @yieldparam event [AutoModRuleDeleteEvent] The event that was raised.
702+
# @return [AutoModRuleDeleteEventHandler] The event handler that was registered.
703+
# @note This event requires the `:server_automod` intent in order to be received.
704+
def automod_rule_update(attributes = {}, &block)
705+
register_event(AutoModRuleUpdateEvent, attributes, block)
706+
end
707+
708+
# This **event** is raised whenever an automod rule is deleted.
709+
# @param attributes [Hash] The event's attributes.
710+
# @option attributes [String, Integer, Server] :server A server to match against.
711+
# @option attributes [String, Regexp] :name A name to match against.
712+
# @option attributes [String, Integer, AutoModRule] :automod_rule An automod rule to match against.
713+
# @option attributes [String, Integer, User, Member] :creator A creator to match against.
714+
# @option attributes [Symbol, Integer] :event_type An event type to match against.
715+
# @option attributes [Symbol, Integer] :trigger_type A trigger type to match against.
716+
# @yield The block is executed when the event is raised.
717+
# @yieldparam event [AutoModRuleDeleteEvent] The event that was raised.
718+
# @return [AutoModRuleDeleteEventHandler] The event handler that was registered.
719+
# @note This event requires the `:server_automod` intent in order to be received.
720+
def automod_rule_delete(attributes = {}, &block)
721+
register_event(AutoModRuleDeleteEvent, attributes, block)
722+
end
723+
724+
# This **event** is raised whenever an automod rule is triggered and takes an action, e.g. blocking a message.
725+
# @param attributes [Hash] The event's attributes.
726+
# @option attributes [String, Integer, Member] :user The user which generated the content that triggered the rule to match against.
727+
# @option attributes [String, Integer, Server] :server A server to match against.
728+
# @option attributes [String, Integer, Channel] :channel The channel in which user content was posted to match against.
729+
# @option attributes [String, Integer, AutoModRule] :automod_rule An automod rule to match against.
730+
# @option attributes [String, Regexp] :content The user generated content which triggered the rule to match against.
731+
# @option attributes [Symbol, Integer] :trigger_type A trigger type to match against.
732+
# @option attributes [String, Regexp] :matched_content The substring in content that triggered the rule to match against.
733+
# @option attributes [String, Regexp] :matched_keyword The keyword or phrase that triggered the rule to match against.
734+
# @option attributes [Symbol, Integer] :event_type An event type to match against.
735+
# @option attributes [Symbol, Integer] :action_type An action type to match against.
736+
# @yield The block is executed when the event is raised.
737+
# @yieldparam event [AutoModActionEvent] The event that was raised.
738+
# @return [AutoModActionEventHandler] The event handler that was registered.
739+
# @note This event requires the `:server_automod_execution` intent in order to be received.
740+
def automod_rule_action(attributes = {}, &block)
741+
register_event(AutoModActionEvent, attributes, block)
742+
end
743+
676744
# This **event** is raised for every dispatch received over the gateway, whether supported by discordrb or not.
677745
# @param attributes [Hash] The event's attributes.
678746
# @option attributes [String, Symbol, Regexp] :type Matches the event type of the dispatch.

lib/discordrb/data.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@
4444
require 'discordrb/data/collectibles'
4545
require 'discordrb/data/primary_server'
4646
require 'discordrb/data/server_preview'
47+
require 'discordrb/data/auto_moderation'

0 commit comments

Comments
 (0)