discorb 0.17.1 → 0.19.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/lint.yml +1 -3
  3. data/.github/workflows/validate.yml +21 -0
  4. data/Changelog.md +25 -0
  5. data/Gemfile +6 -0
  6. data/README.md +2 -1
  7. data/Rakefile +205 -98
  8. data/Steepfile +30 -0
  9. data/docs/application_command.md +1 -0
  10. data/docs/events.md +44 -8
  11. data/docs/tutorial.md +7 -7
  12. data/docs/voice_events.md +8 -8
  13. data/examples/commands/message.rb +12 -7
  14. data/examples/commands/permission.rb +2 -1
  15. data/examples/commands/slash.rb +23 -19
  16. data/examples/commands/user.rb +15 -12
  17. data/examples/components/authorization_button.rb +2 -1
  18. data/examples/components/select_menu.rb +4 -1
  19. data/examples/extension/main.rb +1 -0
  20. data/examples/extension/message_expander.rb +1 -0
  21. data/examples/sig/commands/message.rbs +5 -0
  22. data/examples/simple/eval.rb +1 -0
  23. data/examples/simple/ping_pong.rb +1 -0
  24. data/examples/simple/rolepanel.rb +16 -5
  25. data/examples/simple/shard.rb +2 -1
  26. data/examples/simple/wait_for_message.rb +3 -0
  27. data/exe/discorb +3 -3
  28. data/lib/discorb/allowed_mentions.rb +1 -1
  29. data/lib/discorb/app_command/command.rb +16 -13
  30. data/lib/discorb/app_command/handler.rb +21 -6
  31. data/lib/discorb/audit_logs.rb +6 -2
  32. data/lib/discorb/automod.rb +269 -0
  33. data/lib/discorb/channel/guild.rb +5 -4
  34. data/lib/discorb/channel/stage.rb +1 -1
  35. data/lib/discorb/channel/text.rb +14 -23
  36. data/lib/discorb/channel/thread.rb +15 -11
  37. data/lib/discorb/client.rb +15 -15
  38. data/lib/discorb/color.rb +37 -60
  39. data/lib/discorb/common.rb +1 -1
  40. data/lib/discorb/dictionary.rb +1 -1
  41. data/lib/discorb/embed.rb +5 -4
  42. data/lib/discorb/emoji.rb +4 -4
  43. data/lib/discorb/event.rb +2 -2
  44. data/lib/discorb/exe/about.rb +1 -1
  45. data/lib/discorb/exe/new.rb +1 -5
  46. data/lib/discorb/extension.rb +0 -4
  47. data/lib/discorb/flag.rb +2 -2
  48. data/lib/discorb/gateway.rb +38 -589
  49. data/lib/discorb/gateway_events.rb +638 -0
  50. data/lib/discorb/guild.rb +138 -19
  51. data/lib/discorb/guild_template.rb +3 -3
  52. data/lib/discorb/http.rb +47 -25
  53. data/lib/discorb/integration.rb +2 -2
  54. data/lib/discorb/intents.rb +27 -18
  55. data/lib/discorb/interaction/command.rb +14 -10
  56. data/lib/discorb/interaction/response.rb +150 -84
  57. data/lib/discorb/interaction/root.rb +139 -0
  58. data/lib/discorb/invite.rb +1 -1
  59. data/lib/discorb/member.rb +3 -3
  60. data/lib/discorb/message.rb +17 -15
  61. data/lib/discorb/message_meta.rb +2 -3
  62. data/lib/discorb/modules.rb +5 -4
  63. data/lib/discorb/permission.rb +3 -1
  64. data/lib/discorb/presence.rb +4 -2
  65. data/lib/discorb/reaction.rb +2 -2
  66. data/lib/discorb/role.rb +3 -3
  67. data/lib/discorb/shard.rb +1 -1
  68. data/lib/discorb/sticker.rb +5 -5
  69. data/lib/discorb/user.rb +2 -2
  70. data/lib/discorb/voice_state.rb +8 -8
  71. data/lib/discorb/webhook.rb +20 -11
  72. data/lib/discorb.rb +2 -2
  73. data/rbs_collection.lock.yaml +88 -96
  74. data/rbs_collection.yaml +21 -17
  75. data/sig/async.rbs +11 -5
  76. data/sig/discorb/activity.rbs +24 -0
  77. data/sig/discorb/allowed_mentions.rbs +45 -0
  78. data/sig/discorb/app_command/base.rbs +288 -0
  79. data/sig/discorb/app_command/handler.rbs +171 -0
  80. data/sig/discorb/application.rbs +146 -0
  81. data/sig/discorb/asset.rbs +34 -0
  82. data/sig/discorb/attachment.rbs +99 -0
  83. data/sig/discorb/audit_log.rbs +238 -0
  84. data/sig/discorb/automod.rbs +145 -0
  85. data/sig/discorb/avatar.rbs +27 -0
  86. data/sig/discorb/channel/base.rbs +186 -0
  87. data/sig/discorb/channel/category.rbs +57 -0
  88. data/sig/discorb/channel/container.rbs +33 -0
  89. data/sig/discorb/channel/dm.rbs +14 -0
  90. data/sig/discorb/channel/news.rbs +20 -0
  91. data/sig/discorb/channel/stage.rbs +81 -0
  92. data/sig/discorb/channel/text.rbs +158 -0
  93. data/sig/discorb/channel/thread.rbs +196 -0
  94. data/sig/discorb/channel/voice.rbs +43 -0
  95. data/sig/discorb/client.rbs +2496 -0
  96. data/sig/discorb/color.rbs +148 -0
  97. data/sig/discorb/component/base.rbs +29 -0
  98. data/sig/discorb/component/button.rbs +67 -0
  99. data/sig/discorb/component/select_menu.rbs +111 -0
  100. data/sig/discorb/component/text_input.rbs +70 -0
  101. data/sig/discorb/connectable.rbs +8 -0
  102. data/sig/discorb/custom_emoji.rbs +94 -0
  103. data/sig/discorb/dictionary.rbs +87 -0
  104. data/sig/discorb/discord_model.rbs +17 -0
  105. data/sig/discorb/embed.rbs +286 -0
  106. data/sig/discorb/emoji.rbs +14 -0
  107. data/sig/discorb/error.rbs +73 -0
  108. data/sig/discorb/event_handler.rbs +28 -0
  109. data/sig/discorb/extension.rbs +1735 -0
  110. data/sig/discorb/flag.rbs +74 -0
  111. data/sig/discorb/gateway.rbs +486 -0
  112. data/sig/discorb/guild.rbs +872 -0
  113. data/sig/discorb/guild_template.rbs +174 -0
  114. data/sig/discorb/http.rbs +150 -0
  115. data/sig/discorb/image.rbs +22 -0
  116. data/sig/discorb/integration.rbs +118 -0
  117. data/sig/discorb/intents.rbs +98 -0
  118. data/sig/discorb/interaction/autocomplete.rbs +9 -0
  119. data/sig/discorb/interaction/base.rbs +102 -0
  120. data/sig/discorb/interaction/command.rbs +66 -0
  121. data/sig/discorb/interaction/message_component.rbs +139 -0
  122. data/sig/discorb/interaction/modal.rbs +49 -0
  123. data/sig/discorb/interaction/responder.rbs +157 -0
  124. data/sig/discorb/invite.rbs +86 -0
  125. data/sig/discorb/member.rbs +189 -0
  126. data/sig/discorb/message.rbs +474 -0
  127. data/sig/discorb/messageable.rbs +150 -0
  128. data/sig/discorb/partial_emoji.rbs +38 -0
  129. data/sig/discorb/permissions.rbs +156 -0
  130. data/sig/discorb/presence.rbs +239 -0
  131. data/sig/discorb/reaction.rbs +37 -0
  132. data/sig/discorb/role.rbs +151 -0
  133. data/sig/discorb/scheduled_event.rbs +149 -0
  134. data/sig/discorb/shard.rbs +63 -0
  135. data/sig/discorb/snowflake.rbs +58 -0
  136. data/sig/discorb/stage_instance.rbs +69 -0
  137. data/sig/discorb/sticker.rbs +116 -0
  138. data/sig/discorb/system_channel_flag.rbs +17 -0
  139. data/sig/discorb/unicode_emoji.rbs +53 -0
  140. data/sig/discorb/user.rbs +95 -0
  141. data/sig/discorb/utils.rbs +8 -0
  142. data/sig/discorb/voice_region.rbs +30 -0
  143. data/sig/discorb/voice_state.rbs +71 -0
  144. data/sig/discorb/webhook.rbs +338 -0
  145. data/sig/discorb/welcome_screen.rbs +79 -0
  146. data/sig/discorb.rbs +5 -8661
  147. data/sig/manifest.yaml +3 -0
  148. data/sig/override.rbs +21 -0
  149. metadata +80 -3
