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,266 +1,304 @@
1
- # frozen_string_literal: true
2
-
3
- module Discorb
4
- module ApplicationCommand
5
- #
6
- # Module to handle application commands.
7
- #
8
- module Handler
9
- # @type instance: Discorb::Client
10
-
11
- #
12
- # Add new top-level command.
13
- #
14
- # @param [String, Hash{Symbol => String}] command_name Command name.
15
- # If hash is passed, it must be a pair of Language code and Command name, and `:default` key is required.
16
- # You can use `_` instead of `-` in language code.
17
- # @param [String, Hash{Symbol => String}] description Command description.
18
- # If hash is passed, it must be a pair of Language code and Command description, and `:default` key is required.
19
- # You can use `_` instead of `-` in language code.
20
- # @param [Hash{String => Hash{:description => String, :optional => Boolean, :type => Object}}] options
21
- # Command options.
22
- # The key is the option name, the value is a hash with the following keys:
23
- #
24
- # | Key | Type | Description |
25
- # | --- | --- | --- |
26
- # | `:name_localizations` | Hash{Symbol => String} | Localizations of option name. |
27
- # | `:description` | `String` \| `Hash{Symbol => String}` |
28
- # Description of the option. If hash is passed, it must be a pair of Language code and description,
29
- # and `:default` key is required. You can use `_` instead of `-` in language code. |
30
- # | `:required` | Boolean(true | false) |
31
- # Whether the argument is required. `optional` will be used if not specified. |
32
- # | `:optional` | Boolean(true | false) |
33
- # Whether the argument is optional. `required` will be used if not specified. |
34
- # | `:type` | `Object` | Type of the option. |
35
- # | `:choices` | `Hash{String => String, Integer, Float}` | Type of the option. |
36
- # | `:choices_localizations` | `Hash{String => Hash{Symbol => String}}` |
37
- # Localization of the choice. Key must be the name of a choice. |
38
- # | `:default` | `Object` | Default value of the option. |
39
- # | `:channel_types` | `Array<Class<Discorb::Channel>>` | Type of the channel option. |
40
- # | `:autocomplete` | `Proc` | Autocomplete function. |
41
- # | `:range` | `Range` | Range of the option. Only valid for numeric options. (`:int`, `:float`) |
42
- # | `:length` | `Range` | Range of length of the option. Only valid for `:string`. |
43
- #
44
- # @param [Array<#to_s>, false, nil] guild_ids
45
- # Guild IDs to set the command to. `false` to global command, `nil` to use default.
46
- # @param [Boolean] dm_permission Whether the command is available in DM.
47
- # @param [Discorb::Permission] default_permission The default permission of the command.
48
- # @param [Proc] block Command block.
49
- #
50
- # @return [Discorb::ApplicationCommand::Command::ChatInputCommand] Command object.
51
- #
52
- # @see file:docs/application_command.md#register-slash-command Application Comamnds: Register Slash Command
53
- # @see file:docs/cli/setup.md CLI: setup
54
- #
55
- def slash(
56
- command_name,
57
- description,
58
- options = {},
59
- guild_ids: nil,
60
- dm_permission: true,
61
- default_permission: nil,
62
- &block
63
- )
64
- command_name = { default: command_name } if command_name.is_a?(String)
65
- description = { default: description } if description.is_a?(String)
66
- command_name = ApplicationCommand.modify_localization_hash(command_name)
67
- description = ApplicationCommand.modify_localization_hash(description)
68
- command = Discorb::ApplicationCommand::Command::ChatInputCommand.new(
69
- command_name,
70
- description,
71
- options,
72
- guild_ids,
73
- block,
74
- 1,
75
- nil,
76
- dm_permission,
77
- default_permission
78
- )
79
- @commands << command
80
- @callable_commands << command
81
- command
82
- end
83
-
84
- #
85
- # Add new command with group.
86
- #
87
- # @param [String, Hash{Symbol => String}] command_name Command name.
88
- # @param [String, Hash{Symbol => String}] description Command description.
89
- # @param [Array<#to_s>, false, nil] guild_ids
90
- # Guild IDs to set the command to. `false` to global command, `nil` to use default.
91
- # @param [Boolean] dm_permission Whether the command is available in DM.
92
- # @param [Discorb::Permission] default_permission The default permission of the command.
93
- #
94
- # @yield Block to yield with the command.
95
- # @yieldparam [Discorb::ApplicationCommand::Command::GroupCommand] group Group command.
96
- #
97
- # @return [Discorb::ApplicationCommand::Command::GroupCommand] Command object.
98
- #
99
- # @see file:docs/application_command.md Application Commands
100
- # @see file:docs/cli/setup.md CLI: setup
101
- #
102
- def slash_group(command_name, description, guild_ids: nil, dm_permission: true, default_permission: nil)
103
- command_name = { default: command_name } if command_name.is_a?(String)
104
- description = { default: description } if description.is_a?(String)
105
- command_name = ApplicationCommand.modify_localization_hash(command_name)
106
- description = ApplicationCommand.modify_localization_hash(description)
107
- command = Discorb::ApplicationCommand::Command::GroupCommand.new(
108
- command_name,
109
- description,
110
- guild_ids,
111
- self,
112
- dm_permission,
113
- default_permission
114
- )
115
- yield command if block_given?
116
- @commands << command
117
- command
118
- end
119
-
120
- #
121
- # Add message context menu command.
122
- #
123
- # @param [String, Hash{Symbol => String}] command_name Command name.
124
- # @param [Array<#to_s>, false, nil] guild_ids
125
- # Guild IDs to set the command to. `false` to global command, `nil` to use default.
126
- # @param [Boolean] dm_permission Whether the command is available in DM.
127
- # @param [Discorb::Permission] default_permission The default permission of the command.
128
- # @param [Proc] block Command block.
129
- # @yield [interaction, message] Block to execute.
130
- # @yieldparam [Discorb::CommandInteraction::UserMenuCommand] interaction Interaction object.
131
- # @yieldparam [Discorb::Message] message Message object.
132
- #
133
- # @return [Discorb::ApplicationCommand::Command] Command object.
134
- #
135
- def message_command(command_name, guild_ids: nil, dm_permission: true, default_permission: nil, &block)
136
- command_name = { default: command_name } if command_name.is_a?(String)
137
- command_name = ApplicationCommand.modify_localization_hash(command_name)
138
- command = Discorb::ApplicationCommand::Command.new(
139
- command_name,
140
- guild_ids,
141
- block,
142
- 3,
143
- dm_permission,
144
- default_permission
145
- )
146
- @commands << command
147
- command
148
- end
149
-
150
- #
151
- # Add user context menu command.
152
- #
153
- # @param [String, Hash{Symbol => String}] command_name Command name.
154
- # @param [Array<#to_s>, false, nil] guild_ids
155
- # Guild IDs to set the command to. `false` to global command, `nil` to use default.
156
- # @param [Boolean] dm_permission Whether the command is available in DM.
157
- # @param [Discorb::Permission] default_permission The default permission of the command.
158
- # @param [Proc] block Command block.
159
- # @yield [interaction, user] Block to execute.
160
- # @yieldparam [Discorb::CommandInteraction::UserMenuCommand] interaction Interaction object.
161
- # @yieldparam [Discorb::User] user User object.
162
- #
163
- # @return [Discorb::ApplicationCommand::Command] Command object.
164
- #
165
- def user_command(command_name, guild_ids: nil, dm_permission: true, default_permission: nil, &block)
166
- command_name = { default: command_name } if command_name.is_a?(String)
167
- command_name = ApplicationCommand.modify_localization_hash(command_name)
168
- command = Discorb::ApplicationCommand::Command.new(command_name, guild_ids, block, 2, dm_permission,
169
- default_permission)
170
- @commands << command
171
- command
172
- end
173
-
174
- #
175
- # Setup commands.
176
- # @async
177
- # @see Client#initialize
178
- #
179
- # @param [String] token Bot token.
180
- # @param [Array<#to_s>, false, nil] guild_ids
181
- # Guild IDs to use as default. If `false` is given, it will be global command.
182
- #
183
- # @note `token` parameter only required if you don't run client.
184
- #
185
- def setup_commands(token = nil, guild_ids: nil)
186
- Async do
187
- @token ||= token
188
- @http = HTTP.new(self)
189
- global_commands = @commands.select { |c| c.guild_ids == false or c.guild_ids == [] }
190
- local_commands = @commands.select { |c| c.guild_ids.is_a?(Array) and c.guild_ids.any? }
191
- default_commands = @commands.select { |c| c.guild_ids.nil? }
192
- if guild_ids.is_a?(Array)
193
- default_commands.each do |command|
194
- command.instance_variable_set(:@guild_ids, guild_ids)
195
- end
196
- local_commands += default_commands
197
- else
198
- global_commands += default_commands
199
- end
200
- final_guild_ids = local_commands.map(&:guild_ids).flatten.map(&:to_s).uniq
201
- app_info = fetch_application.wait
202
- unless global_commands.empty?
203
- @http.request(
204
- Route.new(
205
- "/applications/#{app_info.id}/commands",
206
- "//applications/:application_id/commands",
207
- :put
208
- ),
209
- global_commands.map(&:to_hash)
210
- ).wait
211
- end
212
- if ENV.fetch("DISCORB_CLI_FLAG", nil) == "setup"
213
- sputs "Registered commands for global:"
214
- global_commands.each do |command|
215
- iputs "- #{command.name["default"]}"
216
- end
217
- end
218
- unless final_guild_ids.empty?
219
- final_guild_ids.each do |guild_id|
220
- commands = local_commands.select { |c| c.guild_ids.include?(guild_id) }
221
- @http.request(
222
- Route.new("/applications/#{app_info.id}/guilds/#{guild_id}/commands",
223
- "//applications/:application_id/guilds/:guild_id/commands",
224
- :put),
225
- commands.map(&:to_hash)
226
- ).wait
227
- sputs "Registered commands for #{guild_id}:"
228
- commands.each do |command|
229
- iputs "- #{command.name["default"]}"
230
- end
231
- end
232
- end
233
- @logger.info "Successfully setup commands"
234
- end
235
- end
236
-
237
- #
238
- # Claer commands in specified guilds.
239
- # @async
240
- # @see Client#initialize
241
- #
242
- # @param [String] token Bot token.
243
- # @param [Array<#to_s>, false, nil] guild_ids Guild IDs to clear.
244
- #
245
- # @note `token` parameter only required if you don't run client.
246
- #
247
- def clear_commands(token, guild_ids)
248
- Async do
249
- @token ||= token
250
- @http = HTTP.new(self)
251
- app_info = fetch_application.wait
252
-
253
- guild_ids.each do |guild_id|
254
- @http.request(
255
- Route.new("/applications/#{app_info.id}/guilds/#{guild_id}/commands",
256
- "//applications/:application_id/guilds/:guild_id/commands",
257
- :put),
258
- []
259
- ).wait
260
- end
261
- sputs "Cleared commands for #{guild_ids.length} guilds." if ENV.fetch("DISCORB_CLI_FLAG", nil) == "setup"
262
- end
263
- end
264
- end
265
- end
266
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Discorb
4
+ module ApplicationCommand
5
+ #
6
+ # Module to handle application commands.
7
+ #
8
+ module Handler
9
+ # @type instance: Discorb::Client
10
+
11
+ #
12
+ # Add new top-level command.
13
+ #
14
+ # @param [String, Hash{Symbol => String}] command_name Command name.
15
+ # If hash is passed, it must be a pair of Language code and Command name, and `:default` key is required.
16
+ # You can use `_` instead of `-` in language code.
17
+ # @param [String, Hash{Symbol => String}] description Command description.
18
+ # If hash is passed, it must be a pair of Language code and Command description, and `:default` key is required.
19
+ # You can use `_` instead of `-` in language code.
20
+ # @param [Hash{String => Hash{:description => String, :optional => Boolean, :type => Object}}] options
21
+ # Command options.
22
+ # The key is the option name, the value is a hash with the following keys:
23
+ #
24
+ # | Key | Type | Description |
25
+ # | --- | --- | --- |
26
+ # | `:name_localizations` | Hash{Symbol => String} | Localizations of option name. |
27
+ # | `:description` | `String` \| `Hash{Symbol => String}` |
28
+ # Description of the option. If hash is passed, it must be a pair of Language code and description,
29
+ # and `:default` key is required. You can use `_` instead of `-` in language code. |
30
+ # | `:required` | Boolean(true | false) |
31
+ # Whether the argument is required. `optional` will be used if not specified. |
32
+ # | `:optional` | Boolean(true | false) |
33
+ # Whether the argument is optional. `required` will be used if not specified. |
34
+ # | `:type` | `Object` | Type of the option. |
35
+ # | `:choices` | `Hash{String => String, Integer, Float}` | Type of the option. |
36
+ # | `:choices_localizations` | `Hash{String => Hash{Symbol => String}}` |
37
+ # Localization of the choice. Key must be the name of a choice. |
38
+ # | `:default` | `Object` | Default value of the option. |
39
+ # | `:channel_types` | `Array<Class<Discorb::Channel>>` | Type of the channel option. |
40
+ # | `:autocomplete` | `Proc` | Autocomplete function. |
41
+ # | `:range` | `Range` | Range of the option. Only valid for numeric options. (`:int`, `:float`) |
42
+ # | `:length` | `Range` | Range of length of the option. Only valid for `:string`. |
43
+ #
44
+ # @param [Array<#to_s>, false, nil] guild_ids
45
+ # Guild IDs to set the command to. `false` to global command, `nil` to use default.
46
+ # @param [Boolean] dm_permission Whether the command is available in DM.
47
+ # @param [Discorb::Permission] default_permission The default permission of the command.
48
+ # @param [Proc] block Command block.
49
+ #
50
+ # @return [Discorb::ApplicationCommand::Command::ChatInputCommand] Command object.
51
+ #
52
+ # @see file:docs/application_command.md#register-slash-command Application Comamnds: Register Slash Command
53
+ # @see file:docs/cli/setup.md CLI: setup
54
+ #
55
+ def slash(
56
+ command_name,
57
+ description,
58
+ options = {},
59
+ guild_ids: nil,
60
+ dm_permission: true,
61
+ default_permission: nil,
62
+ &block
63
+ )
64
+ command_name = { default: command_name } if command_name.is_a?(String)
65
+ description = { default: description } if description.is_a?(String)
66
+ command_name = ApplicationCommand.modify_localization_hash(command_name)
67
+ description = ApplicationCommand.modify_localization_hash(description)
68
+ command =
69
+ Discorb::ApplicationCommand::Command::ChatInputCommand.new(
70
+ command_name,
71
+ description,
72
+ options,
73
+ guild_ids,
74
+ block,
75
+ 1,
76
+ nil,
77
+ dm_permission,
78
+ default_permission
79
+ )
80
+ @commands << command
81
+ @callable_commands << command
82
+ command
83
+ end
84
+
85
+ #
86
+ # Add new command with group.
87
+ #
88
+ # @param [String, Hash{Symbol => String}] command_name Command name.
89
+ # @param [String, Hash{Symbol => String}] description Command description.
90
+ # @param [Array<#to_s>, false, nil] guild_ids
91
+ # Guild IDs to set the command to. `false` to global command, `nil` to use default.
92
+ # @param [Boolean] dm_permission Whether the command is available in DM.
93
+ # @param [Discorb::Permission] default_permission The default permission of the command.
94
+ #
95
+ # @yield Block to yield with the command.
96
+ # @yieldparam [Discorb::ApplicationCommand::Command::GroupCommand] group Group command.
97
+ #
98
+ # @return [Discorb::ApplicationCommand::Command::GroupCommand] Command object.
99
+ #
100
+ # @see file:docs/application_command.md Application Commands
101
+ # @see file:docs/cli/setup.md CLI: setup
102
+ #
103
+ def slash_group(
104
+ command_name,
105
+ description,
106
+ guild_ids: nil,
107
+ dm_permission: true,
108
+ default_permission: nil
109
+ )
110
+ command_name = { default: command_name } if command_name.is_a?(String)
111
+ description = { default: description } if description.is_a?(String)
112
+ command_name = ApplicationCommand.modify_localization_hash(command_name)
113
+ description = ApplicationCommand.modify_localization_hash(description)
114
+ command =
115
+ Discorb::ApplicationCommand::Command::GroupCommand.new(
116
+ command_name,
117
+ description,
118
+ guild_ids,
119
+ self,
120
+ dm_permission,
121
+ default_permission
122
+ )
123
+ yield command if block_given?
124
+ @commands << command
125
+ command
126
+ end
127
+
128
+ #
129
+ # Add message context menu command.
130
+ #
131
+ # @param [String, Hash{Symbol => String}] command_name Command name.
132
+ # @param [Array<#to_s>, false, nil] guild_ids
133
+ # Guild IDs to set the command to. `false` to global command, `nil` to use default.
134
+ # @param [Boolean] dm_permission Whether the command is available in DM.
135
+ # @param [Discorb::Permission] default_permission The default permission of the command.
136
+ # @param [Proc] block Command block.
137
+ # @yield [interaction, message] Block to execute.
138
+ # @yieldparam [Discorb::CommandInteraction::UserMenuCommand] interaction Interaction object.
139
+ # @yieldparam [Discorb::Message] message Message object.
140
+ #
141
+ # @return [Discorb::ApplicationCommand::Command] Command object.
142
+ #
143
+ def message_command(
144
+ command_name,
145
+ guild_ids: nil,
146
+ dm_permission: true,
147
+ default_permission: nil,
148
+ &block
149
+ )
150
+ command_name = { default: command_name } if command_name.is_a?(String)
151
+ command_name = ApplicationCommand.modify_localization_hash(command_name)
152
+ command =
153
+ Discorb::ApplicationCommand::Command.new(
154
+ command_name,
155
+ guild_ids,
156
+ block,
157
+ 3,
158
+ dm_permission,
159
+ default_permission
160
+ )
161
+ @commands << command
162
+ command
163
+ end
164
+
165
+ #
166
+ # Add user context menu command.
167
+ #
168
+ # @param [String, Hash{Symbol => String}] command_name Command name.
169
+ # @param [Array<#to_s>, false, nil] guild_ids
170
+ # Guild IDs to set the command to. `false` to global command, `nil` to use default.
171
+ # @param [Boolean] dm_permission Whether the command is available in DM.
172
+ # @param [Discorb::Permission] default_permission The default permission of the command.
173
+ # @param [Proc] block Command block.
174
+ # @yield [interaction, user] Block to execute.
175
+ # @yieldparam [Discorb::CommandInteraction::UserMenuCommand] interaction Interaction object.
176
+ # @yieldparam [Discorb::User] user User object.
177
+ #
178
+ # @return [Discorb::ApplicationCommand::Command] Command object.
179
+ #
180
+ def user_command(
181
+ command_name,
182
+ guild_ids: nil,
183
+ dm_permission: true,
184
+ default_permission: nil,
185
+ &block
186
+ )
187
+ command_name = { default: command_name } if command_name.is_a?(String)
188
+ command_name = ApplicationCommand.modify_localization_hash(command_name)
189
+ command =
190
+ Discorb::ApplicationCommand::Command.new(
191
+ command_name,
192
+ guild_ids,
193
+ block,
194
+ 2,
195
+ dm_permission,
196
+ default_permission
197
+ )
198
+ @commands << command
199
+ command
200
+ end
201
+
202
+ #
203
+ # Setup commands.
204
+ # @async
205
+ # @see Client#initialize
206
+ #
207
+ # @param [String] token Bot token.
208
+ # @param [Array<#to_s>, false, nil] guild_ids
209
+ # Guild IDs to use as default. If `false` is given, it will be global command.
210
+ #
211
+ # @note `token` parameter only required if you don't run client.
212
+ #
213
+ def setup_commands(token = nil, guild_ids: nil)
214
+ Async do
215
+ @token ||= token
216
+ @http = HTTP.new(self)
217
+ global_commands =
218
+ @commands.select { |c| c.guild_ids == false or c.guild_ids == [] }
219
+ local_commands =
220
+ @commands.select do |c|
221
+ c.guild_ids.is_a?(Array) and c.guild_ids.any?
222
+ end
223
+ default_commands = @commands.select { |c| c.guild_ids.nil? }
224
+ if guild_ids.is_a?(Array)
225
+ default_commands.each do |command|
226
+ command.instance_variable_set(:@guild_ids, guild_ids)
227
+ end
228
+ local_commands += default_commands
229
+ else
230
+ global_commands += default_commands
231
+ end
232
+ final_guild_ids =
233
+ local_commands.map(&:guild_ids).flatten.map(&:to_s).uniq
234
+ app_info = fetch_application.wait
235
+ unless global_commands.empty?
236
+ @http.request(
237
+ Route.new(
238
+ "/applications/#{app_info.id}/commands",
239
+ "//applications/:application_id/commands",
240
+ :put
241
+ ),
242
+ global_commands.map(&:to_hash)
243
+ ).wait
244
+ end
245
+ if ENV.fetch("DISCORB_CLI_FLAG", nil) == "setup"
246
+ sputs "Registered commands for global:"
247
+ global_commands.each do |command|
248
+ iputs "- #{command.name["default"]}"
249
+ end
250
+ end
251
+ unless final_guild_ids.empty?
252
+ final_guild_ids.each do |guild_id|
253
+ commands =
254
+ local_commands.select { |c| c.guild_ids.include?(guild_id) }
255
+ @http.request(
256
+ Route.new(
257
+ "/applications/#{app_info.id}/guilds/#{guild_id}/commands",
258
+ "//applications/:application_id/guilds/:guild_id/commands",
259
+ :put
260
+ ),
261
+ commands.map(&:to_hash)
262
+ ).wait
263
+ sputs "Registered commands for #{guild_id}:"
264
+ commands.each { |command| iputs "- #{command.name["default"]}" }
265
+ end
266
+ end
267
+ @logger.info "Successfully setup commands"
268
+ end
269
+ end
270
+
271
+ #
272
+ # Claer commands in specified guilds.
273
+ # @async
274
+ # @see Client#initialize
275
+ #
276
+ # @param [String] token Bot token.
277
+ # @param [Array<#to_s>, false, nil] guild_ids Guild IDs to clear.
278
+ #
279
+ # @note `token` parameter only required if you don't run client.
280
+ #
281
+ def clear_commands(token, guild_ids)
282
+ Async do
283
+ @token ||= token
284
+ @http = HTTP.new(self)
285
+ app_info = fetch_application.wait
286
+
287
+ guild_ids.each do |guild_id|
288
+ @http.request(
289
+ Route.new(
290
+ "/applications/#{app_info.id}/guilds/#{guild_id}/commands",
291
+ "//applications/:application_id/guilds/:guild_id/commands",
292
+ :put
293
+ ),
294
+ []
295
+ ).wait
296
+ end
297
+ if ENV.fetch("DISCORB_CLI_FLAG", nil) == "setup"
298
+ sputs "Cleared commands for #{guild_ids.length} guilds."
299
+ end
300
+ end
301
+ end
302
+ end
303
+ end
304
+ end
@@ -1,5 +1,5 @@
1
- # frozen_string_literal: true
2
-
3
- %w[common command handler].each do |file|
4
- require_relative "app_command/#{file}.rb"
5
- end
1
+ # frozen_string_literal: true
2
+
3
+ %w[common command handler].each do |file|
4
+ require_relative "app_command/#{file}.rb"
5
+ end