discorb 0.15.0 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +1 -0
  3. data/.github/workflows/build_main.yml +2 -2
  4. data/.github/workflows/build_version.yml +1 -1
  5. data/.github/workflows/codeql-analysis.yml +1 -1
  6. data/.github/workflows/lint-push.yml +3 -5
  7. data/.github/workflows/lint.yml +1 -1
  8. data/.github/workflows/spec.yml +30 -0
  9. data/.lefthook/commit-msg/validator.rb +5 -0
  10. data/.rspec +2 -0
  11. data/.rspec_parallel +2 -0
  12. data/.rubocop.yml +49 -8
  13. data/Changelog.md +32 -1
  14. data/Gemfile +14 -8
  15. data/Rakefile +46 -25
  16. data/bin/console +3 -3
  17. data/docs/Examples.md +1 -1
  18. data/docs/application_command.md +138 -46
  19. data/docs/cli/irb.md +2 -2
  20. data/docs/cli/new.md +14 -9
  21. data/docs/cli/run.md +7 -11
  22. data/docs/cli.md +17 -10
  23. data/docs/events.md +257 -193
  24. data/docs/extension.md +1 -2
  25. data/docs/faq.md +0 -1
  26. data/docs/tutorial.md +12 -12
  27. data/docs/voice_events.md +106 -106
  28. data/examples/commands/message.rb +63 -0
  29. data/examples/commands/permission.rb +18 -0
  30. data/examples/commands/slash.rb +44 -0
  31. data/examples/commands/user.rb +51 -0
  32. data/examples/components/authorization_button.rb +2 -2
  33. data/examples/components/select_menu.rb +2 -2
  34. data/examples/extension/main.rb +1 -1
  35. data/examples/extension/message_expander.rb +5 -2
  36. data/examples/simple/eval.rb +2 -2
  37. data/examples/simple/ping_pong.rb +1 -1
  38. data/examples/simple/rolepanel.rb +2 -2
  39. data/examples/simple/shard.rb +17 -0
  40. data/examples/simple/wait_for_message.rb +1 -1
  41. data/exe/discorb +31 -16
  42. data/lefthook.yml +45 -0
  43. data/lib/discorb/allowed_mentions.rb +8 -0
  44. data/lib/discorb/app_command/command.rb +184 -60
  45. data/lib/discorb/app_command/common.rb +25 -0
  46. data/lib/discorb/app_command/handler.rb +116 -34
  47. data/lib/discorb/app_command.rb +2 -1
  48. data/lib/discorb/application.rb +17 -7
  49. data/lib/discorb/asset.rb +10 -2
  50. data/lib/discorb/attachment.rb +17 -2
  51. data/lib/discorb/audit_logs.rb +53 -12
  52. data/lib/discorb/channel/base.rb +108 -0
  53. data/lib/discorb/channel/category.rb +32 -0
  54. data/lib/discorb/channel/container.rb +44 -0
  55. data/lib/discorb/channel/dm.rb +28 -0
  56. data/lib/discorb/channel/guild.rb +245 -0
  57. data/lib/discorb/channel/stage.rb +140 -0
  58. data/lib/discorb/channel/text.rb +345 -0
  59. data/lib/discorb/channel/thread.rb +321 -0
  60. data/lib/discorb/channel/voice.rb +79 -0
  61. data/lib/discorb/channel.rb +2 -1126
  62. data/lib/discorb/client.rb +160 -64
  63. data/lib/discorb/common.rb +18 -3
  64. data/lib/discorb/components/button.rb +7 -7
  65. data/lib/discorb/components/select_menu.rb +6 -18
  66. data/lib/discorb/components/text_input.rb +12 -2
  67. data/lib/discorb/components.rb +1 -1
  68. data/lib/discorb/dictionary.rb +2 -0
  69. data/lib/discorb/embed.rb +55 -14
  70. data/lib/discorb/emoji.rb +59 -5
  71. data/lib/discorb/emoji_table.rb +4970 -4
  72. data/lib/discorb/error.rb +7 -1
  73. data/lib/discorb/event.rb +56 -21
  74. data/lib/discorb/exe/about.rb +1 -0
  75. data/lib/discorb/exe/irb.rb +2 -4
  76. data/lib/discorb/exe/new.rb +95 -28
  77. data/lib/discorb/exe/run.rb +9 -37
  78. data/lib/discorb/exe/setup.rb +25 -12
  79. data/lib/discorb/exe/show.rb +4 -3
  80. data/lib/discorb/extend.rb +1 -0
  81. data/lib/discorb/extension.rb +6 -3
  82. data/lib/discorb/flag.rb +11 -0
  83. data/lib/discorb/gateway.rb +312 -169
  84. data/lib/discorb/gateway_requests.rb +4 -7
  85. data/lib/discorb/guild.rb +255 -89
  86. data/lib/discorb/guild_template.rb +34 -7
  87. data/lib/discorb/http.rb +23 -11
  88. data/lib/discorb/integration.rb +27 -9
  89. data/lib/discorb/intents.rb +8 -8
  90. data/lib/discorb/interaction/autocomplete.rb +31 -19
  91. data/lib/discorb/interaction/command.rb +70 -17
  92. data/lib/discorb/interaction/components.rb +20 -4
  93. data/lib/discorb/interaction/modal.rb +0 -1
  94. data/lib/discorb/interaction/response.rb +73 -22
  95. data/lib/discorb/interaction/root.rb +29 -14
  96. data/lib/discorb/interaction.rb +1 -0
  97. data/lib/discorb/invite.rb +16 -9
  98. data/lib/discorb/member.rb +46 -5
  99. data/lib/discorb/message.rb +56 -15
  100. data/lib/discorb/message_meta.rb +39 -9
  101. data/lib/discorb/modules.rb +56 -14
  102. data/lib/discorb/permission.rb +14 -5
  103. data/lib/discorb/presence.rb +43 -10
  104. data/lib/discorb/rate_limit.rb +13 -3
  105. data/lib/discorb/reaction.rb +10 -4
  106. data/lib/discorb/role.rb +31 -4
  107. data/lib/discorb/shard.rb +74 -0
  108. data/lib/discorb/sticker.rb +30 -21
  109. data/lib/discorb/user.rb +13 -1
  110. data/lib/discorb/utils/colored_puts.rb +1 -0
  111. data/lib/discorb/voice_state.rb +30 -8
  112. data/lib/discorb/webhook.rb +88 -25
  113. data/lib/discorb.rb +10 -6
  114. data/po/yard.pot +9 -9
  115. data/sig/discorb.rbs +7232 -5837
  116. metadata +23 -6
  117. data/examples/commands/bookmarker.rb +0 -42
  118. data/examples/commands/hello.rb +0 -10
  119. data/examples/commands/inspect.rb +0 -25
  120. data/lib/discorb/log.rb +0 -81