@@ -0,0 +1,269 @@
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
@@ -34,7 +34,7 @@ module Discorb
34
34
  # @return [-1, 0, 1] -1 if the channel is at lower than the other, 1 if the channel is at highter than the other.
35
35
  #
36
36
  def <=>(other)
37
- return 0 unless other.respond_to?(:position)
37
+ return nil unless other.respond_to?(:position)
38
38
 
39
39
  @position <=> other.position
40
40
  end
@@ -89,7 +89,7 @@ module Discorb
89
89
  #
90
90
  # @return [Async::Task<self>] The deleted channel.
91
91
  #
92
- def delete!(reason: nil)
92
+ def delete(reason: nil)
93
93
  Async do
94
94
  @client.http.request(Route.new(base_url.wait.to_s, "//webhooks/:webhook_id/:token", :delete), {},
95
95
  audit_log_reason: reason).wait
@@ -98,8 +98,8 @@ module Discorb
98
98
  end
99
99
  end
100
100
 
101
- alias close! delete!
102
- alias destroy! delete!
101
+ alias close delete
102
+ alias destroy delete
103
103
 
104
104
  #
105
105
  # Moves the channel to another position.
@@ -114,6 +114,7 @@ module Discorb
114
114
  #
115
115
  def move(position, lock_permissions: false, parent: Discorb::Unset, reason: nil)
