Class: OnyxCord::Interaction
- Inherits:
-
Object
- Object
- OnyxCord::Interaction
- Includes:
- IDObject
- Defined in:
- lib/onyxcord/models/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
- CONTEXTS =
Interaction context types.
{ server: 0, bot_dm: 1, private_channel: 2 }.freeze
- INTEGRATION_TYPES =
Application integration types.
{ server: 0, user: 1 }.freeze
- FLAGS =
Message flags for interaction responses.
{ ephemeral: 1 << 6, suppress_embeds: 1 << 2, suppress_notifications: 1 << 12 }.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.
-
#message ⇒ Interactions::Message?
readonly
The message associated with this interaction.
-
#server_features ⇒ Array<Symbol>
readonly
The features of the server where this 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 ⇒ Components::Button?
Get the button component that triggered the interaction.
-
#defer(flags: 0, ephemeral: true, suppress_embeds: nil, suppress_notifications: nil) ⇒ Object
(also: #deferReply)
Defer an interaction, setting a temporary response that can be later overriden by #send_message.
-
#defer_update ⇒ Object
(also: #deferUpdate)
Defer an update to an interaction.
- #delete_message(message) ⇒ Object
-
#delete_response ⇒ Object
(also: #delete_original, #deleteReply)
Delete the original interaction response.
- #edit_message(message, content: nil, embeds: nil, allowed_mentions: nil, components: nil, attachments: nil, flags: 0, has_components: false, components_v2: false, poll: nil) {|builder| ... } ⇒ Object
-
#edit_response(content: nil, embeds: nil, allowed_mentions: nil, flags: 0, components: nil, attachments: nil, has_components: false, components_v2: false, poll: nil) {|builder| ... } ⇒ InteractionMessage
(also: #edit_original, #editReply)
Edit the original response to this interaction.
-
#get_component(custom_id) ⇒ TextInput, ...
Get a component by its custom ID.
-
#respond(content: nil, tts: nil, embeds: nil, allowed_mentions: nil, flags: 0, ephemeral: nil, suppress_embeds: nil, suppress_notifications: nil, wait: false, components: nil, attachments: nil, has_components: false, components_v2: false, poll: nil) {|builder, view| ... } ⇒ Object
(also: #reply)
Respond to the creation of this interaction.
- #send_message(content: nil, embeds: nil, tts: false, allowed_mentions: nil, flags: 0, ephemeral: false, suppress_embeds: nil, suppress_notifications: nil, components: nil, attachments: nil, has_components: false, components_v2: false, poll: nil) {|builder, view| ... } ⇒ Object (also: #followup, #followUp)
-
#server ⇒ Server?
Get the server associated with the interaction.
-
#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
(also: #showModal)
Create a modal as a response.
-
#text_inputs ⇒ Array<TextInput>
Get the text input components associated with the interaction.
-
#update_message(content: nil, tts: nil, embeds: nil, allowed_mentions: nil, flags: 0, ephemeral: nil, suppress_embeds: nil, suppress_notifications: nil, wait: false, components: nil, attachments: nil, has_components: false, components_v2: false, poll: nil) {|builder, view| ... } ⇒ Object
(also: #update)
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.
74 75 76 |
# File 'lib/onyxcord/models/interaction.rb', line 74 def application_id @application_id end |
#application_permissions ⇒ Permissions (readonly)
Returns The permissions the application has where this interaction originates from.
97 98 99 |
# File 'lib/onyxcord/models/interaction.rb', line 97 def @application_permissions end |
#channel ⇒ Channel (readonly)
Returns The channel where this interaction originates from.
71 72 73 |
# File 'lib/onyxcord/models/interaction.rb', line 71 def channel @channel end |
#channel_id ⇒ Integer (readonly)
Returns The ID of the channel this interaction originates from.
68 69 70 |
# File 'lib/onyxcord/models/interaction.rb', line 68 def channel_id @channel_id end |
#components ⇒ Array<ActionRow> (readonly)
Returns The modal components associated with this interaction.
94 95 96 |
# File 'lib/onyxcord/models/interaction.rb', line 94 def components @components end |
#context ⇒ Integer (readonly)
Returns The context of where this interaction was initiated from.
106 107 108 |
# File 'lib/onyxcord/models/interaction.rb', line 106 def context @context end |
#data ⇒ Hash (readonly)
Returns The interaction data.
88 89 90 |
# File 'lib/onyxcord/models/interaction.rb', line 88 def data @data end |
#max_attachment_size ⇒ Integer (readonly)
Returns The maximum number of bytes an attachment can have when responding to this interaction.
109 110 111 |
# File 'lib/onyxcord/models/interaction.rb', line 109 def @max_attachment_size end |
#message ⇒ Interactions::Message? (readonly)
Returns The message associated with this interaction.
91 92 93 |
# File 'lib/onyxcord/models/interaction.rb', line 91 def @message end |
#server_features ⇒ Array<Symbol> (readonly)
Returns The features of the server where this interaction was initiated from.
112 113 114 |
# File 'lib/onyxcord/models/interaction.rb', line 112 def server_features @server_features end |
#server_id ⇒ Integer? (readonly)
Returns The ID of the server this interaction originates from.
65 66 67 |
# File 'lib/onyxcord/models/interaction.rb', line 65 def server_id @server_id end |
#server_locale ⇒ String? (readonly)
Returns The selected language of the server this interaction originates from.
103 104 105 |
# File 'lib/onyxcord/models/interaction.rb', line 103 def server_locale @server_locale end |
#token ⇒ String (readonly)
Returns The interaction token.
77 78 79 |
# File 'lib/onyxcord/models/interaction.rb', line 77 def token @token end |
#type ⇒ Integer (readonly)
Returns The type of this interaction.
85 86 87 |
# File 'lib/onyxcord/models/interaction.rb', line 85 def type @type end |
#user ⇒ User, Member (readonly)
Returns The user that initiated the interaction.
62 63 64 |
# File 'lib/onyxcord/models/interaction.rb', line 62 def user @user end |
#user_locale ⇒ String (readonly)
Returns The selected language of the user that initiated this interaction.
100 101 102 |
# File 'lib/onyxcord/models/interaction.rb', line 100 def user_locale @user_locale end |
Instance Method Details
#button ⇒ Components::Button?
Get the button component that triggered the interaction.
387 388 389 |
# File 'lib/onyxcord/models/interaction.rb', line 387 def @type == TYPES[:component] ? get_component(@data['custom_id']) : nil end |
#defer(flags: 0, ephemeral: true, suppress_embeds: nil, suppress_notifications: nil) ⇒ Object Also known as: deferReply
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.
193 194 195 196 197 198 199 200 |
# File 'lib/onyxcord/models/interaction.rb', line 193 def defer(flags: 0, ephemeral: true, suppress_embeds: nil, suppress_notifications: nil) flags |= FLAGS[:ephemeral] if ephemeral flags |= FLAGS[:suppress_embeds] if flags |= FLAGS[:suppress_notifications] if suppress_notifications OnyxCord::REST::Interaction.create_interaction_response(@token, @id, CALLBACK_TYPES[:deferred_message], nil, nil, nil, nil, flags) nil end |
#defer_update ⇒ Object Also known as: deferUpdate
Defer an update to an interaction. This is can only currently used by Button interactions.
203 204 205 |
# File 'lib/onyxcord/models/interaction.rb', line 203 def defer_update OnyxCord::REST::Interaction.create_interaction_response(@token, @id, CALLBACK_TYPES[:deferred_update]) end |
#delete_message(message) ⇒ Object
365 366 367 368 |
# File 'lib/onyxcord/models/interaction.rb', line 365 def () OnyxCord::REST::Webhook.(@token, @application_id, .resolve_id) nil end |
#delete_response ⇒ Object Also known as: delete_original, deleteReply
Delete the original interaction response.
288 289 290 |
# File 'lib/onyxcord/models/interaction.rb', line 288 def delete_response OnyxCord::REST::Interaction.delete_original_interaction_response(@token, @application_id) end |
#edit_message(message, content: nil, embeds: nil, allowed_mentions: nil, components: nil, attachments: nil, flags: 0, has_components: false, components_v2: false, poll: nil) {|builder| ... } ⇒ Object
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/onyxcord/models/interaction.rb', line 347 def (, content: nil, embeds: nil, allowed_mentions: nil, components: nil, attachments: nil, flags: 0, has_components: false, components_v2: false, poll: nil) builder = OnyxCord::Webhooks::Builder.new view = OnyxCord::Webhooks::View.new prepare_builder(builder, content, , allowed_mentions, poll) yield(builder, view) if block_given? components ||= view flags = OnyxCord::MessageComponents.apply_v2_flag(flags, components, force: has_components || components_v2) data = builder.to_json_hash resp = OnyxCord::REST::Webhook.( @token, @application_id, .resolve_id, edit_content(content, data), (, data), data[:allowed_mentions], components.to_a, , flags, data[:poll] ) Interactions::Message.new(OnyxCord::Internal::JSON.parse(resp), @bot, self) end |
#edit_response(content: nil, embeds: nil, allowed_mentions: nil, flags: 0, components: nil, attachments: nil, has_components: false, components_v2: false, poll: nil) {|builder| ... } ⇒ InteractionMessage Also known as: edit_original, editReply
Edit the original response to this interaction.
272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/onyxcord/models/interaction.rb', line 272 def edit_response(content: nil, embeds: nil, allowed_mentions: nil, flags: 0, components: nil, attachments: nil, has_components: false, components_v2: false, poll: nil) builder = OnyxCord::Webhooks::Builder.new view = OnyxCord::Webhooks::View.new prepare_builder(builder, content, , allowed_mentions, poll) yield(builder, view) if block_given? components ||= view flags = OnyxCord::MessageComponents.apply_v2_flag(flags, components, force: has_components || components_v2) data = builder.to_json_hash resp = OnyxCord::REST::Interaction.edit_original_interaction_response(@token, @application_id, edit_content(content, data), (, data), data[:allowed_mentions], components.to_a, , flags, data[:poll]) Interactions::Message.new(OnyxCord::Internal::JSON.parse(resp), @bot, self) end |
#get_component(custom_id) ⇒ TextInput, ...
Get a component by its custom ID.
402 403 404 405 |
# File 'lib/onyxcord/models/interaction.rb', line 402 def get_component(custom_id) components = flatten_components((@message&.components || []) + @components) components.find { |component| component.respond_to?(:custom_id) && component.custom_id == custom_id } end |
#respond(content: nil, tts: nil, embeds: nil, allowed_mentions: nil, flags: 0, ephemeral: nil, suppress_embeds: nil, suppress_notifications: nil, wait: false, components: nil, attachments: nil, has_components: false, components_v2: false, poll: nil) {|builder, view| ... } ⇒ Object Also known as:
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.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/onyxcord/models/interaction.rb', line 166 def respond(content: nil, tts: nil, embeds: nil, allowed_mentions: nil, flags: 0, ephemeral: nil, suppress_embeds: nil, suppress_notifications: nil, wait: false, components: nil, attachments: nil, has_components: false, components_v2: false, poll: nil) flags |= FLAGS[:ephemeral] if ephemeral flags |= FLAGS[:suppress_embeds] if flags |= FLAGS[:suppress_notifications] if suppress_notifications builder = OnyxCord::Webhooks::Builder.new view = OnyxCord::Webhooks::View.new # Set builder defaults from parameters prepare_builder(builder, content, , allowed_mentions, poll) yield(builder, view) if block_given? components ||= view flags = OnyxCord::MessageComponents.apply_v2_flag(flags, components, force: has_components || components_v2) data = builder.to_json_hash response = OnyxCord::REST::Interaction.create_interaction_response(@token, @id, CALLBACK_TYPES[:channel_message], data[:content], tts, data[:embeds], data[:allowed_mentions], flags, components.to_a, , nil, wait, data[:poll]) return unless wait Interactions::Message.new(OnyxCord::Internal::JSON.parse(response)['resource']['message'], @bot, self) end |
#send_message(content: nil, embeds: nil, tts: false, allowed_mentions: nil, flags: 0, ephemeral: false, suppress_embeds: nil, suppress_notifications: nil, components: nil, attachments: nil, has_components: false, components_v2: false, poll: nil) {|builder, view| ... } ⇒ Object Also known as: followup, followUp
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/onyxcord/models/interaction.rb', line 303 def (content: nil, embeds: nil, tts: false, allowed_mentions: nil, flags: 0, ephemeral: false, suppress_embeds: nil, suppress_notifications: nil, components: nil, attachments: nil, has_components: false, components_v2: false, poll: nil) flags |= FLAGS[:ephemeral] if ephemeral flags |= FLAGS[:suppress_embeds] if flags |= FLAGS[:suppress_notifications] if suppress_notifications builder = OnyxCord::Webhooks::Builder.new view = OnyxCord::Webhooks::View.new prepare_builder(builder, content, , allowed_mentions, poll) yield(builder, view) if block_given? components ||= view flags = OnyxCord::MessageComponents.apply_v2_flag(flags, components, force: has_components || components_v2) data = builder.to_json_hash resp = OnyxCord::REST::Webhook.token_execute_webhook( @token, @application_id, true, data[:content], nil, nil, tts, nil, data[:embeds], data[:allowed_mentions], flags, components.to_a, , data[:poll] ) Interactions::Message.new(OnyxCord::Internal::JSON.parse(resp), @bot, self) end |
#server ⇒ Server?
Get the server associated with the interaction.
381 382 383 |
# File 'lib/onyxcord/models/interaction.rb', line 381 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.
413 414 415 |
# File 'lib/onyxcord/models/interaction.rb', line 413 def server_integration? @server_id ? @integration_owners[0] == @server_id : false end |
#show_autocomplete_choices(choices) ⇒ Object
Show autocomplete choices as a response.
372 373 374 375 376 |
# File 'lib/onyxcord/models/interaction.rb', line 372 def show_autocomplete_choices(choices) choices = choices.map { |name, value| { name: name, value: value } } unless choices.is_a?(Array) OnyxCord::REST::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 Also known as: showModal
Create a modal as a response.
212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/onyxcord/models/interaction.rb', line 212 def show_modal(title:, custom_id:, components: nil) if block_given? modal_builder = OnyxCord::Webhooks::Modal.new yield modal_builder components = modal_builder.to_a end OnyxCord::REST::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>
Get the text input components associated with the interaction.
393 394 395 396 397 |
# File 'lib/onyxcord/models/interaction.rb', line 393 def text_inputs @components.filter_map do |entity| entity.component if entity.is_a?(Components::Label) && entity.component.is_a?(Components::TextInput) end end |
#update_message(content: nil, tts: nil, embeds: nil, allowed_mentions: nil, flags: 0, ephemeral: nil, suppress_embeds: nil, suppress_notifications: nil, wait: false, components: nil, attachments: nil, has_components: false, components_v2: false, poll: nil) {|builder, view| ... } ⇒ Object Also known as: update
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.
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/onyxcord/models/interaction.rb', line 240 def (content: nil, tts: nil, embeds: nil, allowed_mentions: nil, flags: 0, ephemeral: nil, suppress_embeds: nil, suppress_notifications: nil, wait: false, components: nil, attachments: nil, has_components: false, components_v2: false, poll: nil) flags |= FLAGS[:ephemeral] if ephemeral flags |= FLAGS[:suppress_embeds] if flags |= FLAGS[:suppress_notifications] if suppress_notifications builder = OnyxCord::Webhooks::Builder.new view = OnyxCord::Webhooks::View.new prepare_builder(builder, content, , allowed_mentions, poll) yield(builder, view) if block_given? components ||= view flags = OnyxCord::MessageComponents.apply_v2_flag(flags, components, force: has_components || components_v2) data = builder.to_json_hash response = OnyxCord::REST::Interaction.create_interaction_response(@token, @id, CALLBACK_TYPES[:update_message], data[:content], tts, data[:embeds], data[:allowed_mentions], flags, components.to_a, , nil, wait, data[:poll]) return unless wait Interactions::Message.new(OnyxCord::Internal::JSON.parse(response)['resource']['message'], @bot, self) end |
#user_integration? ⇒ true, false
Returns whether the application was installed by the user who initiated this interaction.
408 409 410 |
# File 'lib/onyxcord/models/interaction.rb', line 408 def user_integration? @integration_owners[1] == @user.id end |