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,398 +1,466 @@
1
- # frozen_string_literal: true
2
-
3
- module Discorb
4
- module ApplicationCommand
5
- #
6
- # Represents a application command.
7
- # @abstract
8
- #
9
- class Command < DiscordModel
10
- # @return [Hash{String => String}] The name of the command.
11
- attr_reader :name
12
- # @return [Array<#to_s>] The guild ids that the command is enabled in.
13
- attr_reader :guild_ids
14
- # @return [Proc] The block of the command.
15
- attr_reader :block
16
- # @return [:chat_input, :user, :message] The type of the command.
17
- attr_reader :type
18
- # @return [Integer] The raw type of the command.
19
- attr_reader :type_raw
20
- # @return [Discorb::Permission] The default permissions for this command.
21
- attr_reader :default_permission
22
- # @return [Boolean] Whether the command is enabled in DMs.
23
- attr_reader :dm_permission
24
-
25
- # @private
26
- # @return [{Integer => Symbol}] The mapping of raw types to types.
27
- TYPES = {
28
- 1 => :chat_input,
29
- 2 => :user,
30
- 3 => :message,
31
- }.freeze
32
-
33
- #
34
- # Initialize a new command.
35
- # @private
36
- #
37
- # @param [String, Hash{Symbol => String}] name The name of the command.
38
- # @param [Array<#to_s>, false, nil] guild_ids The guild ids that the command is enabled in.
39
- # @param [Proc] block The block of the command.
40
- # @param [Integer] type The type of the command.
41
- # @param [Boolean] dm_permission Whether the command is enabled in DMs.
42
- # @param [Discorb::Permission] default_permission The default permission of the command.
43
- #
44
- def initialize(name, guild_ids, block, type, dm_permission = true, default_permission = nil) # rubocop:disable Style/OptionalBooleanParameter
45
- @name = name.is_a?(String) ? { "default" => name } : ApplicationCommand.modify_localization_hash(name)
46
- @guild_ids = guild_ids&.map(&:to_s)
47
- @block = block
48
- @type = Discorb::ApplicationCommand::Command::TYPES[type]
49
- @type_raw = type
50
- @dm_permission = dm_permission
51
- @default_permission = default_permission
52
- end
53
-
54
- #
55
- # Changes the self pointer of block to the given object.
56
- # @private
57
- #
58
- # @param [Object] instance The object to change the self pointer to.
59
- #
60
- def replace_block(instance)
61
- current_block = @block.dup
62
- @block = proc do |*args|
63
- instance.instance_exec(*args, &current_block)
64
- end
65
- end
66
-
67
- #
68
- # Converts the object to a hash.
69
- # @private
70
- #
71
- # @return [Hash] The hash represents the object.
72
- #
73
- def to_hash
74
- {
75
- name: @name["default"],
76
- name_localizations: @name.except("default"),
77
- type: @type_raw,
78
- dm_permission: @dm_permission,
79
- default_member_permissions: @default_permission&.value&.to_s,
80
- }
81
- end
82
-
83
- #
84
- # Represents the slash command.
85
- #
86
- class ChatInputCommand < Command
87
- # @return [Hash{String => String}] The description of the command.
88
- attr_reader :description
89
- # @return [Hash{String => Hash}] The options of the command.
90
- attr_reader :options
91
-
92
- #
93
- # Initialize a new slash command.
94
- # @private
95
- #
96
- # @param [String, Hash{Symbol => String}] name The name of the command.
97
- # The hash should have `default`, and language keys.
98
- # @param [String, Hash{Symbol => String}] description The description of the command.
99
- # The hash should have `default`, and language keys.
100
- # @param [Hash{String => Hash}] options The options of the command.
101
- # @param [Array<#to_s>] guild_ids The guild ids that the command is enabled in.
102
- # @param [Proc] block The block of the command.
103
- # @param [Integer] type The type of the command.
104
- # @param [Discorb::ApplicationCommand::Command, nil] parent The parent command.
105
- # @param [Boolean] dm_permission Whether the command is enabled in DMs.
106
- # @param [Discorb::Permission] default_permission The default permission of the command.
107
- #
108
- def initialize(name, description, options, guild_ids, block, type, parent, dm_permission, default_permission)
109
- super(name, guild_ids, block, type, dm_permission, default_permission)
110
- @description = if description.is_a?(String)
111
- {
112
- "default" => description,
113
- }
114
- else
115
- ApplicationCommand.modify_localization_hash(description)
116
- end
117
- @options = options
118
- @parent = parent
119
- end
120
-
121
- #
122
- # Returns the commands name.
123
- #
124
- # @return [String] The name of the command.
125
- #
126
- def to_s
127
- "#{@parent} #{@name["default"]}".strip
128
- end
129
-
130
- #
131
- # Converts the object to a hash.
132
- # @private
133
- #
134
- # @return [Hash] The hash represents the object.
135
- #
136
- def to_hash
137
- options_payload = options.map do |name, value|
138
- ret = {
139
- type: case value[:type]
140
- when String, :string, :str
141
- 3
142
- when Integer, :integer, :int
143
- 4
144
- when TrueClass, FalseClass, :boolean, :bool
145
- 5
146
- when Discorb::User, Discorb::Member, :user, :member
147
- 6
148
- when Discorb::Channel, :channel
149
- 7
150
- when Discorb::Role, :role
151
- 8
152
- when :mentionable
153
- 9
154
- when Float, :float
155
- 10
156
- when :attachment
157
- 11
158
- else
159
- raise ArgumentError, "Invalid option type: #{value[:type]}"
160
- end,
161
- name: name,
162
- name_localizations: ApplicationCommand.modify_localization_hash(value[:name_localizations]),
163
- required: value[:required].nil? ? !value[:optional] : value[:required],
164
- }
165
-
166
- if value[:description].is_a?(String)
167
- ret[:description] = value[:description]
168
- else
169
- description = ApplicationCommand.modify_localization_hash(value[:description])
170
- ret[:description] = description["default"]
171
- ret[:description_localizations] = description.except("default")
172
- end
173
- if value[:choices]
174
- ret[:choices] = value[:choices].map do |k, v|
175
- r = {
176
- name: k, value: v,
177
- }
178
- if choices_localizations = value[:choices_localizations].clone
179
- name_localizations = ApplicationCommand.modify_localization_hash(choices_localizations.delete(k) do
180
- warn "Missing localization for #{k}"
181
- {}
182
- end)
183
- r[:name_localizations] = name_localizations.except("default")
184
- r[:name] = name_localizations["default"]
185
- r.delete(:name_localizations) if r[:name_localizations].nil?
186
- end
187
- r
188
- end
189
- end
190
-
191
- ret[:channel_types] = value[:channel_types].map(&:channel_type) if value[:channel_types]
192
-
193
- ret[:autocomplete] = !value[:autocomplete].nil? if value[:autocomplete]
194
- if value[:range]
195
- ret[:min_value] = value[:range].begin
196
- ret[:max_value] = value[:range].end
197
- end
198
- if value[:length]
199
- ret[:min_length] = value[:length].begin
200
- ret[:max_length] = value[:length].end
201
- end
202
- ret
203
- end
204
- {
205
- name: @name["default"],
206
- name_localizations: @name.except("default"),
207
- description: @description["default"],
208
- description_localizations: @description.except("default"),
209
- options: options_payload,
210
- dm_permission: @dm_permission,
211
- default_member_permissions: @default_permission&.value&.to_s,
212
- }
213
- end
214
- end
215
-
216
- #
217
- # Represents the command with subcommands.
218
- #
219
- class GroupCommand < Command
220
- # @return [Array<Discorb::ApplicationCommand::Command>] The subcommands of the command.
221
- attr_reader :commands
222
- # @return [Hash{String => String}] The description of the command.
223
- attr_reader :description
224
-
225
- #
226
- # Initialize a new group command.
227
- # @private
228
- #
229
- # @param [String, Hash{Symbol => String}] name The name of the command.
230
- # @param [String, Hash{Symbol => String}] description The description of the command.
231
- # @param [Array<#to_s>] guild_ids The guild ids that the command is enabled in.
232
- # @param [Discorb::Client] client The client of the command.
233
- # @param [Boolean] dm_permission Whether the command is enabled in DMs.
234
- # @param [Discorb::Permission] default_permission The default permission of the command.
235
- #
236
- def initialize(name, description, guild_ids, client, dm_permission, default_permission)
237
- super(name, guild_ids, block, 1, dm_permission, default_permission)
238
- @description = if description.is_a?(String)
239
- {
240
- "default" => description,
241
- }
242
- else
243
- ApplicationCommand.modify_localization_hash(description)
244
- end
245
- @commands = []
246
- @client = client
247
- end
248
-
249
- #
250
- # Add new subcommand.
251
- #
252
- # @param (see Discorb::ApplicationCommand::Handler#slash)
253
- # @return [Discorb::ApplicationCommand::Command::ChatInputCommand] The added subcommand.
254
- #
255
- def slash(command_name, description, options = {}, dm_permission: true, default_permission: nil, &block)
256
- command = Discorb::ApplicationCommand::Command::ChatInputCommand.new(
257
- command_name,
258
- description,
259
- options,
260
- [],
261
- block,
262
- 1,
263
- self,
264
- dm_permission,
265
- default_permission
266
- )
267
- @client.callable_commands << command
268
- @commands << command
269
- command
270
- end
271
-
272
- #
273
- # Add new subcommand group.
274
- #
275
- # @param [String] command_name Group name.
276
- # @param [String] description Group description.
277
- #
278
- # @yield Block to yield with the command.
279
- # @yieldparam [Discorb::ApplicationCommand::Command::SubcommandGroup] group Group command.
280
- #
281
- # @return [Discorb::ApplicationCommand::Command::SubcommandGroup] Command object.
282
- #
283
- # @see file:docs/application_command.md Application Commands
284
- #
285
- def group(command_name, description)
286
- command = Discorb::ApplicationCommand::Command::SubcommandGroup.new(command_name, description, self, @client)
287
- yield command if block_given?
288
- @commands << command
289
- command
290
- end
291
-
292
- #
293
- # Returns the command name.
294
- #
295
- # @return [String] The command name.
296
- #
297
- def to_s
298
- @name["default"]
299
- end
300
-
301
- #
302
- # Changes the self pointer to the given object.
303
- # @private
304
- #
305
- # @param [Object] instance The object to change to.
306
- #
307
- def block_replace(instance)
308
- super
309
- @commands.each { |c| c.replace_block(instance) }
310
- end
311
-
312
- #
313
- # Converts the object to a hash.
314
- # @private
315
- #
316
- # @return [Hash] The hash represents the object.
317
- #
318
- def to_hash
319
- options_payload = @commands.map do |command|
320
- if command.is_a?(ChatInputCommand)
321
- {
322
- name: command.name["default"],
323
- name_localizations: command.name.except("default"),
324
- description: command.description["default"],
325
- description_localizations: command.description.except("default"),
326
- type: 1,
327
- options: command.to_hash[:options],
328
- }
329
- else
330
- {
331
- name: command.name["default"],
332
- name_localizations: command.name.except("default"),
333
- description: command.description["default"],
334
- description_localizations: command.description.except("default"),
335
- type: 2,
336
- options: command.commands.map do |c|
337
- c.to_hash.merge(type: 1).except(:dm_permission, :default_member_permissions)
338
- end,
339
- }
340
- end
341
- end
342
-
343
- {
344
- name: @name["default"],
345
- name_localizations: @name.except("default"),
346
- description: @description["default"],
347
- description_localizations: @description.except("default"),
348
- dm_permission: @dm_permission,
349
- default_member_permissions: @default_permission&.value&.to_s,
350
- options: options_payload,
351
- }
352
- end
353
- end
354
-
355
- #
356
- # Represents the subcommand group.
357
- #
358
- class SubcommandGroup < GroupCommand
359
- # @return [Array<Discorb::ApplicationCommand::Command::ChatInputCommand>] The subcommands of the command.
360
- attr_reader :commands
361
-
362
- #
363
- # Initialize a new subcommand group.
364
- # @private
365
- #
366
- # @param [String] name The name of the command.
367
- # @param [String] description The description of the command.
368
- # @param [Discorb::ApplicationCommand::Command::GroupCommand] parent The parent command.
369
- # @param [Discorb::Client] client The client.
370
- def initialize(name, description, parent, client)
371
- super(name, description, [], client, nil, nil)
372
-
373
- @commands = []
374
- @parent = parent
375
- end
376
-
377
- def to_s
378
- "#{@parent} #{@name}"
379
- end
380
-
381
- #
382
- # Add new subcommand.
383
- # @param (see Discorb::ApplicationCommand::Handler#slash)
384
- # @return [Discorb::ApplicationCommand::Command::ChatInputCommand] The added subcommand.
385
- #
386
- def slash(command_name, description, options = {}, &block)
387
- command = Discorb::ApplicationCommand::Command::ChatInputCommand.new(
388
- command_name, description, options, [],
389
- block, 1, self, nil, nil
390
- )
391
- @commands << command
392
- @client.callable_commands << command
393
- command
394
- end
395
- end
396
- end
397
- end
398
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Discorb
4
+ module ApplicationCommand
5
+ #
6
+ # Represents a application command.
7
+ # @abstract
8
+ #
9
+ class Command < DiscordModel
10
+ # @return [Hash{String => String}] The name of the command.
11
+ attr_reader :name
12
+ # @return [Array<#to_s>] The guild ids that the command is enabled in.
13
+ attr_reader :guild_ids
14
+ # @return [Proc] The block of the command.
15
+ attr_reader :block
16
+ # @return [:chat_input, :user, :message] The type of the command.
17
+ attr_reader :type
18
+ # @return [Integer] The raw type of the command.
19
+ attr_reader :type_raw
20
+ # @return [Discorb::Permission] The default permissions for this command.
21
+ attr_reader :default_permission
22
+ # @return [Boolean] Whether the command is enabled in DMs.
23
+ attr_reader :dm_permission
24
+
25
+ # @private
26
+ # @return [{Integer => Symbol}] The mapping of raw types to types.
27
+ TYPES = { 1 => :chat_input, 2 => :user, 3 => :message }.freeze
28
+
29
+ #
30
+ # Initialize a new command.
31
+ # @private
32
+ #
33
+ # @param [String, Hash{Symbol => String}] name The name of the command.
34
+ # @param [Array<#to_s>, false, nil] guild_ids The guild ids that the command is enabled in.
35
+ # @param [Proc] block The block of the command.
36
+ # @param [Integer] type The type of the command.
37
+ # @param [Boolean] dm_permission Whether the command is enabled in DMs.
38
+ # @param [Discorb::Permission] default_permission The default permission of the command.
39
+ #
40
+ def initialize(
41
+ name,
42
+ guild_ids,
43
+ block,
44
+ type,
45
+ dm_permission = true,
46
+ default_permission = nil
47
+ )
48
+ @name =
49
+ (
50
+ if name.is_a?(String)
51
+ { "default" => name }
52
+ else
53
+ ApplicationCommand.modify_localization_hash(name)
54
+ end
55
+ )
56
+ @guild_ids = guild_ids&.map(&:to_s)
57
+ @block = block
58
+ @type = Discorb::ApplicationCommand::Command::TYPES[type]
59
+ @type_raw = type
60
+ @dm_permission = dm_permission
61
+ @default_permission = default_permission
62
+ end
63
+
64
+ #
65
+ # Changes the self pointer of block to the given object.
66
+ # @private
67
+ #
68
+ # @param [Object] instance The object to change the self pointer to.
69
+ #
70
+ def replace_block(instance)
71
+ current_block = @block.dup
72
+ @block = proc { |*args| instance.instance_exec(*args, &current_block) }
73
+ end
74
+
75
+ #
76
+ # Converts the object to a hash.
77
+ # @private
78
+ #
79
+ # @return [Hash] The hash represents the object.
80
+ #
81
+ def to_hash
82
+ {
83
+ name: @name["default"],
84
+ name_localizations: @name.except("default"),
85
+ type: @type_raw,
86
+ dm_permission: @dm_permission,
87
+ default_member_permissions: @default_permission&.value&.to_s
88
+ }
89
+ end
90
+
91
+ #
92
+ # Represents the slash command.
93
+ #
94
+ class ChatInputCommand < Command
95
+ # @return [Hash{String => String}] The description of the command.
96
+ attr_reader :description
97
+ # @return [Hash{String => Hash}] The options of the command.
98
+ attr_reader :options
99
+
100
+ #
101
+ # Initialize a new slash command.
102
+ # @private
103
+ #
104
+ # @param [String, Hash{Symbol => String}] name The name of the command.
105
+ # The hash should have `default`, and language keys.
106
+ # @param [String, Hash{Symbol => String}] description The description of the command.
107
+ # The hash should have `default`, and language keys.
108
+ # @param [Hash{String => Hash}] options The options of the command.
109
+ # @param [Array<#to_s>] guild_ids The guild ids that the command is enabled in.
110
+ # @param [Proc] block The block of the command.
111
+ # @param [Integer] type The type of the command.
112
+ # @param [Discorb::ApplicationCommand::Command, nil] parent The parent command.
113
+ # @param [Boolean] dm_permission Whether the command is enabled in DMs.
114
+ # @param [Discorb::Permission] default_permission The default permission of the command.
115
+ #
116
+ def initialize(
117
+ name,
118
+ description,
119
+ options,
120
+ guild_ids,
121
+ block,
122
+ type,
123
+ parent,
124
+ dm_permission,
125
+ default_permission
126
+ )
127
+ super(name, guild_ids, block, type, dm_permission, default_permission)
128
+ @description =
129
+ if description.is_a?(String)
130
+ { "default" => description }
131
+ else
132
+ ApplicationCommand.modify_localization_hash(description)
133
+ end
134
+ @options = options
135
+ @parent = parent
136
+ end
137
+
138
+ #
139
+ # Returns the commands name.
140
+ #
141
+ # @return [String] The name of the command.
142
+ #
143
+ def to_s
144
+ "#{@parent} #{@name["default"]}".strip
145
+ end
146
+
147
+ #
148
+ # Converts the object to a hash.
149
+ # @private
150
+ #
151
+ # @return [Hash] The hash represents the object.
152
+ #
153
+ def to_hash
154
+ options_payload =
155
+ options.map do |name, value|
156
+ ret = {
157
+ type:
158
+ case value[:type]
159
+ when String, :string, :str
160
+ 3
161
+ when Integer, :integer, :int
162
+ 4
163
+ when TrueClass, FalseClass, :boolean, :bool
164
+ 5
165
+ when Discorb::User, Discorb::Member, :user, :member
166
+ 6
167
+ when Discorb::Channel, :channel
168
+ 7
169
+ when Discorb::Role, :role
170
+ 8
171
+ when :mentionable
172
+ 9
173
+ when Float, :float
174
+ 10
175
+ when :attachment
176
+ 11
177
+ else
178
+ raise ArgumentError, "Invalid option type: #{value[:type]}"
179
+ end,
180
+ name: name,
181
+ name_localizations:
182
+ ApplicationCommand.modify_localization_hash(
183
+ value[:name_localizations]
184
+ ),
185
+ required:
186
+ value[:required].nil? ? !value[:optional] : value[:required]
187
+ }
188
+
189
+ if value[:description].is_a?(String)
190
+ ret[:description] = value[:description]
191
+ else
192
+ description =
193
+ ApplicationCommand.modify_localization_hash(
194
+ value[:description]
195
+ )
196
+ ret[:description] = description["default"]
197
+ ret[:description_localizations] = description.except("default")
198
+ end
199
+ if value[:choices]
200
+ ret[:choices] = value[:choices].map do |k, v|
201
+ r = { name: k, value: v }
202
+ if choices_localizations = value[:choices_localizations].clone
203
+ name_localizations =
204
+ ApplicationCommand.modify_localization_hash(
205
+ choices_localizations.delete(k) do
206
+ warn "Missing localization for #{k}"
207
+ {}
208
+ end
209
+ )
210
+ r[:name_localizations] = name_localizations.except(
211
+ "default"
212
+ )
213
+ r[:name] = name_localizations["default"]
214
+ r.delete(:name_localizations) if r[:name_localizations].nil?
215
+ end
216
+ r
217
+ end
218
+ end
219
+
220
+ ret[:channel_types] = value[:channel_types].map(
221
+ &:channel_type
222
+ ) if value[:channel_types]
223
+
224
+ ret[:autocomplete] = !value[:autocomplete].nil? if value[
225
+ :autocomplete
226
+ ]
227
+ if value[:range]
228
+ ret[:min_value] = value[:range].begin
229
+ ret[:max_value] = value[:range].end
230
+ end
231
+ if value[:length]
232
+ ret[:min_length] = value[:length].begin
233
+ ret[:max_length] = value[:length].end
234
+ end
235
+ ret
236
+ end
237
+ {
238
+ name: @name["default"],
239
+ name_localizations: @name.except("default"),
240
+ description: @description["default"],
241
+ description_localizations: @description.except("default"),
242
+ options: options_payload,
243
+ dm_permission: @dm_permission,
244
+ default_member_permissions: @default_permission&.value&.to_s
245
+ }
246
+ end
247
+ end
248
+
249
+ #
250
+ # Represents the command with subcommands.
251
+ #
252
+ class GroupCommand < Command
253
+ # @return [Array<Discorb::ApplicationCommand::Command>] The subcommands of the command.
254
+ attr_reader :commands
255
+ # @return [Hash{String => String}] The description of the command.
256
+ attr_reader :description
257
+
258
+ #
259
+ # Initialize a new group command.
260
+ # @private
261
+ #
262
+ # @param [String, Hash{Symbol => String}] name The name of the command.
263
+ # @param [String, Hash{Symbol => String}] description The description of the command.
264
+ # @param [Array<#to_s>] guild_ids The guild ids that the command is enabled in.
265
+ # @param [Discorb::Client] client The client of the command.
266
+ # @param [Boolean] dm_permission Whether the command is enabled in DMs.
267
+ # @param [Discorb::Permission] default_permission The default permission of the command.
268
+ #
269
+ def initialize(
270
+ name,
271
+ description,
272
+ guild_ids,
273
+ client,
274
+ dm_permission,
275
+ default_permission
276
+ )
277
+ super(name, guild_ids, block, 1, dm_permission, default_permission)
278
+ @description =
279
+ if description.is_a?(String)
280
+ { "default" => description }
281
+ else
282
+ ApplicationCommand.modify_localization_hash(description)
283
+ end
284
+ @commands = []
285
+ @client = client
286
+ end
287
+
288
+ #
289
+ # Add new subcommand.
290
+ #
291
+ # @param (see Discorb::ApplicationCommand::Handler#slash)
292
+ # @return [Discorb::ApplicationCommand::Command::ChatInputCommand] The added subcommand.
293
+ #
294
+ def slash(
295
+ command_name,
296
+ description,
297
+ options = {},
298
+ dm_permission: true,
299
+ default_permission: nil,
300
+ &block
301
+ )
302
+ command =
303
+ Discorb::ApplicationCommand::Command::ChatInputCommand.new(
304
+ command_name,
305
+ description,
306
+ options,
307
+ [],
308
+ block,
309
+ 1,
310
+ self,
311
+ dm_permission,
312
+ default_permission
313
+ )
314
+ @client.callable_commands << command
315
+ @commands << command
316
+ command
317
+ end
318
+
319
+ #
320
+ # Add new subcommand group.
321
+ #
322
+ # @param [String] command_name Group name.
323
+ # @param [String] description Group description.
324
+ #
325
+ # @yield Block to yield with the command.
326
+ # @yieldparam [Discorb::ApplicationCommand::Command::SubcommandGroup] group Group command.
327
+ #
328
+ # @return [Discorb::ApplicationCommand::Command::SubcommandGroup] Command object.
329
+ #
330
+ # @see file:docs/application_command.md Application Commands
331
+ #
332
+ def group(command_name, description)
333
+ command =
334
+ Discorb::ApplicationCommand::Command::SubcommandGroup.new(
335
+ command_name,
336
+ description,
337
+ self,
338
+ @client
339
+ )
340
+ yield command if block_given?
341
+ @commands << command
342
+ command
343
+ end
344
+
345
+ #
346
+ # Returns the command name.
347
+ #
348
+ # @return [String] The command name.
349
+ #
350
+ def to_s
351
+ @name["default"]
352
+ end
353
+
354
+ #
355
+ # Changes the self pointer to the given object.
356
+ # @private
357
+ #
358
+ # @param [Object] instance The object to change to.
359
+ #
360
+ def block_replace(instance)
361
+ super
362
+ @commands.each { |c| c.replace_block(instance) }
363
+ end
364
+
365
+ #
366
+ # Converts the object to a hash.
367
+ # @private
368
+ #
369
+ # @return [Hash] The hash represents the object.
370
+ #
371
+ def to_hash
372
+ options_payload =
373
+ @commands.map do |command|
374
+ if command.is_a?(ChatInputCommand)
375
+ {
376
+ name: command.name["default"],
377
+ name_localizations: command.name.except("default"),
378
+ description: command.description["default"],
379
+ description_localizations:
380
+ command.description.except("default"),
381
+ type: 1,
382
+ options: command.to_hash[:options]
383
+ }
384
+ else
385
+ {
386
+ name: command.name["default"],
387
+ name_localizations: command.name.except("default"),
388
+ description: command.description["default"],
389
+ description_localizations:
390
+ command.description.except("default"),
391
+ type: 2,
392
+ options:
393
+ command.commands.map do |c|
394
+ c
395
+ .to_hash
396
+ .merge(type: 1)
397
+ .except(:dm_permission, :default_member_permissions)
398
+ end
399
+ }
400
+ end
401
+ end
402
+
403
+ {
404
+ name: @name["default"],
405
+ name_localizations: @name.except("default"),
406
+ description: @description["default"],
407
+ description_localizations: @description.except("default"),
408
+ dm_permission: @dm_permission,
409
+ default_member_permissions: @default_permission&.value&.to_s,
410
+ options: options_payload
411
+ }
412
+ end
413
+ end
414
+
415
+ #
416
+ # Represents the subcommand group.
417
+ #
418
+ class SubcommandGroup < GroupCommand
419
+ # @return [Array<Discorb::ApplicationCommand::Command::ChatInputCommand>] The subcommands of the command.
420
+ attr_reader :commands
421
+
422
+ #
423
+ # Initialize a new subcommand group.
424
+ # @private
425
+ #
426
+ # @param [String] name The name of the command.
427
+ # @param [String] description The description of the command.
428
+ # @param [Discorb::ApplicationCommand::Command::GroupCommand] parent The parent command.
429
+ # @param [Discorb::Client] client The client.
430
+ def initialize(name, description, parent, client)
431
+ super(name, description, [], client, nil, nil)
432
+
433
+ @commands = []
434
+ @parent = parent
435
+ end
436
+
437
+ def to_s
438
+ "#{@parent} #{@name}"
439
+ end
440
+
441
+ #
442
+ # Add new subcommand.
443
+ # @param (see Discorb::ApplicationCommand::Handler#slash)
444
+ # @return [Discorb::ApplicationCommand::Command::ChatInputCommand] The added subcommand.
445
+ #
446
+ def slash(command_name, description, options = {}, &block)
447
+ command =
448
+ Discorb::ApplicationCommand::Command::ChatInputCommand.new(
449
+ command_name,
450
+ description,
451
+ options,
452
+ [],
453
+ block,
454
+ 1,
455
+ self,
456
+ nil,
457
+ nil
458
+ )
459
+ @commands << command
460
+ @client.callable_commands << command
461
+ command
462
+ end
463
+ end
464
+ end
465
+ end
466
+ end