discorb 0.17.1 → 0.18.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 (141) 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 +10 -0
  5. data/Gemfile +6 -0
  6. data/Rakefile +210 -98
  7. data/Steepfile +28 -0
  8. data/docs/events.md +42 -6
  9. data/docs/tutorial.md +7 -7
  10. data/docs/voice_events.md +2 -2
  11. data/examples/commands/message.rb +12 -7
  12. data/examples/commands/permission.rb +2 -1
  13. data/examples/commands/slash.rb +23 -19
  14. data/examples/commands/user.rb +15 -12
  15. data/examples/components/authorization_button.rb +2 -1
  16. data/examples/components/select_menu.rb +4 -1
  17. data/examples/extension/main.rb +1 -0
  18. data/examples/extension/message_expander.rb +1 -0
  19. data/examples/sig/commands/message.rbs +5 -0
  20. data/examples/simple/eval.rb +1 -0
  21. data/examples/simple/ping_pong.rb +1 -0
  22. data/examples/simple/rolepanel.rb +16 -5
  23. data/examples/simple/shard.rb +2 -1
  24. data/examples/simple/wait_for_message.rb +3 -0
  25. data/exe/discorb +3 -3
  26. data/lib/discorb/allowed_mentions.rb +1 -1
  27. data/lib/discorb/app_command/command.rb +12 -13
  28. data/lib/discorb/app_command/handler.rb +20 -6
  29. data/lib/discorb/audit_logs.rb +6 -2
  30. data/lib/discorb/automod.rb +269 -0
  31. data/lib/discorb/channel/guild.rb +2 -1
  32. data/lib/discorb/channel/stage.rb +1 -1
  33. data/lib/discorb/channel/text.rb +11 -20
  34. data/lib/discorb/channel/thread.rb +15 -11
  35. data/lib/discorb/client.rb +12 -10
  36. data/lib/discorb/color.rb +37 -60
  37. data/lib/discorb/common.rb +1 -1
  38. data/lib/discorb/dictionary.rb +1 -1
  39. data/lib/discorb/embed.rb +4 -3
  40. data/lib/discorb/emoji.rb +2 -2
  41. data/lib/discorb/exe/about.rb +1 -1
  42. data/lib/discorb/exe/new.rb +1 -5
  43. data/lib/discorb/extension.rb +0 -4
  44. data/lib/discorb/flag.rb +2 -2
  45. data/lib/discorb/gateway.rb +21 -582
  46. data/lib/discorb/gateway_events.rb +638 -0
  47. data/lib/discorb/guild.rb +136 -17
  48. data/lib/discorb/guild_template.rb +1 -1
  49. data/lib/discorb/http.rb +47 -25
  50. data/lib/discorb/intents.rb +27 -18
  51. data/lib/discorb/interaction/command.rb +14 -10
  52. data/lib/discorb/interaction/response.rb +74 -14
  53. data/lib/discorb/member.rb +3 -3
  54. data/lib/discorb/message.rb +13 -11
  55. data/lib/discorb/message_meta.rb +2 -3
  56. data/lib/discorb/modules.rb +3 -2
  57. data/lib/discorb/presence.rb +4 -2
  58. data/lib/discorb/reaction.rb +2 -2
  59. data/lib/discorb/role.rb +1 -1
  60. data/lib/discorb/sticker.rb +3 -3
  61. data/lib/discorb/user.rb +2 -2
  62. data/lib/discorb/voice_state.rb +5 -5
  63. data/lib/discorb/webhook.rb +15 -6
  64. data/lib/discorb.rb +2 -2
  65. data/rbs_collection.lock.yaml +88 -96
  66. data/rbs_collection.yaml +21 -17
  67. data/sig/async.rbs +11 -5
  68. data/sig/discorb/activity.rbs +23 -0
  69. data/sig/discorb/allowed_mentions.rbs +44 -0
  70. data/sig/discorb/app_command/base.rbs +282 -0
  71. data/sig/discorb/app_command/handler.rbs +171 -0
  72. data/sig/discorb/application.rbs +142 -0
  73. data/sig/discorb/asset.rbs +32 -0
  74. data/sig/discorb/attachment.rbs +91 -0
  75. data/sig/discorb/audit_log.rbs +231 -0
  76. data/sig/discorb/automod.rbs +128 -0
  77. data/sig/discorb/avatar.rbs +26 -0
  78. data/sig/discorb/channel/base.rbs +179 -0
  79. data/sig/discorb/channel/category.rbs +56 -0
  80. data/sig/discorb/channel/container.rbs +29 -0
  81. data/sig/discorb/channel/dm.rbs +14 -0
  82. data/sig/discorb/channel/news.rbs +20 -0
  83. data/sig/discorb/channel/stage.rbs +77 -0
  84. data/sig/discorb/channel/text.rbs +158 -0
  85. data/sig/discorb/channel/thread.rbs +185 -0
  86. data/sig/discorb/channel/voice.rbs +41 -0
  87. data/sig/discorb/client.rbs +2495 -0
  88. data/sig/discorb/color.rbs +142 -0
  89. data/sig/discorb/component/base.rbs +28 -0
  90. data/sig/discorb/component/button.rbs +65 -0
  91. data/sig/discorb/component/select_menu.rbs +107 -0
  92. data/sig/discorb/component/text_input.rbs +69 -0
  93. data/sig/discorb/connectable.rbs +8 -0
  94. data/sig/discorb/custom_emoji.rbs +90 -0
  95. data/sig/discorb/dictionary.rbs +85 -0
  96. data/sig/discorb/discord_model.rbs +15 -0
  97. data/sig/discorb/embed.rbs +279 -0
  98. data/sig/discorb/emoji.rbs +13 -0
  99. data/sig/discorb/error.rbs +73 -0
  100. data/sig/discorb/event_handler.rbs +27 -0
  101. data/sig/discorb/extension.rbs +1734 -0
  102. data/sig/discorb/flag.rbs +72 -0
  103. data/sig/discorb/gateway.rbs +481 -0
  104. data/sig/discorb/guild.rbs +870 -0
  105. data/sig/discorb/guild_template.rbs +174 -0
  106. data/sig/discorb/http.rbs +147 -0
  107. data/sig/discorb/image.rbs +20 -0
  108. data/sig/discorb/integration.rbs +118 -0
  109. data/sig/discorb/intents.rbs +97 -0
  110. data/sig/discorb/interaction/autocomplete.rbs +9 -0
  111. data/sig/discorb/interaction/base.rbs +66 -0
  112. data/sig/discorb/interaction/command.rbs +66 -0
  113. data/sig/discorb/interaction/message_component.rbs +140 -0
  114. data/sig/discorb/interaction/modal.rbs +50 -0
  115. data/sig/discorb/interaction/responder.rbs +157 -0
  116. data/sig/discorb/invite.rbs +86 -0
  117. data/sig/discorb/member.rbs +187 -0
  118. data/sig/discorb/message.rbs +469 -0
  119. data/sig/discorb/messageable.rbs +153 -0
  120. data/sig/discorb/partial_emoji.rbs +35 -0
  121. data/sig/discorb/permissions.rbs +149 -0
  122. data/sig/discorb/presence.rbs +237 -0
  123. data/sig/discorb/reaction.rbs +33 -0
  124. data/sig/discorb/role.rbs +145 -0
  125. data/sig/discorb/scheduled_event.rbs +148 -0
  126. data/sig/discorb/shard.rbs +62 -0
  127. data/sig/discorb/snowflake.rbs +56 -0
  128. data/sig/discorb/stage_instance.rbs +63 -0
  129. data/sig/discorb/sticker.rbs +116 -0
  130. data/sig/discorb/system_channel_flag.rbs +17 -0
  131. data/sig/discorb/unicode_emoji.rbs +49 -0
  132. data/sig/discorb/user.rbs +93 -0
  133. data/sig/discorb/utils.rbs +8 -0
  134. data/sig/discorb/voice_region.rbs +30 -0
  135. data/sig/discorb/voice_state.rbs +71 -0
  136. data/sig/discorb/webhook.rbs +327 -0
  137. data/sig/discorb/welcome_screen.rbs +78 -0
  138. data/sig/discorb.rbs +5 -8661
  139. data/sig/manifest.yaml +3 -0
  140. data/sig/override.rbs +19 -0
  141. 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
