discorb 0.19.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 (88) 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 +10 -0
  5. data/Rakefile +482 -454
  6. data/lib/discorb/allowed_mentions.rb +68 -72
  7. data/lib/discorb/app_command/command.rb +466 -398
  8. data/lib/discorb/app_command/common.rb +65 -25
  9. data/lib/discorb/app_command/handler.rb +304 -266
  10. data/lib/discorb/app_command.rb +5 -5
  11. data/lib/discorb/application.rb +198 -197
  12. data/lib/discorb/asset.rb +101 -101
  13. data/lib/discorb/attachment.rb +134 -119
  14. data/lib/discorb/audit_logs.rb +412 -385
  15. data/lib/discorb/automod.rb +279 -269
  16. data/lib/discorb/channel/base.rb +107 -108
  17. data/lib/discorb/channel/category.rb +32 -32
  18. data/lib/discorb/channel/container.rb +44 -44
  19. data/lib/discorb/channel/dm.rb +26 -28
  20. data/lib/discorb/channel/guild.rb +311 -246
  21. data/lib/discorb/channel/stage.rb +156 -140
  22. data/lib/discorb/channel/text.rb +430 -336
  23. data/lib/discorb/channel/thread.rb +374 -325
  24. data/lib/discorb/channel/voice.rb +85 -79
  25. data/lib/discorb/channel.rb +5 -5
  26. data/lib/discorb/client.rb +635 -621
  27. data/lib/discorb/color.rb +178 -182
  28. data/lib/discorb/common.rb +168 -164
  29. data/lib/discorb/components/button.rb +107 -106
  30. data/lib/discorb/components/select_menu.rb +157 -145
  31. data/lib/discorb/components/text_input.rb +103 -106
  32. data/lib/discorb/components.rb +68 -66
  33. data/lib/discorb/dictionary.rb +135 -135
  34. data/lib/discorb/embed.rb +404 -398
  35. data/lib/discorb/emoji.rb +309 -302
  36. data/lib/discorb/emoji_table.rb +16099 -8857
  37. data/lib/discorb/error.rb +131 -131
  38. data/lib/discorb/event.rb +360 -314
  39. data/lib/discorb/event_handler.rb +39 -39
  40. data/lib/discorb/exe/about.rb +17 -17
  41. data/lib/discorb/exe/irb.rb +72 -67
  42. data/lib/discorb/exe/new.rb +323 -315
  43. data/lib/discorb/exe/run.rb +69 -68
  44. data/lib/discorb/exe/setup.rb +57 -55
  45. data/lib/discorb/exe/show.rb +12 -12
  46. data/lib/discorb/extend.rb +25 -45
  47. data/lib/discorb/extension.rb +89 -83
  48. data/lib/discorb/flag.rb +126 -128
  49. data/lib/discorb/gateway.rb +984 -804
  50. data/lib/discorb/gateway_events.rb +670 -638
  51. data/lib/discorb/gateway_requests.rb +45 -48
  52. data/lib/discorb/guild.rb +2115 -1626
  53. data/lib/discorb/guild_template.rb +280 -241
  54. data/lib/discorb/http.rb +247 -232
  55. data/lib/discorb/image.rb +42 -42
  56. data/lib/discorb/integration.rb +169 -161
  57. data/lib/discorb/intents.rb +161 -163
  58. data/lib/discorb/interaction/autocomplete.rb +76 -62
  59. data/lib/discorb/interaction/command.rb +279 -224
  60. data/lib/discorb/interaction/components.rb +114 -104
  61. data/lib/discorb/interaction/modal.rb +36 -32
  62. data/lib/discorb/interaction/response.rb +379 -336
  63. data/lib/discorb/interaction/root.rb +271 -257
  64. data/lib/discorb/interaction.rb +5 -5
  65. data/lib/discorb/invite.rb +154 -153
  66. data/lib/discorb/member.rb +344 -311
  67. data/lib/discorb/message.rb +615 -544
  68. data/lib/discorb/message_meta.rb +197 -186
  69. data/lib/discorb/modules.rb +371 -290
  70. data/lib/discorb/permission.rb +305 -291
  71. data/lib/discorb/presence.rb +352 -346
  72. data/lib/discorb/rate_limit.rb +81 -76
  73. data/lib/discorb/reaction.rb +55 -54
  74. data/lib/discorb/role.rb +272 -240
  75. data/lib/discorb/shard.rb +76 -74
  76. data/lib/discorb/sticker.rb +193 -171
  77. data/lib/discorb/user.rb +205 -188
  78. data/lib/discorb/utils/colored_puts.rb +16 -16
  79. data/lib/discorb/utils.rb +12 -16
  80. data/lib/discorb/voice_state.rb +305 -281
  81. data/lib/discorb/webhook.rb +537 -507
  82. data/lib/discorb.rb +62 -56
  83. data/sig/discorb/application.rbs +2 -0
  84. data/sig/discorb/automod.rbs +10 -1
  85. data/sig/discorb/guild.rbs +2 -0
  86. data/sig/discorb/message.rbs +2 -0
  87. data/sig/discorb/user.rbs +22 -20
  88. metadata +2 -2
