discorb 0.18.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,330 +1,379 @@
1
- # frozen_string_literal: true
2
-
3
- module Discorb
4
- #
5
- # Represents an interaction of Discord.
6
- #
7
- class Interaction
8
- #
9
- # A module for response with source.
10
- #
11
- module SourceResponder
12
- # @type instance: Discorb::Interaction
13
-
14
- #
15
- # Response with `DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE`(`5`).
16
- #
17
- # @async
18
- #
19
- # @param [Boolean] ephemeral Whether to make the response ephemeral.
20
- #
21
- # @return [Async::Task<void>] The task.
22
- #
23
- def defer_source(ephemeral: false)
24
- Async do
25
- @client.http.request(
26
- Route.new(
27
- "/interactions/#{@id}/#{@token}/callback",
28
- "//interactions/:interaction_id/:token/callback",
29
- :post
30
- ), {
31
- type: 5,
32
- data: {
33
- flags: (ephemeral ? 1 << 6 : 0),
34
- },
35
- }
36
- ).wait
37
- @defered = true
38
- end
39
- end
40
-
41
- #
42
- # Response with `CHANNEL_MESSAGE_WITH_SOURCE`(`4`).
43
- #
44
- # @async
45
- #
46
- # @param [String] content The content of the response.
47
- # @param [Boolean] tts Whether to send the message as text-to-speech.
48
- # @param [Discorb::Embed] embed The embed to send.
49
- # @param [Array<Discorb::Embed>] embeds The embeds to send. (max: 10)
50
- # @param [Discorb::AllowedMentions] allowed_mentions The allowed mentions to send.
51
- # @param [Discorb::Attachment] attachment The attachment to send.
52
- # @param [Array<Discorb::Attachment>] attachments The attachments to send. (max: 10)
53
- # @param [Array<Discorb::Component>, Array<Array<Discorb::Component>>] components The components to send.
54
- # @param [Boolean] ephemeral Whether to make the response ephemeral.
55
- #
56
- # @return [Discorb::Interaction::SourceResponder::CallbackMessage, Discorb::Webhook::Message]
57
- # The callback message.
58
- #
59
- def post(
60
- content = nil,
61
- tts: false,
62
- embed: nil,
63
- embeds: nil,
64
- allowed_mentions: nil,
65
- attachment: nil,
66
- attachments: nil,
67
- components: nil,
68
- ephemeral: false
69
- )
70
- Async do
71
- payload = {}
72
- payload[:content] = content if content
73
- payload[:tts] = tts
74
- payload[:embeds] = (embeds || [embed]).map { |e| e&.to_hash }.filter { _1 }
75
- payload[:allowed_mentions] =
76
- allowed_mentions&.to_hash(@client.allowed_mentions) || @client.allowed_mentions.to_hash
77
- payload[:components] = Component.to_payload(components) if components
78
- payload[:flags] = (ephemeral ? 1 << 6 : 0)
79
- attachments ||= attachment ? [attachment] : []
80
-
81
- payload[:attachments] = attachments.map.with_index do |a, i|
82
- {
83
- id: i,
84
- filename: a.filename,
85
- description: a.description,
86
- }
87
- end
88
-
89
- ret = if @responded
90
- _resp, data = @client.http.multipart_request(
91
- Route.new(
92
- "/webhooks/#{@application_id}/#{@token}",
93
- "//webhooks/:webhook_id/:token",
94
- :post
95
- ),
96
- payload,
97
- attachments
98
- ).wait
99
- webhook = Webhook::URLWebhook.new("/webhooks/#{@application_id}/#{@token}")
100
- Webhook::Message.new(webhook, data, @client)
101
- elsif @defered
102
- @client.http.multipart_request(
103
- Route.new(
104
- "/webhooks/#{@application_id}/#{@token}/messages/@original",
105
- "//webhooks/:webhook_id/:token/messages/@original",
106
- :patch
107
- ),
108
- payload,
109
- attachments
110
- ).wait
111
- CallbackMessage.new(@client, payload, @application_id, @token)
112
- else
113
- @client.http.multipart_request(
114
- Route.new(
115
- "/interactions/#{@id}/#{@token}/callback",
116
- "//interactions/:interaction_id/:token/callback",
117
- :post
118
- ),
119
- { type: 4, data: payload },
120
- attachments
121
- ).wait
122
- CallbackMessage.new(@client, payload, @application_id, @token)
123
- end
124
- @responded = true
125
- ret
126
- end
127
- end
128
-
129
- #
130
- # Represents of a callback message of interaction.
131
- #
132
- class CallbackMessage
133
- #
134
- # Initializes a new instance of CallbackMessage.
135
- # @private
136
- #
137
- # @param [Client] client The client.
138
- # @param [Hash] data The payload.
139
- # @param [String] application_id The application ID.
140
- # @param [String] token The token.
141
- #
142
- def initialize(client, data, application_id, token)
143
- @client = client
144
- @data = data
145
- @application_id = application_id
146
- @token = token
147
- end
148
-
149
- #
150
- # Edits the callback message.
151
- # @async
152
- # @macro edit
153
- #
154
- # @param [String] content The new content of the message.
155
- # @param [Discorb::Embed] embed The new embed of the message.
156
- # @param [Array<Discorb::Embed>] embeds The new embeds of the message.
157
- # @param [Array<Discorb::Attachment>] attachments The attachments to remain.
158
- # @param [Discorb::Attachment] file The file to send.
159
- # @param [Array<Discorb::Attachment>] files The files to send.
160
- #
161
- # @return [Async::Task<void>] The task.
162
- #
163
- def edit(
164
- content = Discorb::Unset,
165
- embed: Discorb::Unset, embeds: Discorb::Unset,
166
- file: Discorb::Unset, files: Discorb::Unset,
167
- attachments: Discorb::Unset
168
- )
169
- Async do
170
- payload = {}
171
- payload[:content] = content if content != Discorb::Unset
172
- payload[:embeds] = embed ? [embed.to_hash] : [] if embed != Discorb::Unset
173
- payload[:embeds] = embeds.map(&:to_hash) if embeds != Discorb::Unset
174
- payload[:attachments] = attachments.map(&:to_hash) if attachments != Discorb::Unset
175
- files = [file] if file != Discorb::Unset
176
- files = [] if files == Discorb::Unset
177
- @client.http.multipart_request(
178
- Route.new(
179
- "/webhooks/#{@application_id}/#{@token}/messages/@original",
180
- "//webhooks/:webhook_id/:token/messages/@original",
181
- :patch
182
- ),
183
- payload,
184
- files,
185
- ).wait
186
- end
187
- end
188
-
189
- alias modify edit
190
-
191
- #
192
- # Deletes the callback message.
193
- # @async
194
- # @note This will fail if the message is ephemeral.
195
- #
196
- # @return [Async::Task<void>] The task.
197
- #
198
- def delete!
199
- Async do
200
- @client.http.request(Route.new("/webhooks/#{@application_id}/#{@token}/messages/@original",
201
- "//webhooks/:webhook_id/:token/messages/@original", :delete)).wait
202
- end
203
- end
204
-
205
- def inspect
206
- "#<#{self.class.name} application_id=#{@application_id}"
207
- end
208
- end
209
- end
210
-
211
- #
212
- # A module for response with update.
213
- #
214
- module UpdateResponder
215
- # @type instance: Discorb::Interaction
216
-
217
- #
218
- # Response with `DEFERRED_UPDATE_MESSAGE`(`6`).
219
- # @async
220
- #
221
- # @param [Boolean] ephemeral Whether to make the response ephemeral.
222
- #
223
- # @return [Async::Task<void>] The task.
224
- #
225
- def defer_update(ephemeral: false)
226
- Async do
227
- @client.http.request(
228
- Route.new(
229
- "/interactions/#{@id}/#{@token}/callback",
230
- "//interactions/:interaction_id/:token/callback",
231
- :post
232
- ), {
233
- type: 6,
234
- data: {
235
- flags: (ephemeral ? 1 << 6 : 0),
236
- },
237
- }
238
- ).wait
239
- end
240
- end
241
-
242
- #
243
- # Response with `UPDATE_MESSAGE`(`7`).
244
- #
245
- # @async
246
- #
247
- # @param [String] content The content of the response.
248
- # @param [Boolean] tts Whether to send the message as text-to-speech.
249
- # @param [Discorb::Embed] embed The embed to send.
250
- # @param [Array<Discorb::Embed>] embeds The embeds to send. (max: 10)
251
- # @param [Discorb::AllowedMentions] allowed_mentions The allowed mentions to send.
252
- # @param [Discorb::Attachment] attachment The attachment to send.
253
- # @param [Array<Discorb::Attachment>] attachments The attachments to send. (max: 10)
254
- # @param [Array<Discorb::Component>, Array<Array<Discorb::Component>>] components The components to send.
255
- # @param [Boolean] ephemeral Whether to make the response ephemeral.
256
- #
257
- # @return [Async::Task<void>] The task.
258
- #
259
- def edit(
260
- content,
261
- tts: false,
262
- embed: nil,
263
- embeds: nil,
264
- allowed_mentions: nil,
265
- attachment: nil,
266
- attachments: nil,
267
- components: nil,
268
- ephemeral: false
269
- )
270
- Async do
271
- payload = {}
272
- payload[:content] = content if content
273
- payload[:tts] = tts
274
- tmp_embed = if embed
275
- [embed]
276
- elsif embeds
277
- embeds
278
- end
279
- payload[:embeds] = tmp_embed.map(&:to_hash) if tmp_embed
280
- payload[:allowed_mentions] =
281
- allowed_mentions ? allowed_mentions.to_hash(@client.allowed_mentions) : @client.allowed_mentions.to_hash
282
- payload[:components] = Component.to_payload(components) if components
283
- payload[:flags] = (ephemeral ? 1 << 6 : 0)
284
- attachments ||= [attachment] if attachment
285
- payload[:attachments] = attachments.map.with_index do |a, i|
286
- {
287
- id: i,
288
- filename: a.filename,
289
- description: a.description,
290
- }
291
- end
292
- @client.http.multipart_request(
293
- Route.new("/interactions/#{@id}/#{@token}/callback", "//interactions/:interaction_id/:token/callback",
294
- :post), { type: 7, data: payload }, attachments
295
- ).wait
296
- end
297
- end
298
- end
299
-
300
- #
301
- # A module for response with modal.
302
- #
303
- module ModalResponder
304
- # @type instance: Discorb::Interaction
305
-
306
- #
307
- # Response with `MODAL`(`9`).
308
- #
309
- # @param [String] title The title of the modal.
310
- # @param [String] custom_id The custom id of the modal.
311
- # @param [Array<Discorb::TextInput>] components The text inputs to send.
312
- #
313
- # @return [Async::Task<void>] The task.
314
- #
315
- def show_modal(title, custom_id, components)
316
- payload = { title: title, custom_id: custom_id, components: Component.to_payload(components) }
317
- @client.http.request(
318
- Route.new("/interactions/#{@id}/#{@token}/callback", "//interactions/:interaction_id/:token/callback", :post),
319
- { type: 9, data: payload }
320
- ).wait
321
- end
322
- end
323
-
324
- private
325
-
326
- def _set_data(_)
327
- nil
328
- end
329
- end
330
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Discorb
4
+ #
5
+ # Represents an interaction of Discord.
6
+ #
7
+ class Interaction
8
+ #
9
+ # A module for response with source.
10
+ #
11
+ module SourceResponder
12
+ # @type instance: Discorb::Interaction
13
+
14
+ #
15
+ # Response with `DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE`(`5`).
16
+ #
17
+ # @async
18
+ #
19
+ # @param [Boolean] ephemeral Whether to make the response ephemeral.
20
+ #
21
+ # @return [Async::Task<void>] The task.
22
+ #
23
+ def defer_source(ephemeral: false)
24
+ Async do
25
+ @client
26
+ .http
27
+ .request(
28
+ Route.new(
29
+ "/interactions/#{@id}/#{@token}/callback",
30
+ "//interactions/:interaction_id/:token/callback",
31
+ :post
32
+ ),
33
+ { type: 5, data: { flags: (ephemeral ? 1 << 6 : 0) } }
34
+ )
35
+ .wait
36
+ @defered = true
37
+ end
38
+ end
39
+
40
+ #
41
+ # Response with `CHANNEL_MESSAGE_WITH_SOURCE`(`4`).
42
+ #
43
+ # @async
44
+ #
45
+ # @param [String] content The content of the response.
46
+ # @param [Boolean] tts Whether to send the message as text-to-speech.
47
+ # @param [Discorb::Embed] embed The embed to send.
48
+ # @param [Array<Discorb::Embed>] embeds The embeds to send. (max: 10)
49
+ # @param [Discorb::AllowedMentions] allowed_mentions The allowed mentions to send.
50
+ # @param [Discorb::Attachment] attachment The attachment to send.
51
+ # @param [Array<Discorb::Attachment>] attachments The attachments to send. (max: 10)
52
+ # @param [Array<Discorb::Component>, Array<Array<Discorb::Component>>] components The components to send.
53
+ # @param [Boolean] ephemeral Whether to make the response ephemeral.
54
+ #
55
+ # @return [Discorb::Interaction::SourceResponder::CallbackMessage, Discorb::Webhook::Message]
56
+ # The callback message.
57
+ #
58
+ def post(
59
+ content = nil,
60
+ tts: false,
61
+ embed: nil,
62
+ embeds: nil,
63
+ allowed_mentions: nil,
64
+ attachment: nil,
65
+ attachments: nil,
66
+ components: nil,
67
+ ephemeral: false
68
+ )
69
+ Async do
70
+ payload = {}
71
+ payload[:content] = content if content
72
+ payload[:tts] = tts
73
+ payload[:embeds] = (embeds || [embed])
74
+ .map { |e| e&.to_hash }
75
+ .filter { _1 }
76
+ payload[:allowed_mentions] = allowed_mentions&.to_hash(
77
+ @client.allowed_mentions
78
+ ) || @client.allowed_mentions.to_hash
79
+ payload[:components] = Component.to_payload(components) if components
80
+ payload[:flags] = (ephemeral ? 1 << 6 : 0)
81
+ attachments ||= attachment ? [attachment] : []
82
+
83
+ payload[:attachments] = attachments.map.with_index do |a, i|
84
+ { id: i, filename: a.filename, description: a.description }
85
+ end
86
+
87
+ ret =
88
+ if @responded
89
+ _resp, data =
90
+ @client
91
+ .http
92
+ .multipart_request(
93
+ Route.new(
94
+ "/webhooks/#{@application_id}/#{@token}",
95
+ "//webhooks/:webhook_id/:token",
96
+ :post
97
+ ),
98
+ payload,
99
+ attachments
100
+ )
101
+ .wait
102
+ webhook =
103
+ Webhook::URLWebhook.new(
104
+ "/webhooks/#{@application_id}/#{@token}"
105
+ )
106
+ Webhook::Message.new(webhook, data, @client)
107
+ elsif @defered
108
+ @client
109
+ .http
110
+ .multipart_request(
111
+ Route.new(
112
+ "/webhooks/#{@application_id}/#{@token}/messages/@original",
113
+ "//webhooks/:webhook_id/:token/messages/@original",
114
+ :patch
115
+ ),
116
+ payload,
117
+ attachments
118
+ )
119
+ .wait
120
+ CallbackMessage.new(@client, payload, @application_id, @token)
121
+ else
122
+ @client
123
+ .http
124
+ .multipart_request(
125
+ Route.new(
126
+ "/interactions/#{@id}/#{@token}/callback",
127
+ "//interactions/:interaction_id/:token/callback",
128
+ :post
129
+ ),
130
+ { type: 4, data: payload },
131
+ attachments
132
+ )
133
+ .wait
134
+ CallbackMessage.new(@client, payload, @application_id, @token)
135
+ end
136
+ @responded = true
137
+ ret
138
+ end
139
+ end
140
+ end
141
+
142
+ #
143
+ # Represents of a callback message of interaction.
144
+ #
145
+ class CallbackMessage
146
+ #
147
+ # Initializes a new instance of CallbackMessage.
148
+ # @private
149
+ #
150
+ # @param [Client] client The client.
151
+ # @param [Hash] data The payload.
152
+ # @param [String] application_id The application ID.
153
+ # @param [String] token The token.
154
+ #
155
+ def initialize(client, data, application_id, token)
156
+ @client = client
157
+ @data = data
158
+ @application_id = application_id
159
+ @token = token
160
+ end
161
+
162
+ #
163
+ # Edits the callback message.
164
+ # @async
165
+ # @macro edit
166
+ #
167
+ # @param [String] content The new content of the message.
168
+ # @param [Discorb::Embed] embed The new embed of the message.
169
+ # @param [Array<Discorb::Embed>] embeds The new embeds of the message.
170
+ # @param [Array<Discorb::Attachment>] attachments The attachments to remain.
171
+ # @param [Discorb::Attachment] file The file to send.
172
+ # @param [Array<Discorb::Attachment>] files The files to send.
173
+ #
174
+ # @return [Async::Task<void>] The task.
175
+ #
176
+ def edit(
177
+ content = Discorb::Unset,
178
+ embed: Discorb::Unset,
179
+ embeds: Discorb::Unset,
180
+ file: Discorb::Unset,
181
+ files: Discorb::Unset,
182
+ attachments: Discorb::Unset
183
+ )
184
+ Async do
185
+ payload = {}
186
+ payload[:content] = content if content != Discorb::Unset
187
+ payload[:embeds] = embed ? [embed.to_hash] : [] if embed !=
188
+ Discorb::Unset
189
+ payload[:embeds] = embeds.map(&:to_hash) if embeds != Discorb::Unset
190
+ payload[:attachments] = attachments.map(&:to_hash) if attachments !=
191
+ Discorb::Unset
192
+ files = [file] if file != Discorb::Unset
193
+ files = [] if files == Discorb::Unset
194
+ @client
195
+ .http
196
+ .multipart_request(
197
+ Route.new(
198
+ "/webhooks/#{@application_id}/#{@token}/messages/@original",
199
+ "//webhooks/:webhook_id/:token/messages/@original",
200
+ :patch
201
+ ),
202
+ payload,
203
+ files
204
+ )
205
+ .wait
206
+ end
207
+ end
208
+
209
+ alias modify edit
210
+
211
+ #
212
+ # Deletes the callback message.
213
+ # @async
214
+ # @note This will fail if the message is ephemeral.
215
+ #
216
+ # @return [Async::Task<void>] The task.
217
+ #
218
+ def delete
219
+ Async do
220
+ @client
221
+ .http
222
+ .request(
223
+ Route.new(
224
+ "/webhooks/#{@application_id}/#{@token}/messages/@original",
225
+ "//webhooks/:webhook_id/:token/messages/@original",
226
+ :delete
227
+ )
228
+ )
229
+ .wait
230
+ end
231
+ end
232
+
233
+ def inspect
234
+ "#<#{self.class.name} application_id=#{@application_id}"
235
+ end
236
+ end
237
+
238
+ #
239
+ # A module for response with update.
240
+ #
241
+ module UpdateResponder
242
+ # @type instance: Discorb::Interaction
243
+
244
+ #
245
+ # Response with `DEFERRED_UPDATE_MESSAGE`(`6`).
246
+ # @async
247
+ #
248
+ # @param [Boolean] ephemeral Whether to make the response ephemeral.
249
+ #
250
+ # @return [Async::Task<void>] The task.
251
+ #
252
+ def defer_update(ephemeral: false)
253
+ Async do
254
+ @client
255
+ .http
256
+ .request(
257
+ Route.new(
258
+ "/interactions/#{@id}/#{@token}/callback",
259
+ "//interactions/:interaction_id/:token/callback",
260
+ :post
261
+ ),
262
+ { type: 6, data: { flags: (ephemeral ? 1 << 6 : 0) } }
263
+ )
264
+ .wait
265
+ end
266
+ end
267
+
268
+ #
269
+ # Response with `UPDATE_MESSAGE`(`7`).
270
+ #
271
+ # @async
272
+ #
273
+ # @param [String] content The content of the response.
274
+ # @param [Boolean] tts Whether to send the message as text-to-speech.
275
+ # @param [Discorb::Embed] embed The embed to send.
276
+ # @param [Array<Discorb::Embed>] embeds The embeds to send. (max: 10)
277
+ # @param [Discorb::AllowedMentions] allowed_mentions The allowed mentions to send.
278
+ # @param [Discorb::Attachment] attachment The attachment to send.
279
+ # @param [Array<Discorb::Attachment>] attachments The attachments to send. (max: 10)
280
+ # @param [Array<Discorb::Component>, Array<Array<Discorb::Component>>] components The components to send.
281
+ # @param [Boolean] ephemeral Whether to make the response ephemeral.
282
+ #
283
+ # @return [Async::Task<void>] The task.
284
+ #
285
+ def edit(
286
+ content,
287
+ tts: false,
288
+ embed: nil,
289
+ embeds: nil,
290
+ allowed_mentions: nil,
291
+ attachment: nil,
292
+ attachments: nil,
293
+ components: nil,
294
+ ephemeral: false
295
+ )
296
+ Async do
297
+ payload = {}
298
+ payload[:content] = content if content
299
+ payload[:tts] = tts
300
+ tmp_embed =
301
+ if embed
302
+ [embed]
303
+ elsif embeds
304
+ embeds
305
+ end
306
+ payload[:embeds] = tmp_embed.map(&:to_hash) if tmp_embed
307
+ payload[:allowed_mentions] = (
308
+ if allowed_mentions
309
+ allowed_mentions.to_hash(@client.allowed_mentions)
310
+ else
311
+ @client.allowed_mentions.to_hash
312
+ end
313
+ )
314
+ payload[:components] = Component.to_payload(components) if components
315
+ payload[:flags] = (ephemeral ? 1 << 6 : 0)
316
+ attachments ||= [attachment] if attachment
317
+ payload[:attachments] = attachments.map.with_index do |a, i|
318
+ { id: i, filename: a.filename, description: a.description }
319
+ end
320
+ @client
321
+ .http
322
+ .multipart_request(
323
+ Route.new(
324
+ "/interactions/#{@id}/#{@token}/callback",
325
+ "//interactions/:interaction_id/:token/callback",
326
+ :post
327
+ ),
328
+ { type: 7, data: payload },
329
+ attachments
330
+ )
331
+ .wait
332
+ end
333
+ end
334
+ end
335
+
336
+ #
337
+ # A module for response with modal.
338
+ #
339
+ module ModalResponder
340
+ # @type instance: Discorb::Interaction
341
+
342
+ #
343
+ # Response with `MODAL`(`9`).
344
+ #
345
+ # @param [String] title The title of the modal.
346
+ # @param [String] custom_id The custom id of the modal.
347
+ # @param [Array<Discorb::TextInput>] components The text inputs to send.
348
+ #
349
+ # @return [Async::Task<void>] The task.
350
+ #
351
+ def show_modal(title, custom_id, components)
352
+ Async do
353
+ payload = {
354
+ title: title,
355
+ custom_id: custom_id,
356
+ components: Component.to_payload(components)
357
+ }
358
+ @client
359
+ .http
360
+ .request(
361
+ Route.new(
362
+ "/interactions/#{@id}/#{@token}/callback",
363
+ "//interactions/:interaction_id/:token/callback",
364
+ :post
365
+ ),
366
+ { type: 9, data: payload }
367
+ )
368
+ .wait
369
+ end
370
+ end
371
+ end
372
+
373
+ private
374
+
375
+ def _set_data(_)
376
+ nil
377
+ end
378
+ end
379
+ end