data/lib/discorb/embed.rb CHANGED
@@ -44,8 +44,18 @@ module Discorb
44
44
  # @param [Discorb::Embed::Image, String] image The image of embed.
45
45
  # @param [Discorb::Embed::Thumbnail, String] thumbnail The thumbnail of embed.
46
46
  #
47
- def initialize(title = nil, description = nil, color: nil, url: nil, timestamp: nil, author: nil,
48
- fields: nil, footer: nil, image: nil, thumbnail: nil)
47
+ def initialize(
48
+ title = nil,
49
+ description = nil,
50
+ color: nil,
51
+ url: nil,
52
+ timestamp: nil,
53
+ author: nil,
54
+ fields: nil,
55
+ footer: nil,
56
+ image: nil,
57
+ thumbnail: nil
58
+ )
49
59
  @title = title
50
60
  @description = description
51
61
  @url = url
@@ -59,7 +69,12 @@ module Discorb
59
69
  @type = "rich"
60
70
  end
61
71
 
72
+ #
73
+ # Initialize embed from hash.
62
74
  # @private
75
+ #
76
+ # @param [Hash] data The hash data to initialize embed.
77
+ #
63
78
  def initialize_hash(data)
64
79
  @title = data[:title]
65
80
  @description = data[:description]
@@ -118,10 +133,21 @@ module Discorb
118
133
  inst
119
134
  end
120
135
 
136
+ #
137
+ # Represents an entry in embed.
138
+ # @abstract
139
+ # @private
140
+ #
141
+ class Entry
142
+ def inspect
143
+ "#<#{self.class}>"
144
+ end
145
+ end
146
+
121
147
  #
122
148
  # Represents an author of embed.
123
149
  #
124
- class Author
150
+ class Author < Entry
125
151
  # @return [String] The name of author.
126
152
  attr_accessor :name
127
153
  # @return [String, nil] The url of author.
@@ -145,7 +171,8 @@ module Discorb
145
171
  #
146
172
  # Convert author to hash.
147
173
  #
148
- # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-author-structure Offical Discord API Docs
174
+ # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-author-structure
175
+ # Offical Discord API Docs
149
176
  # @return [Hash] Converted author.
150
177
  #
151
178
  def to_hash
@@ -160,7 +187,7 @@ module Discorb
160
187
  #
161
188
  # Represemts a footer of embed.
162
189
  #
163
- class Footer
190
+ class Footer < Entry
164
191
  attr_accessor :text, :icon
165
192
 
166
193
  #
@@ -177,7 +204,8 @@ module Discorb
177
204
  #
178
205
  # Convert footer to hash.
179
206
  #