116
116
  Async do
117
+ # @type var payload: Hash[Symbol, untyped]
117
118
  payload = {
118
119
  position: position,
119
120
  }
@@ -120,7 +120,7 @@ module Discorb
120
120
  end
121
121
 
122
122
  def speakers
123
- voice_states.filter { |state| !state.suppress? }.map(&:member)
123
+ voice_states.reject(&:suppress?).map(&:member)
124
124
  end
125
125
 
126
126
  def audiences
@@ -22,15 +22,6 @@ module Discorb
22
22
 
23
23
  include Messageable
24
24
 
25
- # @return [{Integer => Symbol}] The auto archive duration map.
26
- # @private
27
- DEFAULT_AUTO_ARCHIVE_DURATION = {
28
- 60 => :hour,
29
- 1440 => :day,
30
- 4320 => :three_days,
31
- 10080 => :week,
32
- }.freeze
33
-
34
25
  @channel_type = 0
35
26
 
36
27
  # @!attribute [r] threads
@@ -131,7 +122,7 @@ module Discorb
131
122
  Async do
132
123
  _resp, data = @client.http.request(Route.new("/channels/#{@id}/webhooks", "//channels/:channel_id/webhooks",
133
124
  :get)).wait
134
- data.map { |webhook| Webhook.new([@client, webhook]) }
125
+ data.map { |webhook| Webhook.from_data(@client, webhook) }
135
126
  end
136
127
  end
137
128
 
@@ -144,9 +135,9 @@ module Discorb
144
135
  #
145
136
  # @return [Async::Task<void>] The task.
146
137
  #
147
- def delete_messages!(*messages, force: false)
138
+ def delete_messages(*messages, force: false)
148
139
  Async do
149
- messages = messages.first if messages.length == 1 && messages.first.is_a?(Array)
140
+ messages = messages.flatten
150
141
  unless force
151
142
  time = Time.now
152
143
  messages.delete_if do |message|
@@ -165,8 +156,8 @@ module Discorb
165
156
  end
166
157
  end
167
158
 
168
- alias bulk_delete! delete_messages!
169
- alias destroy_messages! delete_messages!
159
+ alias bulk_delete delete_messages
160
+ alias destroy_messages delete_messages
170
161
 
171
162
  #
172
163
  # Follow the existing announcement channel.
@@ -208,14 +199,13 @@ module Discorb
208
199
  reason: nil
209
200
  )
210
201
  auto_archive_duration ||= @default_auto_archive_duration
211
- auto_archive_duration_value = DEFAULT_AUTO_ARCHIVE_DURATION.key(auto_archive_duration)
212
202
  Async do
213
203
  _resp, data = if message.nil?
214
204
  @client.http.request(
215
205
  Route.new("/channels/#{@id}/threads", "//channels/:channel_id/threads", :post),
216
206
  {
217
207
  name: name,
218
- auto_archive_duration: auto_archive_duration_value,
208
+ auto_archive_duration: auto_archive_duration,
219
209
  type: public ? 11 : 10,
220
210
  rate_limit_per_user: rate_limit_per_user || slowmode,
221
211
  },
@@ -227,7 +217,7 @@ module Discorb
227
217
  "//channels/:channel_id/messages/:message_id/threads", :post),
228
218
  {
229
219
  name: name,
230
- auto_archive_duration: auto_archive_duration_value,
220
+ auto_archive_duration: auto_archive_duration,
231
221
  },
232
222
  audit_log_reason: reason,
233
223
  ).wait
@@ -271,33 +261,34 @@ module Discorb
271
261
  # @async
