Class: DiscordRDA::Interaction
- Defined in:
- lib/discord_rda/interactions/interaction.rb
Overview
Interaction represents a Discord interaction (slash command, button click, etc.) Provides full response handling including messages, modals, and autocomplete.
Constant Summary collapse
- TYPES =
Interaction types
{ ping: 1, application_command: 2, message_component: 3, application_command_autocomplete: 4, modal_submit: 5, premium_required: 6 }.freeze
- CALLBACK_TYPES =
Interaction callback types (response types)
{ pong: 1, # ACK a Ping channel_message_with_source: 4, # Respond with message deferred_channel_message_with_source: 5, # ACK, edit later deferred_update_message: 6, # For components, ACK and edit later update_message: 7, # For components, edit message application_command_autocomplete_result: 8, # Autocomplete choices modal: 9, # Respond with modal premium_required: 10 # Require premium }.freeze
- CONTEXT_TYPES =
Interaction context types
{ guild: 0, bot_dm: 1, private_channel: 2 }.freeze
Class Attribute Summary collapse
-
.api ⇒ Object
Returns the value of attribute api.
-
.supervisor ⇒ Object
Returns the value of attribute supervisor.
Attributes inherited from Entity
Instance Method Summary collapse
-
#autocomplete(choices) ⇒ void
Respond with autocomplete choices.
-
#autocomplete? ⇒ Boolean
Check if this is an autocomplete interaction.
-
#command? ⇒ Boolean
Check if this is an application command (slash command).
-
#command_data ⇒ Hash?
Get the command data for application command interactions.
-
#command_name ⇒ String?
Get the command name.
-
#component? ⇒ Boolean
Check if this is a message component interaction (button/select).
-
#component_data ⇒ Hash?
Get component data for message component interactions.
-
#component_type ⇒ Integer?
Get component type.
-
#context_type ⇒ Symbol
Get context type as symbol.
-
#custom_id ⇒ String?
Get custom ID from component.
-
#defer(ephemeral: false) ⇒ void
Defer the response (show “thinking…” state).
-
#defer_update ⇒ void
Defer updating the original message (for components).
-
#delete_followup(message_id) ⇒ void
Delete a followup message.
-
#delete_original ⇒ void
Delete the original interaction response.
-
#dm_context? ⇒ Boolean
Check if interaction is from a DM.
-
#edit_followup(message_id, content = nil, **options) {|MessageBuilder| ... } ⇒ Message
Edit a followup message.
-
#edit_original(content = nil, **options) {|MessageBuilder| ... } ⇒ Message
Edit the original interaction response.
-
#focused_option ⇒ Hash?
Get focused option for autocomplete.
-
#followup(content = nil, **options) {|MessageBuilder| ... } ⇒ Message
Create a followup message.
-
#get_followup(message_id) ⇒ Message
Get a followup message.
-
#guild_context? ⇒ Boolean
Check if interaction is from a guild.
-
#interaction_type ⇒ Symbol
Get interaction type as symbol.
-
#member ⇒ Member?
Get the guild member who triggered the interaction.
-
#modal(custom_id:, title:, components: nil) {|ModalBuilder| ... } ⇒ void
Respond with a modal.
-
#modal_data ⇒ Hash?
Get modal submit data.
-
#modal_submit? ⇒ Boolean
Check if this is a modal submit.
-
#modal_value(id) ⇒ String?
Get a modal value by custom_id.
-
#modal_values ⇒ Hash
Get values from modal submission.
-
#option(name) ⇒ Object?
Get a specific option value.
-
#options ⇒ Hash
Get command options as a hash.
-
#original_message ⇒ Message?
Get the original message that triggered this component interaction.
-
#ping? ⇒ Boolean
Check if this is a ping interaction.
-
#premium_required ⇒ void
Show premium required response.
-
#respond(content = nil, **options) {|MessageBuilder| ... } ⇒ void
Respond to the interaction with a message.
- #run_isolated(ruby_code:, timeout_seconds: 15, memory_limit_mb: nil, env: {}) ⇒ Object
-
#selected_values ⇒ Array<String>
Get selected values from select menu.
-
#update_message(content = nil, **options) {|MessageBuilder| ... } ⇒ void
Update the original message (for components).
-
#user ⇒ User
Get the user who triggered the interaction.
Methods inherited from Entity
#==, attribute, #created_at, from_hash, #hash, #initialize, #inspect, #to_h, #to_json
Constructor Details
This class inherits a constructor from DiscordRDA::Entity
Class Attribute Details
.api ⇒ Object
Returns the value of attribute api.
56 57 58 |
# File 'lib/discord_rda/interactions/interaction.rb', line 56 def api @api end |
.supervisor ⇒ Object
Returns the value of attribute supervisor.
57 58 59 |
# File 'lib/discord_rda/interactions/interaction.rb', line 57 def supervisor @supervisor end |
Instance Method Details
#autocomplete(choices) ⇒ void
This method returns an undefined value.
Respond with autocomplete choices
314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/discord_rda/interactions/interaction.rb', line 314 def autocomplete(choices) raise 'API client not configured' unless self.class.api raise 'Only available for autocomplete interactions' unless autocomplete? body = { type: CALLBACK_TYPES[:application_command_autocomplete_result], data: { choices: choices } } self.class.api.post("/interactions/#{id}/#{token}/callback", body: body) end |
#autocomplete? ⇒ Boolean
Check if this is an autocomplete interaction
86 87 88 |
# File 'lib/discord_rda/interactions/interaction.rb', line 86 def autocomplete? type == 4 end |
#command? ⇒ Boolean
Check if this is an application command (slash command)
74 75 76 |
# File 'lib/discord_rda/interactions/interaction.rb', line 74 def command? type == 2 end |
#command_data ⇒ Hash?
Get the command data for application command interactions
135 136 137 138 |
# File 'lib/discord_rda/interactions/interaction.rb', line 135 def command_data return nil unless command? @raw_data['data'] end |
#command_name ⇒ String?
Get the command name
142 143 144 |
# File 'lib/discord_rda/interactions/interaction.rb', line 142 def command_name command_data&.dig('name') end |
#component? ⇒ Boolean
Check if this is a message component interaction (button/select)
80 81 82 |
# File 'lib/discord_rda/interactions/interaction.rb', line 80 def component? type == 3 end |
#component_data ⇒ Hash?
Get component data for message component interactions
174 175 176 177 |
# File 'lib/discord_rda/interactions/interaction.rb', line 174 def component_data return nil unless component? @raw_data['data'] end |
#component_type ⇒ Integer?
Get component type
187 188 189 |
# File 'lib/discord_rda/interactions/interaction.rb', line 187 def component_type component_data&.dig('component_type') end |
#context_type ⇒ Symbol
Get context type as symbol
98 99 100 |
# File 'lib/discord_rda/interactions/interaction.rb', line 98 def context_type CONTEXT_TYPES.key(@raw_data['context']) || :guild end |
#custom_id ⇒ String?
Get custom ID from component
181 182 183 |
# File 'lib/discord_rda/interactions/interaction.rb', line 181 def custom_id component_data&.dig('custom_id') end |
#defer(ephemeral: false) ⇒ void
This method returns an undefined value.
Defer the response (show “thinking…” state)
261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/discord_rda/interactions/interaction.rb', line 261 def defer(ephemeral: false) raise 'API client not configured' unless self.class.api data = ephemeral ? { flags: 64 } : {} body = { type: CALLBACK_TYPES[:deferred_channel_message_with_source], data: data } self.class.api.post("/interactions/#{id}/#{token}/callback", body: body) end |
#defer_update ⇒ void
This method returns an undefined value.
Defer updating the original message (for components)
275 276 277 278 279 280 281 282 283 284 |
# File 'lib/discord_rda/interactions/interaction.rb', line 275 def defer_update raise 'API client not configured' unless self.class.api raise 'Only available for component interactions' unless component? body = { type: CALLBACK_TYPES[:deferred_update_message] } self.class.api.post("/interactions/#{id}/#{token}/callback", body: body) end |
#delete_followup(message_id) ⇒ void
This method returns an undefined value.
Delete a followup message
437 438 439 440 441 |
# File 'lib/discord_rda/interactions/interaction.rb', line 437 def delete_followup() raise 'API client not configured' unless self.class.api self.class.api.delete("/webhooks/#{application_id}/#{token}/messages/#{}") end |
#delete_original ⇒ void
This method returns an undefined value.
Delete the original interaction response
377 378 379 380 381 |
# File 'lib/discord_rda/interactions/interaction.rb', line 377 def delete_original raise 'API client not configured' unless self.class.api self.class.api.delete("/webhooks/#{application_id}/#{token}/messages/@original") end |
#dm_context? ⇒ Boolean
Check if interaction is from a DM
110 111 112 |
# File 'lib/discord_rda/interactions/interaction.rb', line 110 def dm_context? context_type == :bot_dm || context_type == :private_channel end |
#edit_followup(message_id, content = nil, **options) {|MessageBuilder| ... } ⇒ Message
Edit a followup message
419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/discord_rda/interactions/interaction.rb', line 419 def edit_followup(, content = nil, **, &block) raise 'API client not configured' unless self.class.api payload = { content: content }.merge().compact if block builder = MessageBuilder.new(payload) block.call(builder) payload = builder.to_h end data = self.class.api.patch("/webhooks/#{application_id}/#{token}/messages/#{}", body: payload) Message.new(data) end |
#edit_original(content = nil, **options) {|MessageBuilder| ... } ⇒ Message
Edit the original interaction response
360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/discord_rda/interactions/interaction.rb', line 360 def edit_original(content = nil, **, &block) raise 'API client not configured' unless self.class.api payload = { content: content }.merge().compact if block builder = MessageBuilder.new(payload) block.call(builder) payload = builder.to_h end data = self.class.api.patch("/webhooks/#{application_id}/#{token}/messages/@original", body: payload) Message.new(data) end |
#focused_option ⇒ Hash?
Get focused option for autocomplete
167 168 169 170 |
# File 'lib/discord_rda/interactions/interaction.rb', line 167 def focused_option return nil unless autocomplete? command_data&.dig('options')&.find { |opt| opt['focused'] } end |
#followup(content = nil, **options) {|MessageBuilder| ... } ⇒ Message
Create a followup message
388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/discord_rda/interactions/interaction.rb', line 388 def followup(content = nil, **, &block) raise 'API client not configured' unless self.class.api payload = { content: content }.merge().compact if block builder = MessageBuilder.new(payload) block.call(builder) payload = builder.to_h end data = self.class.api.post("/webhooks/#{application_id}/#{token}", body: payload) Message.new(data) end |
#get_followup(message_id) ⇒ Message
Get a followup message
406 407 408 409 410 411 |
# File 'lib/discord_rda/interactions/interaction.rb', line 406 def get_followup() raise 'API client not configured' unless self.class.api data = self.class.api.get("/webhooks/#{application_id}/#{token}/messages/#{}") Message.new(data) end |
#guild_context? ⇒ Boolean
Check if interaction is from a guild
104 105 106 |
# File 'lib/discord_rda/interactions/interaction.rb', line 104 def guild_context? context_type == :guild end |
#interaction_type ⇒ Symbol
Get interaction type as symbol
62 63 64 |
# File 'lib/discord_rda/interactions/interaction.rb', line 62 def interaction_type TYPES.key(type) || :unknown end |
#member ⇒ Member?
Get the guild member who triggered the interaction
128 129 130 131 |
# File 'lib/discord_rda/interactions/interaction.rb', line 128 def member return nil unless @raw_data['member'] Member.new(@raw_data['member'].merge('guild_id' => guild_id.to_s)) end |
#modal(custom_id:, title:, components: nil) {|ModalBuilder| ... } ⇒ void
This method returns an undefined value.
Respond with a modal
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/discord_rda/interactions/interaction.rb', line 332 def modal(custom_id:, title:, components: nil, &block) raise 'API client not configured' unless self.class.api data = { custom_id: custom_id, title: title, components: components || [] } if block builder = ModalBuilder.new(data) block.call(builder) data = builder.to_h end body = { type: CALLBACK_TYPES[:modal], data: data } self.class.api.post("/interactions/#{id}/#{token}/callback", body: body) end |
#modal_data ⇒ Hash?
Get modal submit data
199 200 201 202 |
# File 'lib/discord_rda/interactions/interaction.rb', line 199 def modal_data return nil unless modal_submit? @raw_data['data'] end |
#modal_submit? ⇒ Boolean
Check if this is a modal submit
92 93 94 |
# File 'lib/discord_rda/interactions/interaction.rb', line 92 def modal_submit? type == 5 end |
#modal_value(id) ⇒ String?
Get a modal value by custom_id
223 224 225 |
# File 'lib/discord_rda/interactions/interaction.rb', line 223 def modal_value(id) modal_values[id.to_s] end |
#modal_values ⇒ Hash
Get values from modal submission
206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/discord_rda/interactions/interaction.rb', line 206 def modal_values return {} unless modal_submit? values = {} modal_data&.dig('components')&.each do |row| row['components']&.each do |component| if component['custom_id'] && component['value'] values[component['custom_id']] = component['value'] end end end values end |
#option(name) ⇒ Object?
Get a specific option value
161 162 163 |
# File 'lib/discord_rda/interactions/interaction.rb', line 161 def option(name) [name.to_s] end |
#options ⇒ Hash
Get command options as a hash
148 149 150 151 152 153 154 155 156 |
# File 'lib/discord_rda/interactions/interaction.rb', line 148 def return {} unless command_data && command_data['options'] opts = {} command_data['options'].each do |opt| opts[opt['name']] = resolve_option_value(opt) end opts end |
#original_message ⇒ Message?
Get the original message that triggered this component interaction
229 230 231 232 |
# File 'lib/discord_rda/interactions/interaction.rb', line 229 def return nil unless @raw_data['message'] Message.new(@raw_data['message']) end |
#ping? ⇒ Boolean
Check if this is a ping interaction
68 69 70 |
# File 'lib/discord_rda/interactions/interaction.rb', line 68 def ping? type == 1 end |
#premium_required ⇒ void
This method returns an undefined value.
Show premium required response
456 457 458 459 460 461 462 463 464 |
# File 'lib/discord_rda/interactions/interaction.rb', line 456 def premium_required raise 'API client not configured' unless self.class.api body = { type: CALLBACK_TYPES[:premium_required] } self.class.api.post("/interactions/#{id}/#{token}/callback", body: body) end |
#respond(content = nil, **options) {|MessageBuilder| ... } ⇒ void
This method returns an undefined value.
Respond to the interaction with a message
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/discord_rda/interactions/interaction.rb', line 239 def respond(content = nil, **, &block) raise 'API client not configured' unless self.class.api payload = { content: content }.merge().compact if block builder = MessageBuilder.new(payload) block.call(builder) payload = builder.to_h end body = { type: CALLBACK_TYPES[:channel_message_with_source], data: payload } self.class.api.post("/interactions/#{id}/#{token}/callback", body: body) end |
#run_isolated(ruby_code:, timeout_seconds: 15, memory_limit_mb: nil, env: {}) ⇒ Object
443 444 445 446 447 448 449 450 451 452 |
# File 'lib/discord_rda/interactions/interaction.rb', line 443 def run_isolated(ruby_code:, timeout_seconds: 15, memory_limit_mb: nil, env: {}) raise 'Execution supervisor not configured' unless self.class.supervisor self.class.supervisor.run_isolated( ruby_code: ruby_code, timeout_seconds: timeout_seconds, memory_limit_mb: memory_limit_mb, env: env ) end |
#selected_values ⇒ Array<String>
Get selected values from select menu
193 194 195 |
# File 'lib/discord_rda/interactions/interaction.rb', line 193 def selected_values component_data&.dig('values') || [] end |
#update_message(content = nil, **options) {|MessageBuilder| ... } ⇒ void
This method returns an undefined value.
Update the original message (for components)
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/discord_rda/interactions/interaction.rb', line 291 def (content = nil, **, &block) raise 'API client not configured' unless self.class.api raise 'Only available for component interactions' unless component? payload = { content: content }.merge().compact if block builder = MessageBuilder.new(payload) block.call(builder) payload = builder.to_h end body = { type: CALLBACK_TYPES[:update_message], data: payload } self.class.api.post("/interactions/#{id}/#{token}/callback", body: body) end |
#user ⇒ User
Get the user who triggered the interaction
116 117 118 119 120 121 122 123 124 |
# File 'lib/discord_rda/interactions/interaction.rb', line 116 def user if member && member['user'] User.new(member['user'].merge('member' => member)) elsif @raw_data['user'] User.new(@raw_data['user']) else nil end end |