Class: OnyxCord::Interaction

Inherits:
Object
  • Object
show all
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

Attributes included from IDObject

#id

Instance Method Summary collapse

Methods included from IDObject

#==, #creation_time, synthesise

Instance Attribute Details

#application_idInteger (readonly)

Returns The ID of the application associated with this interaction.

Returns:

  • (Integer)

    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_permissionsPermissions (readonly)

Returns The permissions the application has where this interaction originates from.

Returns:

  • (Permissions)

    The permissions the application has where this interaction originates from.



97
98
99
# File 'lib/onyxcord/models/interaction.rb', line 97

def application_permissions
  @application_permissions
end

#channelChannel (readonly)

Returns The channel where this interaction originates from.

Returns:

  • (Channel)

    The channel where this interaction originates from.



71
72
73
# File 'lib/onyxcord/models/interaction.rb', line 71

def channel
  @channel
end

#channel_idInteger (readonly)

Returns The ID of the channel this interaction originates from.

Returns:

  • (Integer)

    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

#componentsArray<ActionRow> (readonly)

Returns The modal components associated with this interaction.

Returns:

  • (Array<ActionRow>)

    The modal components associated with this interaction.



94
95
96
# File 'lib/onyxcord/models/interaction.rb', line 94

def components
  @components
end

#contextInteger (readonly)

Returns The context of where this interaction was initiated from.

Returns:

  • (Integer)

    The context of where this interaction was initiated from.



106
107
108
# File 'lib/onyxcord/models/interaction.rb', line 106

def context
  @context
end

#dataHash (readonly)

Returns The interaction data.

Returns:

  • (Hash)

    The interaction data.



88
89
90
# File 'lib/onyxcord/models/interaction.rb', line 88

def data
  @data
end

#max_attachment_sizeInteger (readonly)

Returns The maximum number of bytes an attachment can have when responding to this interaction.

Returns:

  • (Integer)

    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
  @max_attachment_size
end

#messageInteractions::Message? (readonly)

Returns The message associated with this interaction.

Returns:



91
92
93
# File 'lib/onyxcord/models/interaction.rb', line 91

def message
  @message
end

#server_featuresArray<Symbol> (readonly)

Returns The features of the server where this interaction was initiated from.

Returns:

  • (Array<Symbol>)

    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_idInteger? (readonly)

Returns The ID of the server this interaction originates from.

Returns:

  • (Integer, nil)

    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_localeString? (readonly)

Returns The selected language of the server this interaction originates from.

Returns:

  • (String, nil)

    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

#tokenString (readonly)

Returns The interaction token.

Returns:

  • (String)

    The interaction token.



77
78
79
# File 'lib/onyxcord/models/interaction.rb', line 77

def token
  @token
end

#typeInteger (readonly)

Returns The type of this interaction.

Returns:

  • (Integer)

    The type of this interaction.

See Also:



85
86
87
# File 'lib/onyxcord/models/interaction.rb', line 85

def type
  @type
end

#userUser, Member (readonly)

Returns The user that initiated the interaction.

Returns:

  • (User, Member)

    The user that initiated the interaction.



62
63
64
# File 'lib/onyxcord/models/interaction.rb', line 62

def user
  @user
end

#user_localeString (readonly)

Returns The selected language of the user that initiated this interaction.

Returns:

  • (String)

    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

#buttonComponents::Button?

Get the button component that triggered the interaction.

Returns:

  • (Components::Button, nil)

    The button that triggered this interaction if applicable, otherwise nil.



387
388
389
# File 'lib/onyxcord/models/interaction.rb', line 387

def button
  @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.

Parameters:

  • flags (Integer) (defaults to: 0)

    Message flags.

  • ephemeral (true, false) (defaults to: true)

    Whether this message should only be visible to the interaction initiator.



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 suppress_embeds
  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_updateObject 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

Parameters:

  • message (Integer, String, InteractionMessage, Message)

    The message created by this interaction to be deleted.



365
366
367
368
# File 'lib/onyxcord/models/interaction.rb', line 365

