Module: Discorb::ApplicationCommand::Handler
- Included in:
- Client
- Defined in:
- lib/discorb/app_command/handler.rb
Overview
Module to handle application commands.
Instance Method Summary collapse
-
#clear_commands(token, guild_ids) -> Object
Claer commands in specified guilds.
-
#message_command(command_name, guild_ids: nil, dm_permission: true, default_permission: nil, &block) {|interaction, message| ... } ⇒ Discorb::ApplicationCommand::Command
Add message context menu command.
-
#setup_commands(token = nil, guild_ids: nil) -> Object
Setup commands.
-
#slash(command_name, description, options = {}, guild_ids: nil, dm_permission: true, default_permission: nil, &block) -> Discorb::ApplicationCommand::Command::ChatInputCommand
Add new top-level command.
-
#slash_group(command_name, description, guild_ids: nil, dm_permission: true, default_permission: nil) {|group| ... } ⇒ Discorb::ApplicationCommand::Command::GroupCommand
Add new command with group.
-
#user_command(command_name, guild_ids: nil, dm_permission: true, default_permission: nil, &block) {|interaction, user| ... } ⇒ Discorb::ApplicationCommand::Command
Add user context menu command.
Instance Method Details
#clear_commands(token, guild_ids) -> Object
Note:
token
parameter only required if you don't run client.
Claer commands in specified guilds.
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/discorb/app_command/handler.rb', line 281 def clear_commands(token, guild_ids) Async do @token ||= token @http = HTTP.new(self) app_info = fetch_application.wait guild_ids.each do |guild_id| @http.request( Route.new( "/applications/#{app_info.id}/guilds/#{guild_id}/commands", "//applications/:application_id/guilds/:guild_id/commands", :put ), [] ).wait end if ENV.fetch("DISCORB_CLI_FLAG", nil) == "setup" sputs "Cleared commands for #{guild_ids.length} guilds." end end end |
#message_command(command_name, guild_ids: nil, dm_permission: true, default_permission: nil, &block) {|interaction, message| ... } ⇒ Discorb::ApplicationCommand::Command
Add message context menu command.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/discorb/app_command/handler.rb', line 143 def ( command_name, guild_ids: nil, dm_permission: true, default_permission: nil, &block ) command_name = { default: command_name } if command_name.is_a?(String) command_name = ApplicationCommand.modify_localization_hash(command_name) command = Discorb::ApplicationCommand::Command.new( command_name, guild_ids, block, 3, , ) @commands << command command end |
#setup_commands(token = nil, guild_ids: nil) -> Object
Note:
token
parameter only required if you don't run client.
Setup commands.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/discorb/app_command/handler.rb', line 213 def setup_commands(token = nil, guild_ids: nil) Async do @token ||= token @http = HTTP.new(self) global_commands = @commands.select { |c| c.guild_ids == false or c.guild_ids == [] } local_commands = @commands.select do |c| c.guild_ids.is_a?(Array) and c.guild_ids.any? end default_commands = @commands.select { |c| c.guild_ids.nil? } if guild_ids.is_a?(Array) default_commands.each do |command| command.instance_variable_set(:@guild_ids, guild_ids) end local_commands += default_commands else global_commands += default_commands end final_guild_ids = local_commands.map(&:guild_ids).flatten.map(&:to_s).uniq app_info = fetch_application.wait unless global_commands.empty? @http.request( Route.new( "/applications/#{app_info.id}/commands", "//applications/:application_id/commands", :put ), global_commands.map(&:to_hash) ).wait end if ENV.fetch("DISCORB_CLI_FLAG", nil) == "setup" sputs "Registered commands for global:" global_commands.each do |command| iputs "- #{command.name["default"]}" end end unless final_guild_ids.empty? final_guild_ids.each do |guild_id| commands = local_commands.select { |c| c.guild_ids.include?(guild_id) } @http.request( Route.new( "/applications/#{app_info.id}/guilds/#{guild_id}/commands", "//applications/:application_id/guilds/:guild_id/commands", :put ), commands.map(&:to_hash) ).wait sputs "Registered commands for #{guild_id}:" commands.each { |command| iputs "- #{command.name["default"]}" } end end @logger.info "Successfully setup commands" end end |
#slash(command_name, description, options = {}, guild_ids: nil, dm_permission: true, default_permission: nil, &block) -> Discorb::ApplicationCommand::Command::ChatInputCommand
Add new top-level command.
You can use _
instead of -
in language code.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/discorb/app_command/handler.rb', line 55 def slash( command_name, description, = {}, guild_ids: nil, dm_permission: true, default_permission: nil, &block ) command_name = { default: command_name } if command_name.is_a?(String) description = { default: description } if description.is_a?(String) command_name = ApplicationCommand.modify_localization_hash(command_name) description = ApplicationCommand.modify_localization_hash(description) command = Discorb::ApplicationCommand::Command::ChatInputCommand.new( command_name, description, , guild_ids, block, 1, nil, , ) @commands << command @callable_commands << command command end |
#slash_group(command_name, description, guild_ids: nil, dm_permission: true, default_permission: nil) {|group| ... } ⇒ Discorb::ApplicationCommand::Command::GroupCommand
Add new command with group.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/discorb/app_command/handler.rb', line 103 def slash_group( command_name, description, guild_ids: nil, dm_permission: true, default_permission: nil ) command_name = { default: command_name } if command_name.is_a?(String) description = { default: description } if description.is_a?(String) command_name = ApplicationCommand.modify_localization_hash(command_name) description = ApplicationCommand.modify_localization_hash(description) command = Discorb::ApplicationCommand::Command::GroupCommand.new( command_name, description, guild_ids, self, , ) yield command if block_given? @commands << command command end |
#user_command(command_name, guild_ids: nil, dm_permission: true, default_permission: nil, &block) {|interaction, user| ... } ⇒ Discorb::ApplicationCommand::Command
Add user context menu command.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/discorb/app_command/handler.rb', line 180 def user_command( command_name, guild_ids: nil, dm_permission: true, default_permission: nil, &block ) command_name = { default: command_name } if command_name.is_a?(String) command_name = ApplicationCommand.modify_localization_hash(command_name) command = Discorb::ApplicationCommand::Command.new( command_name, guild_ids, block, 2, , ) @commands << command command end |