272
262
  #
273
263
  # @param [Integer] limit The limit of threads to fetch.
274
- # @param [Time] before <description>
264
+ # @param [Time] before The time before which the threads are created.
275
265
  #
276
266
  # @return [Async::Task<Array<Discorb::ThreadChannel>>] The joined archived private threads in the channel.
277
267
  #
278
268
  def fetch_joined_archived_private_threads(limit: nil, before: nil)
279
269
  Async do
280
270
  if limit.nil?
281
- before = 0
271
+ before = Time.now
282
272
  threads = []
283
273
  loop do
284
274
  _resp, data = @client.http.request(
285
275
  Route.new(
286
- "/channels/#{@id}/users/@me/threads/archived/private?before=#{before}",
276
+ "/channels/#{@id}/users/@me/threads/archived/private?before=#{before.iso8601}",
287
277
  "//channels/:channel_id/users/@me/threads/archived/private",
288
278
  :get
289
279
  )
290
280
  ).wait
291
281
  threads += data[:threads].map { |thread| Channel.make_channel(@client, thread) }
292
- before = data[:threads][-1][:id]
293
282
 
294
283
  break unless data[:has_more]
284
+
285
+ before = Snowflake.new(data[:threads][-1][:id]).timestamp
295
286
  end
296
287
  threads
297
288
  else
