discorb 0.18.0 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/build_version.yml +2 -2
  3. data/.rubocop.yml +12 -75
  4. data/Changelog.md +25 -0
  5. data/Gemfile +4 -4
  6. data/README.md +2 -1
  7. data/Rakefile +482 -459
  8. data/Steepfile +8 -6
  9. data/docs/application_command.md +1 -0
  10. data/docs/events.md +2 -2
  11. data/docs/voice_events.md +6 -6
  12. data/lib/discorb/allowed_mentions.rb +68 -72
  13. data/lib/discorb/app_command/command.rb +466 -394
  14. data/lib/discorb/app_command/common.rb +65 -25
  15. data/lib/discorb/app_command/handler.rb +304 -265
  16. data/lib/discorb/app_command.rb +5 -5
  17. data/lib/discorb/application.rb +198 -197
  18. data/lib/discorb/asset.rb +101 -101
  19. data/lib/discorb/attachment.rb +134 -119
  20. data/lib/discorb/audit_logs.rb +412 -385
  21. data/lib/discorb/automod.rb +279 -269
  22. data/lib/discorb/channel/base.rb +107 -108
  23. data/lib/discorb/channel/category.rb +32 -32
  24. data/lib/discorb/channel/container.rb +44 -44
  25. data/lib/discorb/channel/dm.rb +26 -28
  26. data/lib/discorb/channel/guild.rb +311 -246
  27. data/lib/discorb/channel/stage.rb +156 -140
  28. data/lib/discorb/channel/text.rb +430 -336
  29. data/lib/discorb/channel/thread.rb +374 -325
  30. data/lib/discorb/channel/voice.rb +85 -79
  31. data/lib/discorb/channel.rb +5 -5
  32. data/lib/discorb/client.rb +635 -623
  33. data/lib/discorb/color.rb +178 -182
  34. data/lib/discorb/common.rb +168 -164
  35. data/lib/discorb/components/button.rb +107 -106
  36. data/lib/discorb/components/select_menu.rb +157 -145
  37. data/lib/discorb/components/text_input.rb +103 -106
  38. data/lib/discorb/components.rb +68 -66
  39. data/lib/discorb/dictionary.rb +135 -135
  40. data/lib/discorb/embed.rb +404 -398
  41. data/lib/discorb/emoji.rb +309 -302
  42. data/lib/discorb/emoji_table.rb +16099 -8857
  43. data/lib/discorb/error.rb +131 -131
  44. data/lib/discorb/event.rb +360 -314
  45. data/lib/discorb/event_handler.rb +39 -39
  46. data/lib/discorb/exe/about.rb +17 -17
  47. data/lib/discorb/exe/irb.rb +72 -67
  48. data/lib/discorb/exe/new.rb +323 -315
  49. data/lib/discorb/exe/run.rb +69 -68
  50. data/lib/discorb/exe/setup.rb +57 -55
  51. data/lib/discorb/exe/show.rb +12 -12
  52. data/lib/discorb/extend.rb +25 -45
  53. data/lib/discorb/extension.rb +89 -83
  54. data/lib/discorb/flag.rb +126 -128
  55. data/lib/discorb/gateway.rb +984 -794
  56. data/lib/discorb/gateway_events.rb +670 -638
  57. data/lib/discorb/gateway_requests.rb +45 -48
  58. data/lib/discorb/guild.rb +2115 -1626
  59. data/lib/discorb/guild_template.rb +280 -241
  60. data/lib/discorb/http.rb +247 -232
  61. data/lib/discorb/image.rb +42 -42
  62. data/lib/discorb/integration.rb +169 -161
  63. data/lib/discorb/intents.rb +161 -163
  64. data/lib/discorb/interaction/autocomplete.rb +76 -62
  65. data/lib/discorb/interaction/command.rb +279 -224
  66. data/lib/discorb/interaction/components.rb +114 -104
  67. data/lib/discorb/interaction/modal.rb +36 -32
  68. data/lib/discorb/interaction/response.rb +379 -330
  69. data/lib/discorb/interaction/root.rb +271 -118
  70. data/lib/discorb/interaction.rb +5 -5
  71. data/lib/discorb/invite.rb +154 -153
  72. data/lib/discorb/member.rb +344 -311
  73. data/lib/discorb/message.rb +615 -544
  74. data/lib/discorb/message_meta.rb +197 -186
  75. data/lib/discorb/modules.rb +371 -290
  76. data/lib/discorb/permission.rb +305 -289
  77. data/lib/discorb/presence.rb +352 -346
  78. data/lib/discorb/rate_limit.rb +81 -76
  79. data/lib/discorb/reaction.rb +55 -54
  80. data/lib/discorb/role.rb +272 -240
  81. data/lib/discorb/shard.rb +76 -74
  82. data/lib/discorb/sticker.rb +193 -171
  83. data/lib/discorb/user.rb +205 -188
  84. data/lib/discorb/utils/colored_puts.rb +16 -16
  85. data/lib/discorb/utils.rb +12 -16
  86. data/lib/discorb/voice_state.rb +305 -281
  87. data/lib/discorb/webhook.rb +537 -507
  88. data/lib/discorb.rb +62 -56
  89. data/sig/discorb/activity.rbs +1 -0
  90. data/sig/discorb/allowed_mentions.rbs +1 -0
  91. data/sig/discorb/app_command/base.rbs +7 -1
  92. data/sig/discorb/application.rbs +6 -0
  93. data/sig/discorb/asset.rbs +2 -0
  94. data/sig/discorb/attachment.rbs +8 -0
  95. data/sig/discorb/audit_log.rbs +7 -0
  96. data/sig/discorb/automod.rbs +32 -6
  97. data/sig/discorb/avatar.rbs +1 -0
  98. data/sig/discorb/channel/base.rbs +8 -1
  99. data/sig/discorb/channel/category.rbs +1 -0
  100. data/sig/discorb/channel/container.rbs +4 -0
  101. data/sig/discorb/channel/stage.rbs +4 -0
  102. data/sig/discorb/channel/text.rbs +2 -2
  103. data/sig/discorb/channel/thread.rbs +11 -0
  104. data/sig/discorb/channel/voice.rbs +2 -0
  105. data/sig/discorb/client.rbs +21 -20
  106. data/sig/discorb/color.rbs +6 -0
  107. data/sig/discorb/component/base.rbs +1 -0
  108. data/sig/discorb/component/button.rbs +2 -0
  109. data/sig/discorb/component/select_menu.rbs +4 -0
  110. data/sig/discorb/component/text_input.rbs +1 -0
  111. data/sig/discorb/custom_emoji.rbs +5 -1
  112. data/sig/discorb/dictionary.rbs +2 -0
  113. data/sig/discorb/discord_model.rbs +2 -0
  114. data/sig/discorb/embed.rbs +7 -0
  115. data/sig/discorb/emoji.rbs +1 -0
  116. data/sig/discorb/event_handler.rbs +2 -1
  117. data/sig/discorb/extension.rbs +13 -12
  118. data/sig/discorb/flag.rbs +2 -0
  119. data/sig/discorb/gateway.rbs +5 -0
  120. data/sig/discorb/guild.rbs +8 -4
  121. data/sig/discorb/guild_template.rbs +1 -1
  122. data/sig/discorb/http.rbs +4 -1
  123. data/sig/discorb/image.rbs +2 -0
  124. data/sig/discorb/integration.rbs +1 -1
  125. data/sig/discorb/intents.rbs +4 -3
  126. data/sig/discorb/interaction/base.rbs +36 -0
  127. data/sig/discorb/interaction/message_component.rbs +1 -2
  128. data/sig/discorb/interaction/modal.rbs +1 -2
  129. data/sig/discorb/interaction/responder.rbs +49 -49
  130. data/sig/discorb/invite.rbs +1 -1
  131. data/sig/discorb/member.rbs +2 -0
  132. data/sig/discorb/message.rbs +8 -1
  133. data/sig/discorb/messageable.rbs +1 -4
  134. data/sig/discorb/partial_emoji.rbs +3 -0
  135. data/sig/discorb/permissions.rbs +7 -0
  136. data/sig/discorb/presence.rbs +2 -0
  137. data/sig/discorb/reaction.rbs +5 -1
  138. data/sig/discorb/role.rbs +7 -1
  139. data/sig/discorb/scheduled_event.rbs +2 -1
  140. data/sig/discorb/shard.rbs +2 -1
  141. data/sig/discorb/snowflake.rbs +2 -0
  142. data/sig/discorb/stage_instance.rbs +9 -3
  143. data/sig/discorb/sticker.rbs +1 -1
  144. data/sig/discorb/unicode_emoji.rbs +4 -0
  145. data/sig/discorb/user.rbs +24 -20
  146. data/sig/discorb/webhook.rbs +17 -6
  147. data/sig/discorb/welcome_screen.rbs +1 -0
  148. data/sig/override.rbs +2 -0
  149. metadata +3 -3
