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.
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/discorb/app_command/handler.rb', line 231 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.
125 126 127 128 129 130 131 132 |
# File 'lib/discorb/app_command/handler.rb', line 125 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.
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 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 |
# File 'lib/discorb/app_command/handler.rb', line 169 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.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/discorb/app_command/handler.rb', line 51 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.
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/discorb/app_command/handler.rb', line 98 def slash_group(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::GroupCommand.new(command_name, description, guild_ids, nil, self, , ) command.then(&block) 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.
149 150 151 152 153 154 155 156 |
# File 'lib/discorb/app_command/handler.rb', line 149 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 |