180
- # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-footer-structure Offical Discord API Docs
207
+ # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-footer-structure
208
+ # Offical Discord API Docs
181
209
  # @return [Hash] Converted footer.
182
210
  #
183
211
  def to_hash
@@ -191,7 +219,7 @@ module Discorb
191
219
  #
192
220
  # Represents a field of embed.
193
221
  #
194
- class Field
222
+ class Field < Entry
195
223
  # @return [String] The name of field.
196
224
  attr_accessor :name
197
225
  # @return [String] The value of field.
@@ -215,7 +243,8 @@ module Discorb
215
243
  #
216
244
  # Convert field to hash.
217
245
  #
218
- # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-field-structure Offical Discord API Docs
246
+ # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-field-structure
247
+ # Offical Discord API Docs
219
248
  # @return [Hash] Converted field.
220
249
  #
221
250
  def to_hash
@@ -230,7 +259,7 @@ module Discorb
230
259
  #
231
260
  # Represents an image of embed.
232
261
  #
233
- class Image
262
+ class Image < Entry
234
263
  # @return [String] The url of image.
235
264
  attr_accessor :url
236
265
  # @return [String] The proxy url of image.
@@ -263,7 +292,8 @@ module Discorb
263
292
  #
264
293
  # Convert image to hash for sending.
265
294
  #
266
- # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-image-structure Offical Discord API Docs
295
+ # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-image-structure
296
+ # Offical Discord API Docs
267
297
  # @return [Hash] Converted image.
268
298
  #
269
299
  def to_hash
@@ -274,7 +304,7 @@ module Discorb
274
304
  #
275
305
  # Represents a thumbnail of embed.
276
306
  #
277
- class Thumbnail
307
+ class Thumbnail < Entry
278
308
  # @return [String] The url of thumbnail.
279
309
  attr_accessor :url
280
310
  # @return [String] The proxy url of thumbnail.
@@ -307,7 +337,8 @@ module Discorb
307
337
  #
308
338
  # Convert thumbnail to hash for sending.
309
339
  #
310
- # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-thumbnail-structure Offical Discord API Docs
340
+ # @see https://discord.com/developers/docs/resources/channel#embed-object-embed-thumbnail-structure
341
+ # Offical Discord API Docs
311
342
  # @return [Hash] Converted thumbnail.
312
343
  #
313
344
  def to_hash
@@ -318,7 +349,7 @@ module Discorb
318
349
  #
319
350
  # Represents a video of embed.
320
351
  #
321
- class Video
352
+ class Video < Entry
322
353
  # @return [String] The url of video.
323
354
  attr_reader :url
324
355
  # @return [String] The proxy url of video.
@@ -328,7 +359,12 @@ module Discorb
328
359
  # @return [Integer] The width of video.
329
360
  attr_reader :width
330
361
 
362
+ #
363
+ # Initialize a new Video object.
331
364
  # @private
365
+ #
366
+ # @param [Hash] data The data of video.
367
+ #
332
368
  def initialize(data)
333
369
  @url = data[:url]
334
370
  @proxy_url = data[:proxy_url]
@@ -340,13 +376,18 @@ module Discorb
340
376
  #
341
377
  # Represents a provider of embed.
342
378
  #
343
- class Provider
379
+ class Provider < Entry
344
380
  # @return [String] The name of provider.
345
381
  attr_reader :name
346
382
  # @return [String] The url of provider.
347
383
  attr_reader :url
348
384
 
385
+ #
386
+ # Initialize a new Provider object.
349
387
  # @private
388
+ #
389
+ # @param [Hash] data The data of provider.
390
+ #
350
391
  def initialize(data)
351
392
  @name = data[:name]
352
393
  @url = data[:url]
data/lib/discorb/emoji.rb CHANGED
@@ -44,7 +44,14 @@ module Discorb
44
44
  # @!attribute [r] roles?
45
45
  # @return [Boolean] whether or not this emoji is restricted to certain roles.
46
46
 
47
+ #
48
+ # Initialize a new custom emoji.
47
49
  # @private
50
+ #
51
+ # @param [Discorb::Client] client The client that owns this emoji.
52
+ # @param [Discorb::Guild] guild The guild that owns this emoji.
53
+ # @param [Hash] data The data of the emoji.
54
+ #
48
55
  def initialize(client, guild, data)
49
56
  @client = client
50
57
  @guild = guild
@@ -96,7 +103,10 @@ module Discorb
96
103
  payload = {}
97
104
  payload[:name] = name if name != Discorb::Unset
98
105
  payload[:roles] = roles.map { |r| Discorb::Utils.try(r, :id) } if roles != Discorb::Unset