@@ -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
 
@@ -146,7 +137,7 @@ module Discorb
146
137
  #
147
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|
@@ -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.
@@ -475,7 +475,7 @@ module Discorb
475
475
  #
476
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
@@ -552,8 +553,9 @@ module Discorb
552
553
  end
553
554
 
554
555
  def session_id=(value)
555
- if shard_id
556
- @shards[shard_id].session_id = value
556
+ sid = shard_id
557
+ if sid
558
+ @shards[sid].session_id = value
557
559
  else
558
560
  @session_id = value
559
561
  end
@@ -570,13 +572,13 @@ module Discorb
570
572
  if shards.nil?
571
573
  main_loop(nil)
572
574
  else
573
- @shards = shards.map.with_index do |shard, i|
574
- Shard.new(self, shard, shard_count, i)
575
+ @shards = shards.to_h.with_index do |shard, i|
576
+ [shard, Shard.new(self, shard, shard_count, i)]
575
577
  end
576
- @shards[..-1].each_with_index do |shard, i|
577
- shard.next_shard = @shards[i + 1]
578
+ @shards.values[..-1].each_with_index do |shard, i|
579
+ shard.next_shard = @shards.values[i + 1]
578
580
  end
579
- @shards.each { |s| s.thread.join }
581
+ @shards.each_value { |s| s.thread.join }
580
582
  end
