discorb 0.18.0 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/build_version.yml +2 -2
  3. data/.rubocop.yml +12 -75
  4. data/Changelog.md +25 -0
  5. data/Gemfile +4 -4
  6. data/README.md +2 -1
  7. data/Rakefile +482 -459
  8. data/Steepfile +8 -6
  9. data/docs/application_command.md +1 -0
  10. data/docs/events.md +2 -2
  11. data/docs/voice_events.md +6 -6
  12. data/lib/discorb/allowed_mentions.rb +68 -72
  13. data/lib/discorb/app_command/command.rb +466 -394
  14. data/lib/discorb/app_command/common.rb +65 -25
  15. data/lib/discorb/app_command/handler.rb +304 -265
  16. data/lib/discorb/app_command.rb +5 -5
  17. data/lib/discorb/application.rb +198 -197
  18. data/lib/discorb/asset.rb +101 -101
  19. data/lib/discorb/attachment.rb +134 -119
  20. data/lib/discorb/audit_logs.rb +412 -385
  21. data/lib/discorb/automod.rb +279 -269
  22. data/lib/discorb/channel/base.rb +107 -108
  23. data/lib/discorb/channel/category.rb +32 -32
  24. data/lib/discorb/channel/container.rb +44 -44
  25. data/lib/discorb/channel/dm.rb +26 -28
  26. data/lib/discorb/channel/guild.rb +311 -246
  27. data/lib/discorb/channel/stage.rb +156 -140
  28. data/lib/discorb/channel/text.rb +430 -336
  29. data/lib/discorb/channel/thread.rb +374 -325
  30. data/lib/discorb/channel/voice.rb +85 -79
  31. data/lib/discorb/channel.rb +5 -5
  32. data/lib/discorb/client.rb +635 -623
  33. data/lib/discorb/color.rb +178 -182
  34. data/lib/discorb/common.rb +168 -164
  35. data/lib/discorb/components/button.rb +107 -106
  36. data/lib/discorb/components/select_menu.rb +157 -145
  37. data/lib/discorb/components/text_input.rb +103 -106
  38. data/lib/discorb/components.rb +68 -66
  39. data/lib/discorb/dictionary.rb +135 -135
  40. data/lib/discorb/embed.rb +404 -398
  41. data/lib/discorb/emoji.rb +309 -302
  42. data/lib/discorb/emoji_table.rb +16099 -8857
  43. data/lib/discorb/error.rb +131 -131
  44. data/lib/discorb/event.rb +360 -314
  45. data/lib/discorb/event_handler.rb +39 -39
  46. data/lib/discorb/exe/about.rb +17 -17
  47. data/lib/discorb/exe/irb.rb +72 -67
  48. data/lib/discorb/exe/new.rb +323 -315
  49. data/lib/discorb/exe/run.rb +69 -68
  50. data/lib/discorb/exe/setup.rb +57 -55
  51. data/lib/discorb/exe/show.rb +12 -12
  52. data/lib/discorb/extend.rb +25 -45
  53. data/lib/discorb/extension.rb +89 -83
  54. data/lib/discorb/flag.rb +126 -128
  55. data/lib/discorb/gateway.rb +984 -794
  56. data/lib/discorb/gateway_events.rb +670 -638
  57. data/lib/discorb/gateway_requests.rb +45 -48
  58. data/lib/discorb/guild.rb +2115 -1626
  59. data/lib/discorb/guild_template.rb +280 -241
  60. data/lib/discorb/http.rb +247 -232
  61. data/lib/discorb/image.rb +42 -42
  62. data/lib/discorb/integration.rb +169 -161
  63. data/lib/discorb/intents.rb +161 -163
  64. data/lib/discorb/interaction/autocomplete.rb +76 -62
  65. data/lib/discorb/interaction/command.rb +279 -224
  66. data/lib/discorb/interaction/components.rb +114 -104
  67. data/lib/discorb/interaction/modal.rb +36 -32
  68. data/lib/discorb/interaction/response.rb +379 -330
  69. data/lib/discorb/interaction/root.rb +271 -118
  70. data/lib/discorb/interaction.rb +5 -5
  71. data/lib/discorb/invite.rb +154 -153
  72. data/lib/discorb/member.rb +344 -311
  73. data/lib/discorb/message.rb +615 -544
  74. data/lib/discorb/message_meta.rb +197 -186
  75. data/lib/discorb/modules.rb +371 -290
  76. data/lib/discorb/permission.rb +305 -289
  77. data/lib/discorb/presence.rb +352 -346
  78. data/lib/discorb/rate_limit.rb +81 -76
  79. data/lib/discorb/reaction.rb +55 -54
  80. data/lib/discorb/role.rb +272 -240
  81. data/lib/discorb/shard.rb +76 -74
  82. data/lib/discorb/sticker.rb +193 -171
  83. data/lib/discorb/user.rb +205 -188
  84. data/lib/discorb/utils/colored_puts.rb +16 -16
  85. data/lib/discorb/utils.rb +12 -16
  86. data/lib/discorb/voice_state.rb +305 -281
  87. data/lib/discorb/webhook.rb +537 -507
  88. data/lib/discorb.rb +62 -56
  89. data/sig/discorb/activity.rbs +1 -0
  90. data/sig/discorb/allowed_mentions.rbs +1 -0
  91. data/sig/discorb/app_command/base.rbs +7 -1
  92. data/sig/discorb/application.rbs +6 -0
  93. data/sig/discorb/asset.rbs +2 -0
  94. data/sig/discorb/attachment.rbs +8 -0
  95. data/sig/discorb/audit_log.rbs +7 -0
  96. data/sig/discorb/automod.rbs +32 -6
  97. data/sig/discorb/avatar.rbs +1 -0
  98. data/sig/discorb/channel/base.rbs +8 -1
  99. data/sig/discorb/channel/category.rbs +1 -0
  100. data/sig/discorb/channel/container.rbs +4 -0
  101. data/sig/discorb/channel/stage.rbs +4 -0
  102. data/sig/discorb/channel/text.rbs +2 -2
  103. data/sig/discorb/channel/thread.rbs +11 -0
  104. data/sig/discorb/channel/voice.rbs +2 -0
  105. data/sig/discorb/client.rbs +21 -20
  106. data/sig/discorb/color.rbs +6 -0
  107. data/sig/discorb/component/base.rbs +1 -0
  108. data/sig/discorb/component/button.rbs +2 -0
  109. data/sig/discorb/component/select_menu.rbs +4 -0
  110. data/sig/discorb/component/text_input.rbs +1 -0
  111. data/sig/discorb/custom_emoji.rbs +5 -1
  112. data/sig/discorb/dictionary.rbs +2 -0
  113. data/sig/discorb/discord_model.rbs +2 -0
  114. data/sig/discorb/embed.rbs +7 -0
  115. data/sig/discorb/emoji.rbs +1 -0
  116. data/sig/discorb/event_handler.rbs +2 -1
  117. data/sig/discorb/extension.rbs +13 -12
  118. data/sig/discorb/flag.rbs +2 -0
  119. data/sig/discorb/gateway.rbs +5 -0
  120. data/sig/discorb/guild.rbs +8 -4
  121. data/sig/discorb/guild_template.rbs +1 -1
  122. data/sig/discorb/http.rbs +4 -1
  123. data/sig/discorb/image.rbs +2 -0
  124. data/sig/discorb/integration.rbs +1 -1
  125. data/sig/discorb/intents.rbs +4 -3
  126. data/sig/discorb/interaction/base.rbs +36 -0
  127. data/sig/discorb/interaction/message_component.rbs +1 -2
  128. data/sig/discorb/interaction/modal.rbs +1 -2
  129. data/sig/discorb/interaction/responder.rbs +49 -49
  130. data/sig/discorb/invite.rbs +1 -1
  131. data/sig/discorb/member.rbs +2 -0
  132. data/sig/discorb/message.rbs +8 -1
  133. data/sig/discorb/messageable.rbs +1 -4
  134. data/sig/discorb/partial_emoji.rbs +3 -0
  135. data/sig/discorb/permissions.rbs +7 -0
  136. data/sig/discorb/presence.rbs +2 -0
  137. data/sig/discorb/reaction.rbs +5 -1
  138. data/sig/discorb/role.rbs +7 -1
  139. data/sig/discorb/scheduled_event.rbs +2 -1
  140. data/sig/discorb/shard.rbs +2 -1
  141. data/sig/discorb/snowflake.rbs +2 -0
  142. data/sig/discorb/stage_instance.rbs +9 -3
  143. data/sig/discorb/sticker.rbs +1 -1
  144. data/sig/discorb/unicode_emoji.rbs +4 -0
  145. data/sig/discorb/user.rbs +24 -20
  146. data/sig/discorb/webhook.rbs +17 -6
  147. data/sig/discorb/welcome_screen.rbs +1 -0
  148. data/sig/override.rbs +2 -0
  149. metadata +3 -3