298
289
  _resp, data = @client.http.request(
299
290
  Route.new(
300
- "/channels/#{@id}/users/@me/threads/archived/private?limit=#{limit}&before=#{before}",
291
+ "/channels/#{@id}/users/@me/threads/archived/private?limit=#{limit}&before=#{before.iso8601}",
301
292
  "//channels/:channel_id/users/@me/threads/archived/private",
302
293
  :get
303
294
  )
@@ -315,7 +306,7 @@ module Discorb
315
306
  @last_message_id = data[:last_message_id]
316
307
  @rate_limit_per_user = data[:rate_limit_per_user]
317
308
  @last_pin_timestamp = data[:last_pin_timestamp] && Time.iso8601(data[:last_pin_timestamp])
318
- @default_auto_archive_duration = DEFAULT_AUTO_ARCHIVE_DURATION[data[:default_auto_archive_duration]]
309
+ @default_auto_archive_duration = data[:default_auto_archive_duration]
319
310
  super
320
311
  end
321
312
  end
@@ -112,7 +112,7 @@ module Discorb
112
112
  #
113
113
  # @param [String] reason The reason of archiving the thread.
114
114
  #
115
- # @return [self] The archived thread.
115
+ # @return [Async::Task<self>] The archived thread.
116
116
  #
117
117
  def archive(reason: nil)
118
118
  edit(archived: true, reason: reason)
@@ -123,7 +123,7 @@ module Discorb
123
123
  #
124
124
  # @param [String] reason The reason of locking the thread.
125
125
  #
126
- # @return [self] The locked thread.
126
+ # @return [Async::Task<self>] The locked thread.
127
127
  #
128
128
  def lock(reason: nil)
129
129
  edit(archived: true, locked: true, reason: reason)
@@ -134,7 +134,7 @@ module Discorb
134
134
  #
135
135
  # @param [String] reason The reason of unarchiving the thread.
136
136
  #
137
- # @return [self] The unarchived thread.
137
+ # @return [Async::Task<self>] The unarchived thread.
138
138
  #
139
139
  def unarchive(reason: nil)
140
140
  edit(archived: false, reason: reason)
@@ -145,7 +145,7 @@ module Discorb
145
145
  #
146
146
  # @param [String] reason The reason of unlocking the thread.
147
147
  #
148
- # @return [self] The unlocked thread.
148
+ # @return [Async::Task<self>] The unlocked thread.
149
149
  #
150
150
  # @note This method won't unarchive the thread. Use {#unarchive} instead.
151
151
  #
@@ -232,7 +232,7 @@ module Discorb
232
232
  Async do
233
233
  _resp, data = @client.http.request(Route.new("/channels/#{@id}/thread-members",
234
234
  "//channels/:channel_id/thread-members", :get)).wait
235
- data.map { |d| @members[d[:id]] = Member.new(@client, d) }
235
+ data.map { |d| @members[d[:id]] = Member.new(@client, d, @guild_id) }
236
236
  end
237
237
  end
238
238
 
@@ -267,11 +267,12 @@ module Discorb
267
267
  class Member < DiscordModel
268
268
  attr_reader :joined_at
269
269
 
270
- def initialize(cilent, data)
271
- @cilent = cilent
270
+ def initialize(client, data, guild_id)
271
+ @client = client
272
272
  @thread_id = data[:id]
273
273
  @user_id = data[:user_id]
274
274
  @joined_at = Time.iso8601(data[:join_timestamp])
275
+ @guild_id = guild_id
275
276
  end
276
277
 
277
278
  def thread
@@ -279,7 +280,7 @@ module Discorb
279
280
  end
280
281
 
281
282
  def member
282
- thread && thread.members[@user_id]
283
+ @client.guilds[@guild_id].members[@user_id]
283
284
  end
284
285
 
285
286
  def id
@@ -287,7 +288,7 @@ module Discorb
287
288
  end
288
289
 
289
290
  def user
290
- @cilent.users[@user_id]
291
+ @client.users[@user_id]
291
292
  end
292
293
 
293
294
  def inspect
@@ -312,8 +313,11 @@ module Discorb
312
313
  @message_count = data[:message_count]
313
314
  if data[:member]
314
315
  @members[@client.user.id] =
315
- ThreadChannel::Member.new(@client,
316
- data[:member].merge({ id: data[:id], user_id: @client.user.id }))
316
+ ThreadChannel::Member.new(
317
+ @client,
318
+ data[:member].merge({ id: data[:id], user_id: @client.user.id }),
319
+ @guild_id
320
+ )
317
321
  end
318
322
  @data.merge!(data)
319
323
  end
@@ -66,7 +66,7 @@ module Discorb
66
66
  # @return [Discorb::Shard] The current shard. This is implemented with Thread variables.
67
67
  # @return [nil] If client has no shard.
68
68
  # @!attribute [r] shard_id
69
- # @return [Discorb::Shard] The current shard ID. This is implemented with Thread variables.
69
+ # @return [Integer] The current shard ID. This is implemented with Thread variables.
70
70
  # @return [nil] If client has no shard.
71
71
  # @!attribute [r] logger
72
72
  # @return [Logger] The logger.
@@ -473,9 +473,9 @@ module Discorb
473
473
  #
474
474
  # Stops the client.
475
475
  #
476
- def close!
476
+ def close
477
477
  if @shards.any?
478
- @shards.each(&:close!)
478
+ @shards.each_value(&:close)
479
479
  else
480
480
  @connection.send_close
481
481
  end
@@ -512,7 +512,8 @@ module Discorb
512
512
  end
513
513
 
514
514
  def run_setup(token)
515
- guild_ids = "global"
515
+ # @type var guild_ids: Array[String] | false
516
+ guild_ids = false
516
517
  if guilds = ENV.fetch("DISCORB_SETUP_GUILDS", nil)
517
518
  guild_ids = guilds.split(",")
518
519
  end
@@ -520,9 +521,7 @@ module Discorb
520
521
  setup_commands(token, guild_ids: guild_ids).wait
521
522
  clear_commands(token, ENV.fetch("DISCORB_SETUP_CLEAR_GUILDS", "").split(","))
522
523
  if ENV.fetch("DISCORB_SETUP_SCRIPT", nil) == "true"
523
- @events[:setup]&.each do |event|
524
- event.call
525
- end
524
+ @events[:setup]&.each(&:call)
526
525
  self.on_setup if respond_to? :on_setup
527
526
  end
528
527
  end
@@ -552,8 +551,9 @@ module Discorb
552
551
  end
553
552
 
554
553
  def session_id=(value)
555
- if shard_id
556
- @shards[shard_id].session_id = value
554
+ sid = shard_id
555
+ if sid
556
+ @shards[sid].session_id = value
557
557
  else
558
558
  @session_id = value
559
559
  end
@@ -565,18 +565,18 @@ module Discorb
565
565
  Signal.trap(:SIGINT) do
566
566
  logger.info "SIGINT received, closing..."
567
567
  Signal.trap(:SIGINT, "DEFAULT")
568
- close!
568
+ close
569
569
  end
570
570
  if shards.nil?
571
571
  main_loop(nil)
572
572
  else
573
- @shards = shards.map.with_index do |shard, i|
574
- Shard.new(self, shard, shard_count, i)
573
+ @shards = shards.to_h.with_index do |shard, i|
574
+ [shard, Shard.new(self, shard, shard_count, i)]
575
575
  end
576
- @shards[..-1].each_with_index do |shard, i|
577
- shard.next_shard = @shards[i + 1]
576
+ @shards.values[..-1].each_with_index do |shard, i|
577
+ shard.next_shard = @shards.values[i + 1]
578
578
  end
579
- @shards.each { |s| s.thread.join }
579
+ @shards.each_value { |s| s.thread.join }
580
580
  end
581
581
  end
582
582