@@ -1,265 +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
- #
43
- # @param [Array<#to_s>, false, nil] guild_ids
44
- # Guild IDs to set the command to. `false` to global command, `nil` to use default.
45
- # @param [Boolean] dm_permission Whether the command is available in DM.
46
- # @param [Discorb::Permission] default_permission The default permission of the command.
47
- # @param [Proc] block Command block.
48
- #
49
- # @return [Discorb::ApplicationCommand::Command::ChatInputCommand] Command object.
50
- #
51
- # @see file:docs/application_command.md#register-slash-command Application Comamnds: Register Slash Command
52
- # @see file:docs/cli/setup.md CLI: setup
53
- #
54
- def slash(
55
- command_name,
56
- description,
57
- options = {},
58
- guild_ids: nil,
59
- dm_permission: true,
60
- default_permission: nil,
61
- &block
62
- )
63
- command_name = { default: command_name } if command_name.is_a?(String)
64
- description = { default: description } if description.is_a?(String)
65
- command_name = ApplicationCommand.modify_localization_hash(command_name)
66
- description = ApplicationCommand.modify_localization_hash(description)
67
- command = Discorb::ApplicationCommand::Command::ChatInputCommand.new(
68
- command_name,
69
- description,
70
- options,
71
- guild_ids,
72
- block,
73
- 1,
74
- nil,
75
- dm_permission,
76
- default_permission
77
- )
78
- @commands << command
79
- @callable_commands << command
80
- command
81
- end
82
-
83
- #
84
- # Add new command with group.
85
- #
86
- # @param [String, Hash{Symbol => String}] command_name Command name.
87
- # @param [String, Hash{Symbol => String}] description Command description.
88
- # @param [Array<#to_s>, false, nil] guild_ids
89
- # Guild IDs to set the command to. `false` to global command, `nil` to use default.
90
- # @param [Boolean] dm_permission Whether the command is available in DM.
91
- # @param [Discorb::Permission] default_permission The default permission of the command.
92
- #
93
- # @yield Block to yield with the command.
94
- # @yieldparam [Discorb::ApplicationCommand::Command::GroupCommand] group Group command.
95
- #
96
- # @return [Discorb::ApplicationCommand::Command::GroupCommand] Command object.
97
- #
98
- # @see file:docs/application_command.md Application Commands
99
- # @see file:docs/cli/setup.md CLI: setup
100
- #
101
- def slash_group(command_name, description, guild_ids: nil, dm_permission: true, default_permission: nil)
102
- command_name = { default: command_name } if command_name.is_a?(String)
103
- description = { default: description } if description.is_a?(String)
104
- command_name = ApplicationCommand.modify_localization_hash(command_name)
105
- description = ApplicationCommand.modify_localization_hash(description)
106
- command = Discorb::ApplicationCommand::Command::GroupCommand.new(
107
- command_name,
108
- description,
109
- guild_ids,
110
- self,
111
- dm_permission,
112
- default_permission
113
- )
114
- yield command if block_given?
115
- @commands << command
116
- command
117
- end
118
-
119
- #
120
- # Add message context menu command.
121
- #
122
- # @param [String, Hash{Symbol => String}] command_name Command name.
123
- # @param [Array<#to_s>, false, nil] guild_ids
124
- # Guild IDs to set the command to. `false` to global command, `nil` to use default.
125
- # @param [Boolean] dm_permission Whether the command is available in DM.
126
- # @param [Discorb::Permission] default_permission The default permission of the command.
127
- # @param [Proc] block Command block.
128
- # @yield [interaction, message] Block to execute.
129
- # @yieldparam [Discorb::CommandInteraction::UserMenuCommand] interaction Interaction object.
130
- # @yieldparam [Discorb::Message] message Message object.
131
- #
132
- # @return [Discorb::ApplicationCommand::Command] Command object.
133
- #
134
- def message_command(command_name, guild_ids: nil, dm_permission: true, default_permission: nil, &block)
135
- command_name = { default: command_name } if command_name.is_a?(String)
136
- command_name = ApplicationCommand.modify_localization_hash(command_name)
137
- command = Discorb::ApplicationCommand::Command.new(
138
- command_name,
139
- guild_ids,
140
- block,
141
- 3,
142
- dm_permission,
143
- default_permission
144
- )
145
- @commands << command
146
- command
147
- end
148
-
149
- #
150
- # Add user context menu command.
151
- #
152
- # @param [String, Hash{Symbol => String}] command_name Command name.
153
- # @param [Array<#to_s>, false, nil] guild_ids
154
- # Guild IDs to set the command to. `false` to global command, `nil` to use default.
155
- # @param [Boolean] dm_permission Whether the command is available in DM.
156
- # @param [Discorb::Permission] default_permission The default permission of the command.
157
- # @param [Proc] block Command block.
158
- # @yield [interaction, user] Block to execute.
159
- # @yieldparam [Discorb::CommandInteraction::UserMenuCommand] interaction Interaction object.
160
- # @yieldparam [Discorb::User] user User object.
161
- #
162
- # @return [Discorb::ApplicationCommand::Command] Command object.
163
- #
164
- def user_command(command_name, guild_ids: nil, dm_permission: true, default_permission: nil, &block)
165
- command_name = { default: command_name } if command_name.is_a?(String)
166
- command_name = ApplicationCommand.modify_localization_hash(command_name)
167
- command = Discorb::ApplicationCommand::Command.new(command_name, guild_ids, block, 2, dm_permission,
168
- default_permission)
169
- @commands << command
170
- command
171
- end
172
-
173
- #
174
- # Setup commands.
175
- # @async
176
- # @see Client#initialize
177
- #
178
- # @param [String] token Bot token.
179
- # @param [Array<#to_s>, false, nil] guild_ids
180
- # Guild IDs to use as default. If `false` is given, it will be global command.
181
- #
182
- # @note `token` parameter only required if you don't run client.
183
- #
184
- def setup_commands(token = nil, guild_ids: nil)
185
- Async do
186
- @token ||= token
187
- @http = HTTP.new(self)
188
- global_commands = @commands.select { |c| c.guild_ids == false or c.guild_ids == [] }
189
- local_commands = @commands.select { |c| c.guild_ids.is_a?(Array) and c.guild_ids.any? }
190
- default_commands = @commands.select { |c| c.guild_ids.nil? }
191
- if guild_ids.is_a?(Array)
192
- default_commands.each do |command|
193
- command.instance_variable_set(:@guild_ids, guild_ids)
194
- end
195
- local_commands += default_commands
196
- else
197
- global_commands += default_commands
198
- end
199
- final_guild_ids = local_commands.map(&:guild_ids).flatten.map(&:to_s).uniq
200
- app_info = fetch_application.wait
201
- unless global_commands.empty?
202
- @http.request(
203
- Route.new(
204
- "/applications/#{app_info.id}/commands",
205
- "//applications/:application_id/commands",
206
- :put
207
- ),
208
- global_commands.map(&:to_hash)
209
- ).wait
210
- end
211
- if ENV.fetch("DISCORB_CLI_FLAG", nil) == "setup"
212
- sputs "Registered commands for global:"
213
- global_commands.each do |command|
214
- iputs "- #{command.name["default"]}"
215
- end
216
- end
217
- unless final_guild_ids.empty?
218
- final_guild_ids.each do |guild_id|
219
- commands = local_commands.select { |c| c.guild_ids.include?(guild_id) }
220
- @http.request(
221
- Route.new("/applications/#{app_info.id}/guilds/#{guild_id}/commands",
222
- "//applications/:application_id/guilds/:guild_id/commands",
223
- :put),
224
- commands.map(&:to_hash)
225
- ).wait
226
- sputs "Registered commands for #{guild_id}:"
227
- commands.each do |command|
228
- iputs "- #{command.name["default"]}"
229
- end
230
- end
231
- end
232
- @logger.info "Successfully setup commands"
233
- end
234
- end
235
-
236
- #
237
- # Claer commands in specified guilds.
238
- # @async
239
- # @see Client#initialize
240
- #
241
- # @param [String] token Bot token.
242
- # @param [Array<#to_s>, false, nil] guild_ids Guild IDs to clear.
243
- #
244
- # @note `token` parameter only required if you don't run client.
245
- #
246
- def clear_commands(token, guild_ids)
247
- Async do
248
- @token ||= token
249
- @http = HTTP.new(self)
250
- app_info = fetch_application.wait
251
-
252
- guild_ids.each do |guild_id|
253
- @http.request(
254
- Route.new("/applications/#{app_info.id}/guilds/#{guild_id}/commands",
255
- "//applications/:application_id/guilds/:guild_id/commands",
256
- :put),
257
- []
258
- ).wait
259
- end
260
- sputs "Cleared commands for #{guild_ids.length} guilds." if ENV.fetch("DISCORB_CLI_FLAG", nil) == "setup"
261
- end
262
- end
263
- end
264
- end
265
- 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