Class: Discordrb::Interaction

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

Interaction context types.

{
  server: 0,
  bot_dm: 1,
  private_channel: 2
}.freeze
INTEGRATION_TYPES =

Application integration types.

{
  server: 0,
  user: 1
}.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.



60
61
62
# File 'lib/discordrb/data/interaction.rb', line 60

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.



83
84
85
# File 'lib/discordrb/data/interaction.rb', line 83

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.



57
58
59
# File 'lib/discordrb/data/interaction.rb', line 57

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.



54
55
56
# File 'lib/discordrb/data/interaction.rb', line 54

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.



80
81
82
# File 'lib/discordrb/data/interaction.rb', line 80

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.



92
93
94
# File 'lib/discordrb/data/interaction.rb', line 92

def context
  @context
end

#dataHash (readonly)

Returns The interaction data.

Returns:

  • (Hash)

    The interaction data.



74
75
76
# File 'lib/discordrb/data/interaction.rb', line 74

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.



95
96
97
# File 'lib/discordrb/data/interaction.rb', line 95

def max_attachment_size
  @max_attachment_size
end

#messageInteractions::Message? (readonly)

Returns The message associated with this interaction.

Returns:



77
78
79
# File 'lib/discordrb/data/interaction.rb', line 77

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.



98
99
100
# File 'lib/discordrb/data/interaction.rb', line 98

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.



51
52
53
# File 'lib/discordrb/data/interaction.rb', line 51

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.



89
90
91
# File 'lib/discordrb/data/interaction.rb', line 89

def server_locale
  @server_locale
end

#tokenString (readonly)

Returns The interaction token.

Returns:

  • (String)

    The interaction token.



63
64
65
# File 'lib/discordrb/data/interaction.rb', line 63

def token
  @token
end

#typeInteger (readonly)

Returns The type of this interaction.

Returns:

  • (Integer)

    The type of this interaction.

See Also:



71
72
73
# File 'lib/discordrb/data/interaction.rb', line 71

def type
  @type
end

#userUser, Member (readonly)

Returns The user that initiated the interaction.

Returns:

  • (User, Member)

    The user that initiated the interaction.



48
49
50
# File 'lib/discordrb/data/interaction.rb', line 48

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.



86
87
88
# File 'lib/discordrb/data/interaction.rb', line 86

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.



341
342
343
# File 'lib/discordrb/data/interaction.rb', line 341

def button
  @type == TYPES[:component] ? get_component(@data['custom_id']) : nil
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.

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.



170
171
172
173
174
175
# File 'lib/discordrb/data/interaction.rb', line 170

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_updateObject

Defer an update to an interaction. This is can only currently used by Button interactions.



178
179
180
# File 'lib/discordrb/data/interaction.rb', line 178

def defer_update
  Discordrb::API::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.



319
320
321
322
# File 'lib/discordrb/data/interaction.rb', line 319

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

#delete_responseObject

Delete the original interaction response.



260
261
262
# File 'lib/discordrb/data/interaction.rb', line 260

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, flags: 0, has_components: false) {|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 content and embeds.

Yield Parameters:

  • builder (Webhooks::Builder)

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



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/discordrb/data/interaction.rb', line 300

def edit_message(message, content: nil, embeds: nil, allowed_mentions: nil, components: nil, attachments: nil, flags: 0, has_components: false)
  builder = Discordrb::Webhooks::Builder.new
  view = Discordrb::Webhooks::View.new

  flags |= (1 << 15) if has_components

  prepare_builder(builder, content, embeds, allowed_mentions)
  yield builder, view if block_given?

  components ||= view
  data = builder.to_json_hash

  resp = Discordrb::API::Webhook.token_edit_message(
    @token, @application_id, message.resolve_id, data[:content], data[:embeds], data[:allowed_mentions], components.to_a, attachments, flags
  )
  Interactions::Message.new(JSON.parse(resp), @bot, self)
end

#edit_response(content: nil, embeds: nil, allowed_mentions: nil, flags: 0, components: nil, attachments: nil, has_components: false) {|builder| ... } ⇒ InteractionMessage

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 content and embeds.

Yield Parameters:

  • builder (Webhooks::Builder)

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

Returns:

  • (InteractionMessage)

    The updated response message.



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/discordrb/data/interaction.rb', line 243

def edit_response(content: nil, embeds: nil, allowed_mentions: nil, flags: 0, components: nil, attachments: nil, has_components: false)
  flags |= (1 << 15) if has_components

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

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

  Interactions::Message.new(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.



356
357
358
359
# File 'lib/discordrb/data/interaction.rb', line 356

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, wait: false, components: nil, attachments: nil, has_components: false) {|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.

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 content and embeds.

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.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/discordrb/data/interaction.rb', line 145

def respond(content: nil, tts: nil, embeds: nil, allowed_mentions: nil, flags: 0, ephemeral: nil, wait: false, components: nil, attachments: nil, has_components: false)
  flags |= 1 << 6 if ephemeral
  flags |= (1 << 15) if has_components

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

  # Set builder defaults from parameters
  prepare_builder(builder, content, embeds, allowed_mentions)
  yield(builder, view) if block_given?

  components ||= view
  data = builder.to_json_hash

  response = Discordrb::API::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)
  return unless wait

  Interactions::Message.new(JSON.parse(response)['resource']['message'], @bot, self)
end

#send_message(content: nil, embeds: nil, tts: false, allowed_mentions: nil, flags: 0, ephemeral: false, components: nil, attachments: nil, has_components: false) {|builder| ... } ⇒ Object

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 content and embeds.

Yield Parameters:

  • builder (Webhooks::Builder)

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



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/discordrb/data/interaction.rb', line 273

def send_message(content: nil, embeds: nil, tts: false, allowed_mentions: nil, flags: 0, ephemeral: false, components: nil, attachments: nil, has_components: false)
  flags |= 64 if ephemeral
  flags |= (1 << 15) if has_components

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

  prepare_builder(builder, content, embeds, 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, attachments
  )
  Interactions::Message.new(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.



335
336
337
# File 'lib/discordrb/data/interaction.rb', line 335

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.



367
368
369
# File 'lib/discordrb/data/interaction.rb', line 367

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.



326
327
328
329
330
# File 'lib/discordrb/data/interaction.rb', line 326

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.

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:



187
188
189
190
191
192
193
194
195
196
197
# File 'lib/discordrb/data/interaction.rb', line 187

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

Get the text input components associated with the interaction.

Returns:

  • (Array<TextInput>)

    The text input components associated with this interaction.



347
348
349
350
351
# File 'lib/discordrb/data/interaction.rb', line 347

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, wait: false, components: nil, attachments: nil, has_components: false) {|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.

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 content and embeds.

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.



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/discordrb/data/interaction.rb', line 214

def update_message(content: nil, tts: nil, embeds: nil, allowed_mentions: nil, flags: 0, ephemeral: nil, wait: false, components: nil, attachments: nil, has_components: false)
  flags |= 1 << 6 if ephemeral
  flags |= (1 << 15) if has_components

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

  prepare_builder(builder, content, embeds, allowed_mentions)
  yield(builder, view) if block_given?

  components ||= view
  data = builder.to_json_hash

  response = Discordrb::API::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)
  return unless wait

  Interactions::Message.new(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.



362
363
364
# File 'lib/discordrb/data/interaction.rb', line 362

def user_integration?
  @integration_owners[1] == @user.id
end