581
583
  end
582
584
 
data/lib/discorb/color.rb CHANGED
@@ -85,7 +85,11 @@ module Discorb
85
85
  # @return [Hash{:r, :g, :b => Integer}] A RGB hash.
86
86
  #
87
87
  def to_rgb_hash
88
- [@value / (256 * 256), @value / 256 % 256, @value % 256]
88
+ {
89
+ r: @value / (256 * 256),
90
+ g: @value / 256 % 256,
91
+ b: @value % 256,
92
+ }
89
93
  end
90
94
 
91
95
  alias deconstruct_keys to_rgb_hash
@@ -131,65 +135,38 @@ module Discorb
131
135
  # Create a color from a Discord's color.
132
136
  # Currently these colors are supported:
133
137
  #
134
- # | Color Name | Hexadecimal |
135
- # |------------|------------|
136
- # | `:teal` | `#1abc9c` |
137
- # | `:dark_teal` | `#11806a` |
138
- # | `:green` | `#2ecc71` |
139
- # | `:dark_green` | `#1f8b4c` |
140
- # | `:blue` | `#3498db` |
141
- # | `:dark_blue` | `#206694` |
142
- # | `:purple` | `#9b59b6` |
143
- # | `:dark_purple` | `#71368a` |
144
- # | `:magenta` | `#e91e63` |
145
- # | `:dark_magenta` | `#ad1457` |
146
- # | `:gold` | `#f1c40f` |
147
- # | `:dark_gold` | `#c27c0e` |
148
- # | `:orange` | `#e67e22` |
149
- # | `:dark_orange` | `#a84300` |
150
- # | `:red` | `#e74c3c` |
151
- # | `:dark_red` | `#992d22` |
152
- # | `:lighter_grey` | `#95a5a6` |
153
- # | `:lighter_gray` | `#95a5a6` |
154
- # | `:dark_grey` | `#607d8b` |
155
- # | `:dark_gray` | `#607d8b` |
156
- # | `:light_grey` | `#979c9f` |
157
- # | `:light_gray` | `#979c9f` |
158
- # | `:darker_grey` | `#546e7a` |
159
- # | `:darker_gray` | `#546e7a` |
160
- # | `:og_blurple` | `#7289da` |
161
- # | `:blurple` | `#5865f2` |
162
- # | `:greyple` | `#99aab5` |
163
- # | `:dark_theme` | `#36393f` |
164
- # | `:fuchsia` | `#eb459e` |
165
- # | `:dark_teal` | `#11806a` |
166
- # | `:green` | `#2ecc71` |
167
- # | `:dark_green` | `#1f8b4c` |
168
- # | `:blue` | `#3498db` |
169
- # | `:dark_blue` | `#206694` |
170
- # | `:purple` | `#9b59b6` |
171
- # | `:dark_purple` | `#71368a` |
172
- # | `:magenta` | `#e91e63` |
173
- # | `:dark_magenta` | `#ad1457` |
174
- # | `:gold` | `#f1c40f` |
175
- # | `:dark_gold` | `#c27c0e` |
176
- # | `:orange` | `#e67e22` |
177
- # | `:dark_orange` | `#a84300` |
178
- # | `:red` | `#e74c3c` |
179
- # | `:dark_red` | `#992d22` |
180
- # | `:lighter_grey` | `#95a5a6` |
181
- # | `:lighter_gray` | `#95a5a6` |
182
- # | `:dark_grey` | `#607d8b` |
183
- # | `:dark_gray` | `#607d8b` |
184
- # | `:light_grey` | `#979c9f` |
185
- # | `:light_gray` | `#979c9f` |
186
- # | `:darker_grey` | `#546e7a` |
187
- # | `:darker_gray` | `#546e7a` |
188
- # | `:og_blurple` | `#7289da` |
189
- # | `:blurple` | `#5865f2` |
190
- # | `:greyple` | `#99aab5` |
191
- # | `:dark_theme` | `#36393f` |
192
- # | `:fuchsia` | `#eb459e` |
138
+ # | Color name | Color code |
139
+ # |-----------------|------------|
140
+ # | `:teal` | `#1abc9c` |
141
+ # | `:dark_teal` | `#11806a` |
142
+ # | `:green` | `#2ecc71` |
143
+ # | `:dark_green` | `#1f8b4c` |
144
+ # | `:blue` | `#3498db` |
145
+ # | `:dark_blue` | `#206694` |
146
+ # | `:purple` | `#9b59b6` |
147
+ # | `:dark_purple` | `#71368a` |
148
+ # | `:magenta` | `#e91e63` |
149
+ # | `:dark_magenta` | `#ad1457` |
150
+ # | `:gold` | `#f1c40f` |
151
+ # | `:dark_gold` | `#c27c0e` |
152
+ # | `:orange` | `#e67e22` |
153
+ # | `:dark_orange` | `#a84300` |
154
+ # | `:red` | `#e74c3c` |
155
+ # | `:dark_red` | `#992d22` |
156
+ # | `:lighter_grey` | `#95a5a6` |
157
+ # | `:lighter_gray` | `#95a5a6` |
158
+ # | `:dark_grey` | `#607d8b` |
159
+ # | `:dark_gray` | `#607d8b` |
160
+ # | `:light_grey` | `#979c9f` |
161
+ # | `:light_gray` | `#979c9f` |
162
+ # | `:darker_grey` | `#546e7a` |
163
+ # | `:darker_gray` | `#546e7a` |
164
+ # | `:og_blurple` | `#7289da` |
165
+ # | `:blurple` | `#5865f2` |
166
+ # | `:greyple` | `#99aab5` |
167
+ # | `:dark_theme` | `#36393f` |
168
+ # | `:fuchsia` | `#eb459e` |
169
+ # | `:yellow` | `#fee75c` |
193
170
  #
194
171
  # @param [Symbol] color A Discord color name.
195
172
  #
@@ -4,7 +4,7 @@ module Discorb
4
4
  # @return [String] The API base URL.
5
5
  API_BASE_URL = "https://discord.com/api/v10"
6
6
  # @return [String] The version of discorb.
7
- VERSION = "0.17.1"
7
+ VERSION = "0.18.0"
8
8
  # @return [Array<Integer>] The version array of discorb.
9
9
  VERSION_ARRAY = VERSION.split(".").map(&:to_i).freeze
10
10
  # @return [String] The user agent for the bot.
@@ -34,7 +34,7 @@ module Discorb
34
34
  @cache[id.to_s] = body
35
35
  @cache = @cache.sort_by(&@sort).to_h if @sort
36
36
  @cache.delete(@cache.keys[0]) if !@limit.nil? && @cache.size > @limit
37
- body
37
+ self
38
38
  end
39
39
 
40
40
  #