@@ -1,336 +1,430 @@
1
- # frozen_string_literal: true
2
-
3
- module Discorb
4
- #
5
- # Represents a text channel.
6
- #
7
- class TextChannel < GuildChannel
8
- # @return [String] The topic of the channel.
9
- attr_reader :topic
10
- # @return [Boolean] Whether the channel is nsfw.
11
- attr_reader :nsfw
12
- # @return [Discorb::Snowflake] The id of the last message.
13
- attr_reader :last_message_id
14
- # @return [Integer] The rate limit per user (Slowmode) in the channel.
15
- attr_reader :rate_limit_per_user
16
- alias slowmode rate_limit_per_user
17
- # @return [Time] The time when the last pinned message was pinned.
18
- attr_reader :last_pin_timestamp
19
- alias last_pinned_at last_pin_timestamp
20
- # @return [Integer] The default value of duration of auto archive.
21
- attr_reader :default_auto_archive_duration
22
-
23
- include Messageable
24
-
25
- @channel_type = 0
26
-
27
- # @!attribute [r] threads
28
- # @return [Array<Discorb::ThreadChannel>] The threads in the channel.
29
- def threads
30
- guild.threads.select { |thread| thread.parent == self }
31
- end
32
-
33
- #
34
- # Edits the channel.
35
- # @async
36
- # @macro edit
37
- #
38
- # @param [String] name The name of the channel.
39
- # @param [Integer] position The position of the channel.
40
- # @param [Discorb::CategoryChannel, nil] category The parent of channel. Specify `nil` to remove the parent.
41
- # @param [Discorb::CategoryChannel, nil] parent Alias of `category`.
42
- # @param [String] topic The topic of the channel.
43
- # @param [Boolean] nsfw Whether the channel is nsfw.
44
- # @param [Boolean] announce Whether the channel is announce channel.
45
- # @param [Integer] rate_limit_per_user The rate limit per user (Slowmode) in the channel.
46
- # @param [Integer] slowmode Alias of `rate_limit_per_user`.
47
- # @param [Integer] default_auto_archive_duration The default auto archive duration of the channel.
48
- # @param [Integer] archive_in Alias of `default_auto_archive_duration`.
49
- # @param [String] reason The reason of editing the channel.
50
- #
51
- # @return [Async::Task<self>] The edited channel.
52
- #
53
- def edit(
54
- name: Discorb::Unset,
55
- position: Discorb::Unset,
56
- category: Discorb::Unset,
57
- parent: Discorb::Unset,
58
- topic: Discorb::Unset,
59
- nsfw: Discorb::Unset,
60
- announce: Discorb::Unset,
61
- rate_limit_per_user: Discorb::Unset,
62
- slowmode: Discorb::Unset,
63
- default_auto_archive_duration: Discorb::Unset,
64
- archive_in: Discorb::Unset,
65
- reason: nil
66
- )
67
- Async do
68
- payload = {}
69
- payload[:name] = name if name != Discorb::Unset
70
- payload[:announce] = announce ? 5 : 0 if announce != Discorb::Unset
71
- payload[:position] = position if position != Discorb::Unset
72
- payload[:topic] = topic || "" if topic != Discorb::Unset
73
- payload[:nsfw] = nsfw if nsfw != Discorb::Unset
74
-
75
- slowmode = rate_limit_per_user if slowmode == Discorb::Unset
76
- payload[:rate_limit_per_user] = slowmode || 0 if slowmode != Discorb::Unset
77
- parent = category if parent == Discorb::Unset
78
- payload[:parent_id] = parent&.id if parent != Discorb::Unset
79
-
80
- default_auto_archive_duration ||= archive_in
81
- if default_auto_archive_duration != Discorb::Unset
82
- payload[:default_auto_archive_duration] =
83
- default_auto_archive_duration
84
- end
85
-
86
- @client.http.request(Route.new("/channels/#{@id}", "//channels/:channel_id", :patch), payload,
87
- audit_log_reason: reason).wait
88
- self
89
- end
90
- end
91
-
92
- alias modify edit
93
-
94
- #
95
- # Create webhook in the channel.
96
- # @async
97
- #
98
- # @param [String] name The name of the webhook.
99
- # @param [Discorb::Image] avatar The avatar of the webhook.
100
- #
101
- # @return [Async::Task<Discorb::Webhook::IncomingWebhook>] The created webhook.
102
- #
103
- def create_webhook(name, avatar: nil)
104
- Async do
105
- payload = {}
106
- payload[:name] = name
107
- payload[:avatar] = avatar.to_s if avatar
108
- _resp, data = @client.http.request(
109
- Route.new("/channels/#{@id}/webhooks", "//channels/:channel_id/webhooks", :post), payload
110
- ).wait
111
- Webhook.from_data(@client, data)
112
- end
113
- end
114
-
115
- #
116
- # Fetch webhooks in the channel.
117
- # @async
118
- #
119
- # @return [Async::Task<Array<Discorb::Webhook>>] The webhooks in the channel.
120
- #
121
- def fetch_webhooks
122
- Async do
123
- _resp, data = @client.http.request(Route.new("/channels/#{@id}/webhooks", "//channels/:channel_id/webhooks",
124
- :get)).wait
125
- data.map { |webhook| Webhook.from_data(@client, webhook) }
126
- end
127
- end
128
-
129
- #
130
- # Bulk delete messages in the channel.
131
- # @async
132
- #
133
- # @param [Discorb::Message] messages The messages to delete.
134
- # @param [Boolean] force Whether to ignore the validation for message (14 days limit).
135
- #
136
- # @return [Async::Task<void>] The task.
137
- #
138
- def delete_messages(*messages, force: false)
139
- Async do
140
- messages = messages.flatten
141
- unless force
142
- time = Time.now
143
- messages.delete_if do |message|
144
- next false unless message.is_a?(Message)
145
-
146
- time - message.created_at > 60 * 60 * 24 * 14
147
- end
148
- end
149
-
150
- message_ids = messages.map { |m| Discorb::Utils.try(m, :id).to_s }
151
-
152
- @client.http.request(
153
- Route.new("/channels/#{@id}/messages/bulk-delete", "//channels/:channel_id/messages/bulk-delete",
154
- :post), { messages: message_ids }
155
- ).wait
156
- end
157
- end
158
-
159
- alias bulk_delete delete_messages
160
- alias destroy_messages delete_messages
161
-
162
- #
163
- # Follow the existing announcement channel.
164
- # @async
165
- #
166
- # @param [Discorb::NewsChannel] target The channel to follow.
167
- # @param [String] reason The reason of following the channel.
168
- #
169
- # @return [Async::Task<void>] The task.
170
- #
171
- def follow_from(target, reason: nil)
172
- Async do
173
- @client.http.request(Route.new("/channels/#{target.id}/followers", "//channels/:channel_id/followers", :post),
174
- { webhook_channel_id: @id }, audit_log_reason: reason).wait
175
- end
176
- end
177
-
178
- #
179
- # Start thread in the channel.
180
- # @async
181
- #
182
- # @param [String] name The name of the thread.
183
- # @param [Discorb::Message] message The message to start the thread.
184
- # @param [:hour, :day, :three_days, :week] auto_archive_duration The duration of auto-archiving.
185
- # @param [Boolean] public Whether the thread is public.
186
- # @param [Integer] rate_limit_per_user The rate limit per user.
187
- # @param [Integer] slowmode Alias of `rate_limit_per_user`.
188
- # @param [String] reason The reason of starting the thread.
189
- #
190
- # @return [Async::Task<Discorb::ThreadChannel>] The started thread.
191
- #
192
- def start_thread(
193
- name,
194
- message: nil,
195
- auto_archive_duration: nil,
196
- public: true,
197
- rate_limit_per_user: nil,
198
- slowmode: nil,
199
- reason: nil
200
- )
201
- auto_archive_duration ||= @default_auto_archive_duration
202
- Async do
203
- _resp, data = if message.nil?
204
- @client.http.request(
205
- Route.new("/channels/#{@id}/threads", "//channels/:channel_id/threads", :post),
206
- {
207
- name: name,
208
- auto_archive_duration: auto_archive_duration,
209
- type: public ? 11 : 10,
210
- rate_limit_per_user: rate_limit_per_user || slowmode,
211
- },
212
- audit_log_reason: reason,
213
- ).wait
214
- else
215
- @client.http.request(
216
- Route.new("/channels/#{@id}/messages/#{Utils.try(message, :id)}/threads",
217
- "//channels/:channel_id/messages/:message_id/threads", :post),
218
- {
219
- name: name,
220
- auto_archive_duration: auto_archive_duration,
221
- },
222
- audit_log_reason: reason,
223
- ).wait
224
- end
225
- Channel.make_channel(@client, data)
226
- end
227
- end
228
-
229
- alias create_thread start_thread
230
-
231
- #
232
- # Fetch archived threads in the channel.
233
- # @async
234
- #
235
- # @return [Async::Task<Array<Discorb::ThreadChannel>>] The archived threads in the channel.
236
- #
237
- def fetch_archived_public_threads
238
- Async do
239
- _resp, data = @client.http.request(Route.new("/channels/#{@id}/threads/archived/public",
240
- "//channels/:channel_id/threads/archived/public", :get)).wait
241
- data.map { |thread| Channel.make_channel(@client, thread) }
242
- end
243
- end
244
-
245
- #
246
- # Fetch archived private threads in the channel.
247
- # @async
248
- #
249
- # @return [Async::Task<Array<Discorb::ThreadChannel>>] The archived private threads in the channel.
250
- #
251
- def fetch_archived_private_threads
252
- Async do
253
- _resp, data = @client.http.request(Route.new("/channels/#{@id}/threads/archived/private",
254
- "//channels/:channel_id/threads/archived/private", :get)).wait
255
- data.map { |thread| Channel.make_channel(@client, thread) }
256
- end
257
- end
258
-
259
- #
260
- # Fetch joined archived private threads in the channel.
261
- # @async
262
- #
263
- # @param [Integer] limit The limit of threads to fetch.
264
- # @param [Time] before The time before which the threads are created.
265
- #
266
- # @return [Async::Task<Array<Discorb::ThreadChannel>>] The joined archived private threads in the channel.
267
- #
268
- def fetch_joined_archived_private_threads(limit: nil, before: nil)
269
- Async do
270
- if limit.nil?
271
- before = Time.now
272
- threads = []
273
- loop do
274
- _resp, data = @client.http.request(
275
- Route.new(
276
- "/channels/#{@id}/users/@me/threads/archived/private?before=#{before.iso8601}",
277
- "//channels/:channel_id/users/@me/threads/archived/private",
278
- :get
279
- )
280
- ).wait
281
- threads += data[:threads].map { |thread| Channel.make_channel(@client, thread) }
282
-
283
- break unless data[:has_more]
284
-
285
- before = Snowflake.new(data[:threads][-1][:id]).timestamp
286
- end
287
- threads
288
- else
289
- _resp, data = @client.http.request(
290
- Route.new(
291
- "/channels/#{@id}/users/@me/threads/archived/private?limit=#{limit}&before=#{before.iso8601}",
292
- "//channels/:channel_id/users/@me/threads/archived/private",
293
- :get
294
- )
295
- ).wait
296
- data.map { |thread| Channel.make_channel(@client, thread) }
297
- end
298
- end
299
- end
300
-
301
- private
302
-
303
- def _set_data(data)
304
- @topic = data[:topic]
305
- @nsfw = data[:nsfw]
306
- @last_message_id = data[:last_message_id]
307
- @rate_limit_per_user = data[:rate_limit_per_user]
308
- @last_pin_timestamp = data[:last_pin_timestamp] && Time.iso8601(data[:last_pin_timestamp])
309
- @default_auto_archive_duration = data[:default_auto_archive_duration]
310
- super
311
- end
312
- end
313
-
314
- #
315
- # Represents a news channel (announcement channel).
316
- #
317
- class NewsChannel < TextChannel
318
- @channel_type = 5
319
-
320
- #
321
- # Follow the existing announcement channel from self.
322
- # @async
323
- #
324
- # @param [Discorb::TextChannel] target The channel to follow to.
325
- # @param [String] reason The reason of following the channel.
326
- #
327
- # @return [Async::Task<void>] The task.
328
- #
329
- def follow_to(target, reason: nil)
330
- Async do
331
- @client.http.request(Route.new("/channels/#{@id}/followers", "//channels/:channel_id/followers", :post),
332
- { webhook_channel_id: target.id }, audit_log_reason: reason).wait
333
- end
334
- end
335
- end
336
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Discorb
4
+ #
5
+ # Represents a text channel.
6
+ #
7
+ class TextChannel < GuildChannel
8
+ # @return [String] The topic of the channel.
9
+ attr_reader :topic
10
+ # @return [Boolean] Whether the channel is nsfw.
11
+ attr_reader :nsfw
12
+ # @return [Discorb::Snowflake] The id of the last message.
13
+ attr_reader :last_message_id
14
+ # @return [Integer] The rate limit per user (Slowmode) in the channel.
15
+ attr_reader :rate_limit_per_user
16
+ alias slowmode rate_limit_per_user
17
+ # @return [Time] The time when the last pinned message was pinned.
18
+ attr_reader :last_pin_timestamp
19
+ alias last_pinned_at last_pin_timestamp
20
+ # @return [Integer] The default value of duration of auto archive.
21
+ attr_reader :default_auto_archive_duration
22
+
23
+ include Messageable
24
+
25
+ @channel_type = 0
26
+
27
+ # @!attribute [r] threads
28
+ # @return [Array<Discorb::ThreadChannel>] The threads in the channel.
29
+ def threads
30
+ guild.threads.select { |thread| thread.parent == self }
31
+ end
32
+
33
+ #
34
+ # Edits the channel.
35
+ # @async
36
+ # @macro edit
37
+ #
38
+ # @param [String] name The name of the channel.
39
+ # @param [Integer] position The position of the channel.
40
+ # @param [Discorb::CategoryChannel, nil] category The parent of channel. Specify `nil` to remove the parent.
41
+ # @param [Discorb::CategoryChannel, nil] parent Alias of `category`.
42
+ # @param [String] topic The topic of the channel.
43
+ # @param [Boolean] nsfw Whether the channel is nsfw.
44
+ # @param [Boolean] announce Whether the channel is announce channel.
45
+ # @param [Integer] rate_limit_per_user The rate limit per user (Slowmode) in the channel.
46
+ # @param [Integer] slowmode Alias of `rate_limit_per_user`.
47
+ # @param [Integer] default_auto_archive_duration The default auto archive duration of the channel.
48
+ # @param [Integer] archive_in Alias of `default_auto_archive_duration`.
49
+ # @param [String] reason The reason of editing the channel.
50
+ #
51
+ # @return [Async::Task<self>] The edited channel.
52
+ #
53
+ def edit(
54
+ name: Discorb::Unset,
55
+ position: Discorb::Unset,
56
+ category: Discorb::Unset,
57
+ parent: Discorb::Unset,
58
+ topic: Discorb::Unset,
59
+ nsfw: Discorb::Unset,
60
+ announce: Discorb::Unset,
61
+ rate_limit_per_user: Discorb::Unset,
62
+ slowmode: Discorb::Unset,
63
+ default_auto_archive_duration: Discorb::Unset,
64
+ archive_in: Discorb::Unset,
65
+ reason: nil
66
+ )
67
+ Async do
68
+ payload = {}
69
+ payload[:name] = name if name != Discorb::Unset
70
+ payload[:announce] = announce ? 5 : 0 if announce != Discorb::Unset
71
+ payload[:position] = position if position != Discorb::Unset
72
+ payload[:topic] = topic || "" if topic != Discorb::Unset
73
+ payload[:nsfw] = nsfw if nsfw != Discorb::Unset
74
+
75
+ slowmode = rate_limit_per_user if slowmode == Discorb::Unset
76
+ payload[:rate_limit_per_user] = slowmode || 0 if slowmode !=
77
+ Discorb::Unset
78
+ parent = category if parent == Discorb::Unset
79
+ payload[:parent_id] = parent&.id if parent != Discorb::Unset
80
+
81
+ default_auto_archive_duration ||= archive_in
82
+ if default_auto_archive_duration != Discorb::Unset
83
+ payload[
84
+ :default_auto_archive_duration
85
+ ] = default_auto_archive_duration
86
+ end
87
+
88
+ @client
89
+ .http
90
+ .request(
91
+ Route.new("/channels/#{@id}", "//channels/:channel_id", :patch),
92
+ payload,
93
+ audit_log_reason: reason
94
+ )
95
+ .wait
96
+ self
97
+ end
98
+ end
99
+
100
+ alias modify edit
101
+
102
+ #
103
+ # Create webhook in the channel.
104
+ # @async
105
+ #
106
+ # @param [String] name The name of the webhook.
107
+ # @param [Discorb::Image] avatar The avatar of the webhook.
108
+ #
109
+ # @return [Async::Task<Discorb::Webhook::IncomingWebhook>] The created webhook.
110
+ #
111
+ def create_webhook(name, avatar: nil)
112
+ Async do
113
+ payload = {}
114
+ payload[:name] = name
115
+ payload[:avatar] = avatar.to_s if avatar
116
+ _resp, data =
117
+ @client
118
+ .http
119
+ .request(
120
+ Route.new(
121
+ "/channels/#{@id}/webhooks",
122
+ "//channels/:channel_id/webhooks",
123
+ :post
124
+ ),
125
+ payload
126
+ )
127
+ .wait
128
+ Webhook.from_data(@client, data)
129
+ end
130
+ end
131
+
132
+ #
133
+ # Fetch webhooks in the channel.
134
+ # @async
135
+ #
136
+ # @return [Async::Task<Array<Discorb::Webhook>>] The webhooks in the channel.
137
+ #
138
+ def fetch_webhooks
139
+ Async do
140
+ _resp, data =
141
+ @client
142
+ .http
143
+ .request(
144
+ Route.new(
145
+ "/channels/#{@id}/webhooks",
146
+ "//channels/:channel_id/webhooks",
147
+ :get
148
+ )
149
+ )
150
+ .wait
151
+ data.map { |webhook| Webhook.from_data(@client, webhook) }
152
+ end
153
+ end
154
+
155
+ #
156
+ # Bulk delete messages in the channel.
157
+ # @async
158
+ #
159
+ # @param [Discorb::Message] messages The messages to delete.
160
+ # @param [Boolean] force Whether to ignore the validation for message (14 days limit).
161
+ #
162
+ # @return [Async::Task<void>] The task.
163
+ #
164
+ def delete_messages(*messages, force: false)
165
+ Async do
166
+ messages = messages.flatten
167
+ unless force
168
+ time = Time.now
169
+ messages.delete_if do |message|
170
+ next false unless message.is_a?(Message)
171
+
172
+ time - message.created_at > 60 * 60 * 24 * 14
173
+ end
174
+ end
175
+
176
+ message_ids = messages.map { |m| Discorb::Utils.try(m, :id).to_s }
177
+
178
+ @client
179
+ .http
180
+ .request(
181
+ Route.new(
182
+ "/channels/#{@id}/messages/bulk-delete",
183
+ "//channels/:channel_id/messages/bulk-delete",
184
+ :post
185
+ ),
186
+ { messages: message_ids }
187
+ )
188
+ .wait
189
+ end
190
+ end
191
+
192
+ alias bulk_delete delete_messages
193
+ alias destroy_messages delete_messages
194
+
195
+ #
196
+ # Follow the existing announcement channel.
197
+ # @async
198
+ #
199
+ # @param [Discorb::NewsChannel] target The channel to follow.
200
+ # @param [String] reason The reason of following the channel.
201
+ #
202
+ # @return [Async::Task<void>] The task.
203
+ #
204
+ def follow_from(target, reason: nil)
205
+ Async do
206
+ @client
207
+ .http
208
+ .request(
209
+ Route.new(
210
+ "/channels/#{target.id}/followers",
211
+ "//channels/:channel_id/followers",
212
+ :post
213
+ ),
214
+ { webhook_channel_id: @id },
215
+ audit_log_reason: reason
216
+ )
217
+ .wait
218
+ end
219
+ end
220
+
221
+ #
222
+ # Start thread in the channel.
223
+ # @async
224
+ #
225
+ # @param [String] name The name of the thread.
226
+ # @param [Discorb::Message] message The message to start the thread.
227
+ # @param [:hour, :day, :three_days, :week] auto_archive_duration The duration of auto-archiving.
228
+ # @param [Boolean] public Whether the thread is public.
229
+ # @param [Integer] rate_limit_per_user The rate limit per user.
230
+ # @param [Integer] slowmode Alias of `rate_limit_per_user`.
231
+ # @param [String] reason The reason of starting the thread.
232
+ #
233
+ # @return [Async::Task<Discorb::ThreadChannel>] The started thread.
234
+ #
235
+ def start_thread(
236
+ name,
237
+ message: nil,
238
+ auto_archive_duration: nil,
239
+ public: true,
240
+ rate_limit_per_user: nil,
241
+ slowmode: nil,
242
+ reason: nil
243
+ )
244
+ auto_archive_duration ||= @default_auto_archive_duration
245
+ Async do
246
+ _resp, data =
247
+ if message.nil?
248
+ @client
249
+ .http
250
+ .request(
251
+ Route.new(
252
+ "/channels/#{@id}/threads",
253
+ "//channels/:channel_id/threads",
254
+ :post
255
+ ),
256
+ {
257
+ name: name,
258
+ auto_archive_duration: auto_archive_duration,
259
+ type: public ? 11 : 10,
260
+ rate_limit_per_user: rate_limit_per_user || slowmode
261
+ },
262
+ audit_log_reason: reason
263
+ )
264
+ .wait
265
+ else
266
+ @client
267
+ .http
268
+ .request(
269
+ Route.new(
270
+ "/channels/#{@id}/messages/#{Utils.try(message, :id)}/threads",
271
+ "//channels/:channel_id/messages/:message_id/threads",
272
+ :post
273
+ ),
274
+ { name: name, auto_archive_duration: auto_archive_duration },
275
+ audit_log_reason: reason
276
+ )
277
+ .wait
278
+ end
279
+ Channel.make_channel(@client, data)
280
+ end
281
+ end
282
+
283
+ alias create_thread start_thread
284
+
285
+ #
286
+ # Fetch archived threads in the channel.
287
+ # @async
288
+ #
289
+ # @return [Async::Task<Array<Discorb::ThreadChannel>>] The archived threads in the channel.
290
+ #
291
+ def fetch_archived_public_threads
292
+ Async do
293
+ _resp, data =
294
+ @client
295
+ .http
296
+ .request(
297
+ Route.new(
298
+ "/channels/#{@id}/threads/archived/public",
299
+ "//channels/:channel_id/threads/archived/public",
300
+ :get
301
+ )
302
+ )
303
+ .wait
304
+ data.map { |thread| Channel.make_channel(@client, thread) }
305
+ end
306
+ end
307
+
308
+ #
309
+ # Fetch archived private threads in the channel.
310
+ # @async
311
+ #
312
+ # @return [Async::Task<Array<Discorb::ThreadChannel>>] The archived private threads in the channel.
313
+ #
314
+ def fetch_archived_private_threads
315
+ Async do
316
+ _resp, data =
317
+ @client
318
+ .http
319
+ .request(
320
+ Route.new(
321
+ "/channels/#{@id}/threads/archived/private",
322
+ "//channels/:channel_id/threads/archived/private",
323
+ :get
324
+ )
325
+ )
326
+ .wait
327
+ data.map { |thread| Channel.make_channel(@client, thread) }
328
+ end
329
+ end
330
+
331
+ #
332
+ # Fetch joined archived private threads in the channel.
333
+ # @async
334
+ #
335
+ # @param [Integer] limit The limit of threads to fetch.
336
+ # @param [Time] before The time before which the threads are created.
337
+ #
338
+ # @return [Async::Task<Array<Discorb::ThreadChannel>>] The joined archived private threads in the channel.
339
+ #
340
+ def fetch_joined_archived_private_threads(limit: nil, before: nil)
341
+ Async do
342
+ if limit.nil?
343
+ before = Time.now
344
+ threads = []
345
+ loop do
346
+ _resp, data =
347
+ @client
348
+ .http
349
+ .request(
350
+ Route.new(
351
+ "/channels/#{@id}/users/@me/threads/archived/private?before=#{before.iso8601}",
352
+ "//channels/:channel_id/users/@me/threads/archived/private",
353
+ :get
354
+ )
355
+ )
356
+ .wait
357
+ threads +=
358
+ data[:threads].map do |thread|
359
+ Channel.make_channel(@client, thread)
360
+ end
361
+
362
+ break unless data[:has_more]
363
+
364
+ before = Snowflake.new(data[:threads][-1][:id]).timestamp
365
+ end
366
+ threads
367
+ else
368
+ _resp, data =
369
+ @client
370
+ .http
371
+ .request(
372
+ Route.new(
373
+ "/channels/#{@id}/users/@me/threads/archived/private?limit=#{limit}&before=#{before.iso8601}",
374
+ "//channels/:channel_id/users/@me/threads/archived/private",
375
+ :get
376
+ )
377
+ )
378
+ .wait
379
+ data.map { |thread| Channel.make_channel(@client, thread) }
380
+ end
381
+ end
382
+ end
383
+
384
+ private
385
+
386
+ def _set_data(data)
387
+ @topic = data[:topic]
388
+ @nsfw = data[:nsfw]
389
+ @last_message_id = data[:last_message_id]
390
+ @rate_limit_per_user = data[:rate_limit_per_user]
391
+ @last_pin_timestamp =
392
+ data[:last_pin_timestamp] && Time.iso8601(data[:last_pin_timestamp])
393
+ @default_auto_archive_duration = data[:default_auto_archive_duration]
394
+ super
395
+ end
396
+ end
397
+
398
+ #
399
+ # Represents a news channel (announcement channel).
400
+ #
401
+ class NewsChannel < TextChannel
402
+ @channel_type = 5
403
+
404
+ #
405
+ # Follow the existing announcement channel from self.
406
+ # @async
407
+ #
408
+ # @param [Discorb::TextChannel] target The channel to follow to.
409
+ # @param [String] reason The reason of following the channel.
410
+ #
411
+ # @return [Async::Task<void>] The task.
412
+ #
413
+ def follow_to(target, reason: nil)
414
+ Async do
415
+ @client
416
+ .http
417
+ .request(
418
+ Route.new(
419
+ "/channels/#{@id}/followers",
420
+ "//channels/:channel_id/followers",
421
+ :post
422
+ ),
423
+ { webhook_channel_id: target.id },
424
+ audit_log_reason: reason
425
+ )
426
+ .wait
427
+ end
428
+ end
429
+ end
430
+ end