99
- @client.http.request(Route.new("/guilds/#{@guild.id}/emojis/#{@id}", "//guilds/:guild_id/emojis/:emoji_id", :patch), payload, audit_log_reason: reason)
106
+ @client.http.request(
107
+ Route.new("/guilds/#{@guild.id}/emojis/#{@id}", "//guilds/:guild_id/emojis/:emoji_id",
108
+ :patch), payload, audit_log_reason: reason,
109
+ )
100
110
  self
101
111
  end
102
112
  end
@@ -113,7 +123,10 @@ module Discorb
113
123
  #
114
124
  def delete!(reason: nil)
115
125
  Async do
116
- @client.http.request(Route.new("/guilds/#{@guild.id}/emojis/#{@id}", "//guilds/:guild_id/emojis/:emoji_id", :delete), audit_log_reason: reason).wait
126
+ @client.http.request(
127
+ Route.new("/guilds/#{@guild.id}/emojis/#{@id}", "//guilds/:guild_id/emojis/:emoji_id",
128
+ :delete), {}, audit_log_reason: reason,
129
+ ).wait
117
130
  @available = false
118
131
  self
119
132
  end
@@ -121,7 +134,12 @@ module Discorb
121
134
 
122
135
  alias destroy! delete!
123
136
 
137
+ #
138
+ # Converts the object to a hash.
124
139
  # @private
140
+ #
141
+ # @return [Hash] The hash represents the object.
142
+ #
125
143
  def to_hash
126
144
  {
127
145
  name: @name,
@@ -135,7 +153,7 @@ module Discorb
135
153
  def _set_data(data)
136
154
  @id = Snowflake.new(data[:id])
137
155
  @name = data[:name]
138
- @roles = data[:role] ? data[:role].map { |r| Role.new(@client, r) } : []
156
+ @roles = data[:role] ? data[:role].map { |r| Role.new(@client, guild, r) } : []
139
157
  @user = User.new(@client, data[:user]) if data[:user]
140
158
  @require_colons = data[:require_colons]
141
159
  @managed = data[:managed]
@@ -158,7 +176,12 @@ module Discorb
158
176
  attr_reader :deleted
159
177
  alias deleted? deleted
160
178
 
179
+ #
180
+ # Initialize a new partial custom emoji.
161
181
  # @private
182
+ #
183
+ # @param [Hash] data The data of the emoji.
184
+ #
162
185
  def initialize(data)
163
186
  @id = Snowflake.new(data[:id])
164
187
  @name = data[:name]
@@ -200,7 +223,12 @@ module Discorb
200
223
  # @return [Integer] The skin tone of the emoji.
201
224
  attr_reader :skin_tone
202
225
 
203
- # @private
226
+ #
227
+ # Initialize a new unicode emoji.
228
+ #
229
+ # @param [String] name The name of the emoji.
230
+ # @param [Integer] tone The skin tone of the emoji.
231
+ #
204
232
  def initialize(name, tone: 0)
205
233
  if EmojiTable::DISCORD_TO_UNICODE.key?(name)
206
234
  @name = name
@@ -208,10 +236,31 @@ module Discorb
208
236
  elsif EmojiTable::UNICODE_TO_DISCORD.key?(name)
209
237
  @name = EmojiTable::UNICODE_TO_DISCORD[name][0]
210
238
  @value = name
239
+ elsif EmojiTable::SKIN_TONES.any? { |t| name.include?(t) }
240
+ name2 = name.dup
241
+ EmojiTable::SKIN_TONES.each.with_index do |t, i|
242
+ next unless name2.include?(t)
243
+
244
+ @skin_tone = i
245
+ name2.sub!(t, "")
246
+ break
247
+ end
248
+ raise ArgumentError, "Invalid skin tone: #{tone}" unless @skin_tone
249
+
250
+ @name = EmojiTable::UNICODE_TO_DISCORD[name2]
251
+ @value = name
211
252
  else
212
253
  raise ArgumentError, "No such emoji: #{name}"
213
254
  end
214
- @value += EmojiTable::SKIN_TONES[tone] if tone.positive?
255
+ if tone.positive?
256
+ unless @value = EmojiTable::DISCORD_TO_UNICODE["#{name}_tone#{tone}"]
257
+ raise ArgumentError,
258
+ "Invalid skin tone for emoji: #{name}"
259
+ end
260
+
261
+ @name = "#{name}_tone#{tone}"
262
+ @skin_tone = tone
263
+ end
215
264
  end
216
265
 
217
266
  # @return [String] The unicode string of the emoji.
@@ -232,7 +281,12 @@ module Discorb
232
281
  "#<#{self.class} :#{@name}:>"
233
282
  end
234
283
 
284
+ #
285
+ # Converts the object to a hash.
235
286
  # @private
287
+ #
288
+ # @return [Hash] The hash represents the object.
289
+ #
236
290
  def to_hash
237
291
  {
238
292
  name: @value,