Class: Discordrb::Interaction
- Inherits:
-
Object
- Object
- Discordrb::Interaction
- Includes:
- IDObject
- Defined in:
- lib/discordrb/data/interaction.rb
Overview
Base class for interaction objects.
Constant Summary collapse
- TYPES =
Interaction types.
{ ping: 1, command: 2, component: 3, autocomplete: 4, modal_submit: 5 }.freeze
- CALLBACK_TYPES =
Interaction response types.
{ pong: 1, channel_message: 4, deferred_message: 5, deferred_update: 6, update_message: 7, autocomplete: 8, modal: 9 }.freeze
Instance Attribute Summary collapse
-
#application_id ⇒ Integer
readonly
The ID of the application associated with this interaction.
-
#application_permissions ⇒ Permissions
readonly
The permissions the application has where this interaction originates from.
-
#channel ⇒ Channel
readonly
The channel where this interaction originates from.
-
#channel_id ⇒ Integer
readonly
The ID of the channel this interaction originates from.
-
#components ⇒ Array<ActionRow>
readonly
The modal components associated with this interaction.
-
#context ⇒ Integer
readonly
The context of where this interaction was initiated from.
-
#data ⇒ Hash
readonly
The interaction data.
-
#max_attachment_size ⇒ Integer
readonly
The maximum number of bytes an attachment can have when responding to this interaction.
-
#server_features ⇒ Array<Symbol>
readonly
The features of the server where the interaction was initiated from.
-
#server_id ⇒ Integer?
readonly
The ID of the server this interaction originates from.
-
#server_locale ⇒ String?
readonly
The selected language of the server this interaction originates from.
-
#token ⇒ String
readonly
The interaction token.
-
#type ⇒ Integer
readonly
The type of this interaction.
-
#user ⇒ User, Member
readonly
The user that initiated the interaction.
-
#user_locale ⇒ String
readonly
The selected language of the user that initiated this interaction.
Attributes included from IDObject
Instance Method Summary collapse
-
#button ⇒ Hash?
Returns the button that triggered this interaction if applicable, otherwise nil.
-
#defer(flags: 0, ephemeral: true) ⇒ Object
Defer an interaction, setting a temporary response that can be later overriden by #send_message.
-
#defer_update ⇒ Object
Defer an update to an interaction.
- #delete_message(message) ⇒ Object
-
#delete_response ⇒ Object
Delete the original interaction response.
- #edit_message(message, content: nil, embeds: nil, allowed_mentions: nil, components: nil, attachments: nil) {|builder| ... } ⇒ Object
-
#edit_response(content: nil, embeds: nil, allowed_mentions: nil, components: nil, attachments: nil) {|builder| ... } ⇒ InteractionMessage
Edit the original response to this interaction.
- #get_component(custom_id) ⇒ TextInput, ...
-
#respond(content: nil, tts: nil, embeds: nil, allowed_mentions: nil, flags: 0, ephemeral: nil, wait: false, components: nil, attachments: nil) {|builder, view| ... } ⇒ Object
Respond to the creation of this interaction.
- #send_message(content: nil, embeds: nil, tts: false, allowed_mentions: nil, flags: 0, ephemeral: false, components: nil, attachments: nil) {|builder| ... } ⇒ Object
-
#server ⇒ Server?
This will be nil for interactions that occur in DM channels or servers where the bot does not have the
botscope. -
#server_integration? ⇒ true, false
Whether the application was installed by the server where this interaction originates from.
-
#show_autocomplete_choices(choices) ⇒ Object
Show autocomplete choices as a response.
-
#show_modal(title:, custom_id:, components: nil) {|A| ... } ⇒ Object
Create a modal as a response.
- #text_inputs ⇒ Array<TextInput>
-
#update_message(content: nil, tts: nil, embeds: nil, allowed_mentions: nil, flags: 0, ephemeral: nil, wait: false, components: nil, attachments: nil) {|builder, view| ... } ⇒ Object
Respond to the creation of this interaction.
-
#user_integration? ⇒ true, false
Whether the application was installed by the user who initiated this interaction.
Methods included from IDObject
#==, #creation_time, synthesise
Instance Attribute Details
#application_id ⇒ Integer (readonly)
Returns The ID of the application associated with this interaction.
45 46 47 |
# File 'lib/discordrb/data/interaction.rb', line 45 def application_id @application_id end |
#application_permissions ⇒ Permissions (readonly)
Returns The permissions the application has where this interaction originates from.
65 66 67 |
# File 'lib/discordrb/data/interaction.rb', line 65 def @application_permissions end |
#channel ⇒ Channel (readonly)
Returns The channel where this interaction originates from.
42 43 44 |
# File 'lib/discordrb/data/interaction.rb', line 42 def channel @channel end |
#channel_id ⇒ Integer (readonly)
Returns The ID of the channel this interaction originates from.
39 40 41 |
# File 'lib/discordrb/data/interaction.rb', line 39 def channel_id @channel_id end |
#components ⇒ Array<ActionRow> (readonly)
Returns The modal components associated with this interaction.
62 63 64 |
# File 'lib/discordrb/data/interaction.rb', line 62 def components @components end |
#context ⇒ Integer (readonly)
Returns The context of where this interaction was initiated from.
74 75 76 |
# File 'lib/discordrb/data/interaction.rb', line 74 def context @context end |
#data ⇒ Hash (readonly)
Returns The interaction data.
59 60 61 |
# File 'lib/discordrb/data/interaction.rb', line 59 def data @data end |
#max_attachment_size ⇒ Integer (readonly)
Returns The maximum number of bytes an attachment can have when responding to this interaction.
77 78 79 |
# File 'lib/discordrb/data/interaction.rb', line 77 def @max_attachment_size end |
#server_features ⇒ Array<Symbol> (readonly)
Returns the features of the server where the interaction was initiated from.
80 81 82 |
# File 'lib/discordrb/data/interaction.rb', line 80 def server_features @server_features end |
#server_id ⇒ Integer? (readonly)
Returns The ID of the server this interaction originates from.
36 37 38 |
# File 'lib/discordrb/data/interaction.rb', line 36 def server_id @server_id end |
#server_locale ⇒ String? (readonly)
Returns The selected language of the server this interaction originates from.
71 72 73 |
# File 'lib/discordrb/data/interaction.rb', line 71 def server_locale @server_locale end |
#token ⇒ String (readonly)
Returns The interaction token.
48 49 50 |
# File 'lib/discordrb/data/interaction.rb', line 48 def token @token end |
#type ⇒ Integer (readonly)
Returns The type of this interaction.
56 57 58 |
# File 'lib/discordrb/data/interaction.rb', line 56 def type @type end |
#user ⇒ User, Member (readonly)
Returns The user that initiated the interaction.
33 34 35 |
# File 'lib/discordrb/data/interaction.rb', line 33 def user @user end |
#user_locale ⇒ String (readonly)
Returns The selected language of the user that initiated this interaction.
68 69 70 |
# File 'lib/discordrb/data/interaction.rb', line 68 def user_locale @user_locale end |
Instance Method Details
#button ⇒ Hash?
Returns the button that triggered this interaction if applicable, otherwise nil
311 312 313 314 315 316 317 318 319 |
# File 'lib/discordrb/data/interaction.rb', line 311 def return unless @type == TYPES[:component] @message['components'].each do |row| Components::ActionRow.new(row, @bot)..each do || return if .custom_id == @data['custom_id'] end end end |
#defer(flags: 0, ephemeral: true) ⇒ Object
Defer an interaction, setting a temporary response that can be later overriden by #send_message. This method is used when you want to use a single message for your response but require additional processing time, or to simply ack an interaction so an error is not displayed.
152 153 154 155 156 157 |
# File 'lib/discordrb/data/interaction.rb', line 152 def defer(flags: 0, ephemeral: true) flags |= 1 << 6 if ephemeral Discordrb::API::Interaction.create_interaction_response(@token, @id, CALLBACK_TYPES[:deferred_message], nil, nil, nil, nil, flags) nil end |
#defer_update ⇒ Object
Defer an update to an interaction. This is can only currently used by Button interactions.
160 161 162 |
# File 'lib/discordrb/data/interaction.rb', line 160 def defer_update Discordrb::API::Interaction.create_interaction_response(@token, @id, CALLBACK_TYPES[:deferred_update]) end |
#delete_message(message) ⇒ Object
291 292 293 294 |
# File 'lib/discordrb/data/interaction.rb', line 291 def () Discordrb::API::Webhook.(@token, @application_id, .resolve_id) nil end |
#delete_response ⇒ Object
Delete the original interaction response.
238 239 240 |
# File 'lib/discordrb/data/interaction.rb', line 238 def delete_response Discordrb::API::Interaction.delete_original_interaction_response(@token, @application_id) end |
#edit_message(message, content: nil, embeds: nil, allowed_mentions: nil, components: nil, attachments: nil) {|builder| ... } ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/discordrb/data/interaction.rb', line 274 def (, content: nil, embeds: nil, allowed_mentions: nil, components: nil, attachments: nil) builder = Discordrb::Webhooks::Builder.new view = Discordrb::Webhooks::View.new prepare_builder(builder, content, , allowed_mentions) yield builder, view if block_given? components ||= view data = builder.to_json_hash resp = Discordrb::API::Webhook.( @token, @application_id, .resolve_id, data[:content], data[:embeds], data[:allowed_mentions], components.to_a, ) Interactions::Message.new(JSON.parse(resp), @bot, @interaction) end |
#edit_response(content: nil, embeds: nil, allowed_mentions: nil, components: nil, attachments: nil) {|builder| ... } ⇒ InteractionMessage
Edit the original response to this interaction.
223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/discordrb/data/interaction.rb', line 223 def edit_response(content: nil, embeds: nil, allowed_mentions: nil, components: nil, attachments: nil) builder = Discordrb::Webhooks::Builder.new view = Discordrb::Webhooks::View.new prepare_builder(builder, content, , allowed_mentions) yield(builder, view) if block_given? components ||= view data = builder.to_json_hash resp = Discordrb::API::Interaction.edit_original_interaction_response(@token, @application_id, data[:content], data[:embeds], data[:allowed_mentions], components.to_a, ) Interactions::Message.new(JSON.parse(resp), @bot, @interaction) end |
#get_component(custom_id) ⇒ TextInput, ...
327 328 329 330 331 332 |
# File 'lib/discordrb/data/interaction.rb', line 327 def get_component(custom_id) top_level = @components.flat_map(&:components) || [] = (@message.instance_of?(Hash) ? Message.new(@message, @bot) : @message)&.components&.flat_map(&:components) || [] components = top_level.concat() components.find { |component| component.custom_id == custom_id } end |
#respond(content: nil, tts: nil, embeds: nil, allowed_mentions: nil, flags: 0, ephemeral: nil, wait: false, components: nil, attachments: nil) {|builder, view| ... } ⇒ Object
Respond to the creation of this interaction. An interaction must be responded to or deferred, The response may be modified with #edit_response or deleted with #delete_response. Further messages can be sent with #send_message.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/discordrb/data/interaction.rb', line 126 def respond(content: nil, tts: nil, embeds: nil, allowed_mentions: nil, flags: 0, ephemeral: nil, wait: false, components: nil, attachments: nil) flags |= 1 << 6 if ephemeral builder = Discordrb::Webhooks::Builder.new view = Discordrb::Webhooks::View.new # Set builder defaults from parameters prepare_builder(builder, content, , allowed_mentions) yield(builder, view) if block_given? components ||= view data = builder.to_json_hash Discordrb::API::Interaction.create_interaction_response(@token, @id, CALLBACK_TYPES[:channel_message], data[:content], tts, data[:embeds], data[:allowed_mentions], flags, components.to_a, ) return unless wait response = Discordrb::API::Interaction.get_original_interaction_response(@token, @application_id) Interactions::Message.new(JSON.parse(response), @bot, @interaction) end |
#send_message(content: nil, embeds: nil, tts: false, allowed_mentions: nil, flags: 0, ephemeral: false, components: nil, attachments: nil) {|builder| ... } ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/discordrb/data/interaction.rb', line 250 def (content: nil, embeds: nil, tts: false, allowed_mentions: nil, flags: 0, ephemeral: false, components: nil, attachments: nil) flags |= 64 if ephemeral builder = Discordrb::Webhooks::Builder.new view = Discordrb::Webhooks::View.new prepare_builder(builder, content, , allowed_mentions) yield builder, view if block_given? components ||= view data = builder.to_json_hash resp = Discordrb::API::Webhook.token_execute_webhook( @token, @application_id, true, data[:content], nil, nil, tts, nil, data[:embeds], data[:allowed_mentions], flags, components.to_a, ) Interactions::Message.new(JSON.parse(resp), @bot, @interaction) end |
#server ⇒ Server?
Returns This will be nil for interactions that occur in DM channels or servers where the bot
does not have the bot scope.
306 307 308 |
# File 'lib/discordrb/data/interaction.rb', line 306 def server @bot.server(@server_id) end |
#server_integration? ⇒ true, false
Returns whether the application was installed by the server where this interaction originates from.
340 341 342 |
# File 'lib/discordrb/data/interaction.rb', line 340 def server_integration? @server_id ? @integration_owners[0] == @server_id : false end |
#show_autocomplete_choices(choices) ⇒ Object
Show autocomplete choices as a response.
298 299 300 301 302 |
# File 'lib/discordrb/data/interaction.rb', line 298 def show_autocomplete_choices(choices) choices = choices.map { |name, value| { name: name, value: value } } unless choices.is_a?(Array) Discordrb::API::Interaction.create_interaction_response(@token, @id, CALLBACK_TYPES[:autocomplete], nil, nil, nil, nil, nil, nil, nil, choices) nil end |
#show_modal(title:, custom_id:, components: nil) {|A| ... } ⇒ Object
Create a modal as a response.
169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/discordrb/data/interaction.rb', line 169 def show_modal(title:, custom_id:, components: nil) if block_given? modal_builder = Discordrb::Webhooks::Modal.new yield modal_builder components = modal_builder.to_a end Discordrb::API::Interaction.create_interaction_modal_response(@token, @id, custom_id, title, components.to_a) unless type == Interaction::TYPES[:modal_submit] nil end |
#text_inputs ⇒ Array<TextInput>
322 323 324 |
# File 'lib/discordrb/data/interaction.rb', line 322 def text_inputs @components&.select { |component| component.is_a? TextInput } | [] end |
#update_message(content: nil, tts: nil, embeds: nil, allowed_mentions: nil, flags: 0, ephemeral: nil, wait: false, components: nil, attachments: nil) {|builder, view| ... } ⇒ Object
Respond to the creation of this interaction. An interaction must be responded to or deferred, The response may be modified with #edit_response or deleted with #delete_response. Further messages can be sent with #send_message.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/discordrb/data/interaction.rb', line 195 def (content: nil, tts: nil, embeds: nil, allowed_mentions: nil, flags: 0, ephemeral: nil, wait: false, components: nil, attachments: nil) flags |= 1 << 6 if ephemeral builder = Discordrb::Webhooks::Builder.new view = Discordrb::Webhooks::View.new prepare_builder(builder, content, , allowed_mentions) yield(builder, view) if block_given? components ||= view data = builder.to_json_hash Discordrb::API::Interaction.create_interaction_response(@token, @id, CALLBACK_TYPES[:update_message], data[:content], tts, data[:embeds], data[:allowed_mentions], flags, components.to_a, ) return unless wait response = Discordrb::API::Interaction.get_original_interaction_response(@token, @application_id) Interactions::Message.new(JSON.parse(response), @bot, @interaction) end |
#user_integration? ⇒ true, false
Returns whether the application was installed by the user who initiated this interaction.
335 336 337 |
# File 'lib/discordrb/data/interaction.rb', line 335 def user_integration? @integration_owners[1] == @user_id end |