@@ -1,269 +1,279 @@
1
- # frozen_string_literal: true
2
-
3
- module Discorb
4
- #
5
- # Represents a rule of auto moderation.
6
- #
7
- class AutoModRule < DiscordModel
8
- # @return [Hash{Integer => Symbol}] The mapping of trigger types.
9
- # @private
10
- TRIGGER_TYPES = {
11
- 1 => :keyword,
12
- 2 => :harmful_link,
13
- 3 => :spam,
14
- 4 => :keyword_preset,
15
- }.freeze
16
- # @return [Hash{Integer => Symbol}] The mapping of preset types.
17
- # @private
18
- PRESET_TYPES = {
19
- 1 => :profanity,
20
- 2 => :sexual_content,
21
- 3 => :slurs,
22
- }.freeze
23
- # @return [Hash{Integer => Symbol}] The mapping of event types.
24
- # @private
25
- EVENT_TYPES = {
26
- 1 => :message_send,
27
- }.freeze
28
-
29
- # @return [Discorb::Snowflake] The ID of the rule.
30
- attr_reader :id
31
- # @return [String] The name of the rule.
32
- attr_reader :name
33
- # @return [Boolean] Whether the rule is enabled.
34
- attr_reader :enabled
35
- alias enabled? enabled
36
- # @return [Array<Discorb::AutoModRule::Action>] The actions of the rule.
37
- attr_reader :actions
38
- # @return [Array<String>] The keywords that the rule is triggered by.
39
- # @note This is only available if the trigger type is `:keyword`.
40
- attr_reader :keyword_filter
41
-
42
- #
43
- # Initialize a new auto mod.
44
- # @private
45
- #
46
- # @param [Discorb::Client] client The client.
47
- # @param [Hash] data The auto mod data.
48
- #
49
- def initialize(client, data)
50
- @client = client
51
- _set_data(data)
52
- end
53
-
54
- # @!attribute [r]
55
- # @return [Symbol] Returns the type of the preset.
56
- # @note This is only available if the trigger type is `:keyword_preset`.
57
- def preset_type
58
- PRESET_TYPES[@presets_raw]
59
- end
60
-
61
- # @!attribute [r]
62
- # @return [Symbol] Returns the type of the trigger.
63
- def trigger_type
64
- TRIGGER_TYPES[@trigger_type_raw]
65
- end
66
-
67
- # @!attribute [r]
68
- # @return [Symbol] Returns the type of the event.
69
- def event_type
70
- EVENT_TYPES[@event_type_raw]
71
- end
72
-
73
- # @!attribute [r]
74
- # @macro client_cache
75
- # @return [Discorb::Member] The member who created the rule.
76
- def creator
77
- guild.members[@creator_id]
78
- end
79
-
80
- # @!attribute [r]
81
- # @return [Discorb::Guild] The guild that the rule is in.
82
- def guild
83
- @client.guilds[@guild_id]
84
- end
85
-
86
- # @!attribute [r]
87
- # @return [Array<Discorb::Role>] The roles that the rule is exempt from.
88
- def exempt_roles
89
- @exempt_roles_id.map { |id| guild.roles[id] }
90
- end
91
-
92
- # @!attribute [r]
93
- # @return [Array<Discorb::Channel>] The channels that the rule is exempt from.
94
- def exempt_channels
95
- @exempt_channels_id.map { |id| guild.channels[id] }
96
- end
97
-
98
- #
99
- # Edit the rule.
100
- # @async
101
- # @edit
102
- #
103
- # @param [String] name The name of the rule.
104
- # @param [Symbol] event_type The event type of the rule. See {Discorb::AutoModRule::EVENT_TYPES}.
105
- # @param [Array<Discorb::AutoModRule::Action>] actions The actions of the rule.
106
- # @param [Boolean] enabled Whether the rule is enabled or not.
107
- # @param [Array<Discorb::Role>] exempt_roles The roles that are exempt from the rule.
108
- # @param [Array<Discorb::Channel>] exempt_channels The channels that are exempt from the rule.
109
- # @param [Array<String>] keyword_filter The keywords to filter.
110
- # @param [Symbol] presets The preset of the rule. See {Discorb::AutoModRule::PRESET_TYPES}.
111
- # @param [String] reason The reason for creating the rule.
112
- #
113
- # @return [Async::Task<void>] The task.
114
- #
115
- def edit(
116
- name: Discorb::Unset,
117
- event_type: Discorb::Unset,
118
- actions: Discorb::Unset,
119
- enabled: Discorb::Unset,
120
- exempt_roles: Discorb::Unset,
121
- exempt_channels: Discorb::Unset,
122
- keyword_filter: Discorb::Unset,
123
- presets: Discorb::Unset,
124
- reason: nil
125
- )
126
- # @type var payload: Hash[Symbol, untyped]
127
- payload = {
128
- metadata: {},
129
- }
130
- payload[:name] = name unless name == Discorb::Unset
131
- payload[:event_type] = EVENT_TYPES.key(event_type) unless event_type == Discorb::Unset
132
- payload[:actions] = actions unless actions == Discorb::Unset
133
- payload[:enabled] = enabled unless enabled == Discorb::Unset
134
- payload[:exempt_roles] = exempt_roles.map(&:id) unless exempt_roles == Discorb::Unset
135
- payload[:exempt_channels] = exempt_channels.map(&:id) unless exempt_channels == Discorb::Unset
136
- payload[:metadata][:keyword_filter] = keyword_filter unless keyword_filter == Discorb::Unset
137
- payload[:metadata][:presets] = PRESET_TYPES.key(presets) unless presets == Discorb::Unset
138
-
139
- @client.http.request(
140
- Route.new(
141
- "/guilds/#{@guild_id}/automod/rules/#{@id}",
142
- "//guilds/:guild_id/automod/rules/:id",
143
- :patch
144
- ),
145
- payload,
146
- audit_log_reason: reason,
147
- )
148
- end
149
-
150
- #
151
- # Delete the rule.
152
- #
153
- # @param [String] reason The reason for deleting the rule.
154
- #
155
- # @return [Async::Task<void>] The task.
156
- #
157
- def delete!(reason: nil)
158
- Async do
159
- @client.http.request(
160
- Route.new(
161
- "/guilds/#{@guild_id}/automod/rules/#{@id}",
162
- "//guilds/:guild_id/automod/rules/:id",
163
- :delete
164
- ),
165
- audit_log_reason: reason,
166
- )
167
- end
168
- end
169
-
170
- # @private
171
- def _set_data(data)
172
- @id = Snowflake.new(data[:id])
173
- @guild_id = data[:guild_id]
174
- @name = data[:name]
175
- @creator_id = data[:creator_id]
176
- @trigger_type_raw = data[:trigger_type]
177
- @event_type_raw = data[:event_type]
178
- @actions = data[:actions].map { |action| Action.from_hash(@client, action) }
179
- case trigger_type
180
- when :keyword
181
- @keyword_filter = data[:trigger_metadata][:keyword_filter]
182
- when :presets
183
- @presets_raw = data[:trigger_metadata][:presets]
184
- end
185
- @enabled = data[:enabled]
186
- @exempt_roles_id = data[:exempt_roles]
187
- @exempt_channels_id = data[:exempt_channels]
188
- end
189
-
190
- #
191
- # Represents the action of auto moderation.
192
- #
193
- class Action < DiscordModel
194
- # @return [Hash{Integer => Symbol}] The mapping of action types.
195
- # @private
196
- ACTION_TYPES = {
197
- 1 => :block_message,
198
- 2 => :send_alert_message,
199
- 3 => :timeout,
200
- }.freeze
201
-
202
- # @return [Symbol] Returns the type of the action.
203
- attr_reader :type
204
- # @return [Integer] The duration of the timeout.
205
- # @note This is only available if the action type is `:timeout`.
206
- attr_reader :duration_seconds
207
-
208
- #
209
- # Initialize a new action.
210
- #
211
- # @param [Symbol] type The type of the action.
212
- # @param [Integer] duration_seconds The duration of the timeout.
213
- # This is only available if the action type is `:timeout`.
214
- # @param [Discorb::Channel] channel The channel that the alert message is sent to.
215
- # This is only available if the action type is `:send_alert_message`.
216
- #
217
- def initialize(type, duration_seconds: nil, channel: nil)
218
- @type = type
219
- @duration_seconds = duration_seconds
220
- @channel = channel
221
- end
222
-
223
- #
224
- # Convert the action to hash.
225
- #
226
- # @return [Hash] The action hash.
227
- #
228
- def to_hash
229
- {
230
- type: @type,
231
- metadata: {
232
- channel_id: @channel&.id,
233
- duration_seconds: @duration_seconds,
234
- },
235
- }
236
- end
237
-
238
- #
239
- # Initialize a new action from hash.
240
- # @private
241
- #
242
- # @param [Discorb::Client] client The client.
243
- # @param [Hash] data The action data.
244
- #
245
- def initialize_hash(client, data)
246
- @client = client
247
- _set_data(data)
248
- end
249
-
250
- # @!attribute [r]
251
- # @return [Discorb::Channel] The channel that the alert message is sent to.
252
- # @note This is only available if the action type is `:send_alert_message`.
253
- def channel
254
- @client.channels[@channel_id]
255
- end
256
-
257
- # @private
258
- def _set_data(data)
259
- @type = ACTION_TYPES[data[:type]]
260
- @channel_id = data[:metadata][:channel_id]
261
- @duration_seconds = data[:metadata][:duration_seconds]
262
- end
263
-
264
- def self.from_hash(client, data)
265
- allocate.tap { |action| action.initialize_hash(client, data) }
266
- end
267
- end
268
- end
269
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Discorb
4
+ #
5
+ # Represents a rule of auto moderation.
6
+ #
7
+ class AutoModRule < DiscordModel
8
+ # @return [Hash{Integer => Symbol}] The mapping of trigger types.
9
+ # @private
10
+ TRIGGER_TYPES = {
11
+ 1 => :keyword,
12
+ 2 => :harmful_link,
13
+ 3 => :spam,
14
+ 4 => :keyword_preset,
15
+ 5 => :mention_spam
16
+ }.freeze
17
+ # @return [Hash{Integer => Symbol}] The mapping of preset types.
18
+ # @private
19
+ PRESET_TYPES = { 1 => :profanity, 2 => :sexual_content, 3 => :slurs }.freeze
20
+ # @return [Hash{Integer => Symbol}] The mapping of event types.
21
+ # @private
22
+ EVENT_TYPES = { 1 => :message_send }.freeze
23
+
24
+ # @return [Discorb::Snowflake] The ID of the rule.
25
+ attr_reader :id
26
+ # @return [String] The name of the rule.
27
+ attr_reader :name
28
+ # @return [Boolean] Whether the rule is enabled.
29
+ attr_reader :enabled
30
+ alias enabled? enabled
31
+ # @return [Array<Discorb::AutoModRule::Action>] The actions of the rule.
32
+ attr_reader :actions
33
+ # @return [Array<String>] The keywords that the rule is triggered by.
34
+ # @note This is only available if the trigger type is `:keyword`.
35
+ attr_reader :keyword_filter
36
+ # @return [Array<String>] Substrings which will be exempt from triggering the preset trigger type.
37
+ # @note This is only available if the trigger type is `:keyword_preset`.
38
+ attr_reader :allow_list
39
+ # @return [Integer] Total number of mentions allowed per message.
40
+ # @note This is only available if the trigger type is `:mention_spam`.
41
+ attr_reader :mention_total_limit
42
+
43
+ #
44
+ # Initialize a new auto mod.
45
+ # @private
46
+ #
47
+ # @param [Discorb::Client] client The client.
48
+ # @param [Hash] data The auto mod data.
49
+ #
50
+ def initialize(client, data)
51
+ @client = client
52
+ _set_data(data)
53
+ end
54
+
55
+ # @!attribute [r]
56
+ # @return [Symbol] Returns the type of the preset.
57
+ # @note This is only available if the trigger type is `:keyword_preset`.
58
+ def preset_type
59
+ PRESET_TYPES[@presets_raw]
60
+ end
61
+
62
+ # @!attribute [r]
63
+ # @return [Symbol] Returns the type of the trigger.
64
+ def trigger_type
65
+ TRIGGER_TYPES[@trigger_type_raw]
66
+ end
67
+
68
+ # @!attribute [r]
69
+ # @return [Symbol] Returns the type of the event.
70
+ def event_type
71
+ EVENT_TYPES[@event_type_raw]
72
+ end
73
+
74
+ # @!attribute [r]
75
+ # @macro client_cache
76
+ # @return [Discorb::Member] The member who created the rule.
77
+ def creator
78
+ guild.members[@creator_id]
79
+ end
80
+
81
+ # @!attribute [r]
82
+ # @return [Discorb::Guild] The guild that the rule is in.
83
+ def guild
84
+ @client.guilds[@guild_id]
85
+ end
86
+
87
+ # @!attribute [r]
88
+ # @return [Array<Discorb::Role>] The roles that the rule is exempt from.
89
+ def exempt_roles
90
+ @exempt_roles_id.map { |id| guild.roles[id] }
91
+ end
92
+
93
+ # @!attribute [r]
94
+ # @return [Array<Discorb::Channel>] The channels that the rule is exempt from.
95
+ def exempt_channels
96
+ @exempt_channels_id.map { |id| guild.channels[id] }
97
+ end
98
+
99
+ #
100
+ # Edit the rule.
101
+ # @async
102
+ # @edit
103
+ #
104
+ # @param [String] name The name of the rule.
105
+ # @param [Symbol] event_type The event type of the rule. See {Discorb::AutoModRule::EVENT_TYPES}.
106
+ # @param [Array<Discorb::AutoModRule::Action>] actions The actions of the rule.
107
+ # @param [Boolean] enabled Whether the rule is enabled or not.
108
+ # @param [Array<Discorb::Role>] exempt_roles The roles that are exempt from the rule.
109
+ # @param [Array<Discorb::Channel>] exempt_channels The channels that are exempt from the rule.
110
+ # @param [Array<String>] keyword_filter The keywords to filter.
111
+ # @param [Symbol] presets The preset of the rule. See {Discorb::AutoModRule::PRESET_TYPES}.
112
+ # @param [String] reason The reason for creating the rule.
113
+ #
114
+ # @return [Async::Task<void>] The task.
115
+ #
116
+ def edit(
117
+ name: Discorb::Unset,
118
+ event_type: Discorb::Unset,
119
+ actions: Discorb::Unset,
120
+ enabled: Discorb::Unset,
121
+ exempt_roles: Discorb::Unset,
122
+ exempt_channels: Discorb::Unset,
123
+ keyword_filter: Discorb::Unset,
124
+ presets: Discorb::Unset,
125
+ reason: nil
126
+ )
127
+ # @type var payload: Hash[Symbol, untyped]
128
+ payload = { metadata: {} }
129
+ payload[:name] = name unless name == Discorb::Unset
130
+ payload[:event_type] = EVENT_TYPES.key(event_type) unless event_type ==
131
+ Discorb::Unset
132
+ payload[:actions] = actions unless actions == Discorb::Unset
133
+ payload[:enabled] = enabled unless enabled == Discorb::Unset
134
+ payload[:exempt_roles] = exempt_roles.map(&:id) unless exempt_roles ==
135
+ Discorb::Unset
136
+ payload[:exempt_channels] = exempt_channels.map(
137
+ &:id
138
+ ) unless exempt_channels == Discorb::Unset
139
+ payload[:metadata][
140
+ :keyword_filter
141
+ ] = keyword_filter unless keyword_filter == Discorb::Unset
142
+ payload[:metadata][:presets] = PRESET_TYPES.key(presets) unless presets ==
143
+ Discorb::Unset
144
+
145
+ @client.http.request(
146
+ Route.new(
147
+ "/guilds/#{@guild_id}/automod/rules/#{@id}",
148
+ "//guilds/:guild_id/automod/rules/:id",
149
+ :patch
150
+ ),
151
+ payload,
152
+ audit_log_reason: reason
153
+ )
154
+ end
155
+
156
+ #
157
+ # Delete the rule.
158
+ #
159
+ # @param [String] reason The reason for deleting the rule.
160
+ #
161
+ # @return [Async::Task<void>] The task.
162
+ #
163
+ def delete(reason: nil)
164
+ Async do
165
+ @client.http.request(
166
+ Route.new(
167
+ "/guilds/#{@guild_id}/automod/rules/#{@id}",
168
+ "//guilds/:guild_id/automod/rules/:id",
169
+ :delete
170
+ ),
171
+ audit_log_reason: reason
172
+ )
173
+ end
174
+ end
175
+
176
+ # @private
177
+ def _set_data(data)
178
+ @id = Snowflake.new(data[:id])
179
+ @guild_id = data[:guild_id]
180
+ @name = data[:name]
181
+ @creator_id = data[:creator_id]
182
+ @trigger_type_raw = data[:trigger_type]
183
+ @event_type_raw = data[:event_type]
184
+ @actions =
185
+ data[:actions].map { |action| Action.from_hash(@client, action) }
186
+ case trigger_type
187
+ when :keyword
188
+ @keyword_filter = data[:trigger_metadata][:keyword_filter]
189
+ when :keyword_preset
190
+ @presets_raw = data[:trigger_metadata][:presets]
191
+ @allow_list = data[:trigger_metadata][:allow_list]
192
+ when :mention_spam
193
+ @mention_total_limit = data[:metadata][:mention_total_limit]
194
+ end
195
+ @enabled = data[:enabled]
196
+ @exempt_roles_id = data[:exempt_roles]
197
+ @exempt_channels_id = data[:exempt_channels]
198
+ end
199
+
200
+ #
201
+ # Represents the action of auto moderation.
202
+ #
203
+ class Action < DiscordModel
204
+ # @return [Hash{Integer => Symbol}] The mapping of action types.
205
+ # @private
206
+ ACTION_TYPES = {
207
+ 1 => :block_message,
208
+ 2 => :send_alert_message,
209
+ 3 => :timeout
210
+ }.freeze
211
+
212
+ # @return [Symbol] Returns the type of the action.
213
+ attr_reader :type
214
+ # @return [Integer] The duration of the timeout.
215
+ # @note This is only available if the action type is `:timeout`.
216
+ attr_reader :duration_seconds
217
+
218
+ #
219
+ # Initialize a new action.
220
+ #
221
+ # @param [Symbol] type The type of the action.
222
+ # @param [Integer] duration_seconds The duration of the timeout.
223
+ # This is only available if the action type is `:timeout`.
224
+ # @param [Discorb::Channel] channel The channel that the alert message is sent to.
225
+ # This is only available if the action type is `:send_alert_message`.
226
+ #
227
+ def initialize(type, duration_seconds: nil, channel: nil)
228
+ @type = type
229
+ @duration_seconds = duration_seconds
230
+ @channel = channel
231
+ end
232
+
233
+ #
234
+ # Convert the action to hash.
235
+ #
236
+ # @return [Hash] The action hash.
237
+ #
238
+ def to_hash
239
+ {
240
+ type: @type,
241
+ metadata: {
242
+ channel_id: @channel&.id,
243
+ duration_seconds: @duration_seconds
244
+ }
245
+ }
246
+ end
247
+
248
+ #
249
+ # Initialize a new action from hash.
250
+ # @private
251
+ #
252
+ # @param [Discorb::Client] client The client.
253
+ # @param [Hash] data The action data.
254
+ #
255
+ def initialize_hash(client, data)
256
+ @client = client
257
+ _set_data(data)
258
+ end
259
+
260
+ # @!attribute [r]
261
+ # @return [Discorb::Channel] The channel that the alert message is sent to.
262
+ # @note This is only available if the action type is `:send_alert_message`.
263
+ def channel
264
+ @client.channels[@channel_id]
265
+ end
266
+
267
+ # @private
268
+ def _set_data(data)
269
+ @type = ACTION_TYPES[data[:type]]
270
+ @channel_id = data[:metadata][:channel_id]
271
+ @duration_seconds = data[:metadata][:duration_seconds]
272
+ end
273
+
274
+ def self.from_hash(client, data)
275
+ allocate.tap { |action| action.initialize_hash(client, data) }
276
+ end
277
+ end
278
+ end
279
+ end