def delete_message(message)
  OnyxCord::REST::Webhook.token_delete_message(@token, @application_id, message.resolve_id)
  nil
end

#delete_responseObject 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

Parameters:

  • message (String, Integer, InteractionMessage, Message)

    The message created by this interaction to be edited.

  • content (String) (defaults to: nil)

    The message content.

  • embeds (Array<Hash, Webhooks::Embed>) (defaults to: nil)

    The embeds for the message.

  • allowed_mentions (Hash, AllowedMentions) (defaults to: nil)

    Mentions that can ping on this message.

  • attachments (Array<File>) (defaults to: nil)

    Files that can be referenced in embeds via attachment://file.png.

  • flags (Integer) (defaults to: 0)

    Message flags.

  • has_components (true, false) (defaults to: false)

    Whether this message includes any V2 components. Enabling this disables sending content, polls, and embeds.

  • poll (Hash, Poll::Builder, Poll, nil) (defaults to: nil)

    The poll that should be attached to this message.

Yield Parameters:

  • builder (Webhooks::Builder)

    An optional message builder. Arguments passed to the method overwrite builder data.



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 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 = OnyxCord::Webhooks::Builder.new
  view = OnyxCord::Webhooks::View.new

  prepare_builder(builder, content, embeds, 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_edit_message(
    @token, @application_id, message.resolve_id, edit_content(content, data), edit_embeds(embeds, data), data[:allowed_mentions], components.to_a, attachments, 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.

Parameters:

  • content (String) (defaults to: nil)

    The content of the message.

  • embeds (Array<Hash, Webhooks::Embed>) (defaults to: nil)

    The embeds for the message.

  • allowed_mentions (Hash, AllowedMentions) (defaults to: nil)

    Mentions that can ping on this message.

  • flags (Integer) (defaults to: 0)

    Message flags.

  • components (Array<#to_h>) (defaults to: nil)

    An array of components.

  • attachments (Array<File>) (defaults to: nil)

    Files that can be referenced in embeds and components via attachment://file.png.

  • has_components (true, false) (defaults to: false)

    Whether this message includes any V2 components. Enabling this disables sending content, polls, and embeds.

  • poll (Hash, Poll::Builder, Poll, nil) (defaults to: nil)

    The poll that should be attached to this message.

Yield Parameters:

  • builder (Webhooks::Builder)

    An optional message builder. Arguments passed to the method overwrite builder data.

Returns:

  • (InteractionMessage)

    The updated response message.



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, embeds, 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), edit_embeds(embeds, data), data[:allowed_mentions], components.to_a, attachments, 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.

Parameters:

  • custom_id (String)

    the custom ID of the component to find.

Returns:

  • (TextInput, Button, SelectMenu, Checkbox, ModalActionGroup, nil)

    The component associated with the custom ID, or nil.



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: reply

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.

Parameters:

  • content (String) (defaults to: nil)

    The content of the message.

  • tts (true, false) (defaults to: nil)
  • embeds (Array<Hash, Webhooks::Embed>) (defaults to: nil)

    The embeds for the message.

  • allowed_mentions (Hash, AllowedMentions) (defaults to: nil)

    Mentions that can ping on this message.

  • flags (Integer) (defaults to: 0)

    Message flags.

  • ephemeral (true, false) (defaults to: nil)

    Whether this message should only be visible to the interaction initiator.

  • wait (true, false) (defaults to: false)

    Whether this method should return a Message object of the interaction response.

  • components (Array<#to_h>) (defaults to: nil)

    An array of components.

  • attachments (Array<File>) (defaults to: nil)

    Files that can be referenced in embeds and components via attachment://file.png.

  • has_components (true, false) (defaults to: false)

    Whether this message includes any V2 components. Enabling this disables sending content, polls, and embeds.

  • poll (Hash, Poll::Builder, Poll, nil) (defaults to: nil)

    The poll that should be attached to this message.

Yield Parameters:

  • builder (Webhooks::Builder)

    An optional message builder. Arguments passed to the method overwrite builder data.

  • view (Webhooks::View)

    A builder for creating interaction components.



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 suppress_embeds
  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, embeds, 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, attachments, 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

Parameters:

  • content (String) (defaults to: nil)

    The content of the message.

  • tts (true, false) (defaults to: false)
  • embeds (Array<Hash, Webhooks::Embed>) (defaults to: nil)

    The embeds for the message.

  • allowed_mentions (Hash, AllowedMentions) (defaults to: nil)

    Mentions that can ping on this message.

  • flags (Integer) (defaults to: 0)

    Message flags.

  • ephemeral (true, false) (defaults to: false)

    Whether this message should only be visible to the interaction initiator.

  • attachments (Array<File>) (defaults to: nil)

    Files that can be referenced in embeds and components via attachment://file.png.

  • has_components (true, false) (defaults to: false)

    Whether this message includes any V2 components. Enabling this disables sending content, polls, and embeds.

  • poll (Hash, Poll::Builder, Poll, nil) (defaults to: nil)

    The poll that should be attached to this message.

Yield Parameters:

  • builder (Webhooks::Builder)

    An optional message builder. Arguments passed to the method overwrite builder data.

  • view (Webhooks::View)

    A builder for creating interaction components.



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 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)
  flags |= FLAGS[:ephemeral] if ephemeral
  flags |= FLAGS[:suppress_embeds] if suppress_embeds
  flags |= FLAGS[:suppress_notifications] if suppress_notifications

  builder = OnyxCord::Webhooks::Builder.new
  view = OnyxCord::Webhooks::View.new

  prepare_builder(builder, content, embeds, 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, attachments, data[:poll]
  )
  Interactions::Message.new(OnyxCord::Internal::JSON.parse(resp), @bot, self)
end

#serverServer?

Get the server associated with the interaction.

Returns:

  • (Server, nil)

    This will be nil for interactions that occur in DM channels or servers where the bot does not have the bot scope.



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.

Returns:

  • (true, false)

    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.

Parameters:

  • choices (Array<Hash>, Hash)

    Array of autocomplete choices to show the user.



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.

Parameters:

  • title (String)

    The title of the modal being shown.

  • custom_id (String)

    The custom_id used to identify the modal and store data.

  • components (Array<Component, Hash>, nil) (defaults to: nil)

    An array of components. These can be defined through the block as well.

Yield Parameters:



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_inputsArray<TextInput>

Get the text input components associated with the interaction.

Returns:

  • (Array<TextInput>)

    The text input components associated with this 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.

Parameters:

  • content (String) (defaults to: nil)

    The content of the message.

  • tts (true, false) (defaults to: nil)
  • embeds (Array<Hash, Webhooks::Embed>) (defaults to: nil)

    The embeds for the message.

  • allowed_mentions (Hash, AllowedMentions) (defaults to: nil)

    Mentions that can ping on this message.

  • flags (Integer) (defaults to: 0)

    Message flags.

  • ephemeral (true, false) (defaults to: nil)

    Whether this message should only be visible to the interaction initiator.

  • wait (true, false) (defaults to: false)

    Whether this method should return a Message object of the interaction response.

  • components (Array<#to_h>) (defaults to: nil)

    An array of components.

  • attachments (Array<File>) (defaults to: nil)

    Files that can be referenced in embeds and components via attachment://file.png.

  • has_components (true, false) (defaults to: false)

    Whether this message includes any V2 components. Enabling this disables sending content, polls, and embeds.

  • poll (Hash, Poll::Builder, Poll, nil) (defaults to: nil)

    The poll that should be attached to this message.

Yield Parameters:

  • builder (Webhooks::Builder)

    An optional message builder. Arguments passed to the method overwrite builder data.

  • view (Webhooks::View)

    A builder for creating interaction components.



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 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)
  flags |= FLAGS[:ephemeral] if ephemeral
  flags |= FLAGS[:suppress_embeds] if suppress_embeds
  flags |= FLAGS[:suppress_notifications] if suppress_notifications

  builder = OnyxCord::Webhooks::Builder.new
  view = OnyxCord::Webhooks::View.new

  prepare_builder(builder, content, embeds, 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, attachments, 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.

Returns:

  • (true, false)

    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