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.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/discorb/app_command/handler.rb', line 247 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 sputs "Cleared commands for #{guild_ids.length} guilds." if ENV.fetch("DISCORB_CLI_FLAG", nil) == "setup" 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.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/discorb/app_command/handler.rb', line 135 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.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/discorb/app_command/handler.rb', line 185 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 { |c| c.guild_ids.is_a?(Array) and c.guild_ids.any? } 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 do |command| iputs "- #{command.name["default"]}" end 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 |
# 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.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/discorb/app_command/handler.rb', line 102 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.
165 166 167 168 169 170 171 172 |
# File 'lib/discorb/app_command/handler.rb', line 165 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 |