Class: Discordrb::Interaction

Inherits:
Object
  • Object
show all
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,
  modal_submit: 5
}.freeze
CALLBACK_TYPES =

Interaction response types.

{
  pong: 1,
  channel_message: 4,
  deferred_message: 5,
  deferred_update: 6,
  update_message: 7,
  modal: 9
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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.



41
42
43
# File 'lib/discordrb/data/interaction.rb', line 41

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



35
36
37
# File 'lib/discordrb/data/interaction.rb', line 35

def channel_id
  @channel_id
end

#componentsArray<ActionRow> (readonly)

Returns:

  • (Array<ActionRow>)


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

def components
  @components
end

#dataHash (readonly)

Returns The interaction data.

Returns:

  • (Hash)

    The interaction data.



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

def data
  @data
end

#idInteger (readonly)

Returns The ID of this interaction.

Returns:

  • (Integer)

    The ID of this interaction.



38
39
40
# File 'lib/discordrb/data/interaction.rb', line 38

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



32
33
34
# File 'lib/discordrb/data/interaction.rb', line 32

def server_id
  @server_id
end

#tokenString (readonly)

Returns The interaction token.

Returns:

  • (String)

    The interaction token.



44
45
46
# File 'lib/discordrb/data/interaction.rb', line 44

def token
  @token
end

#typeInteger (readonly)

Returns The type of this interaction.

Returns:

  • (Integer)

    The type of this interaction.

See Also:



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

def type
  @type
end

#userUser, Member (readonly)

Returns The user that initiated the interaction.

Returns:

  • (User, Member)

    The user that initiated the interaction.



29
30
31
# File 'lib/discordrb/data/interaction.rb', line 29

def user
  @user
end

Instance Method Details

#buttonHash?

Returns the button that triggered this interaction if applicable, otherwise nil

Returns:

  • (Hash, nil)

    Returns the button that triggered this interaction if applicable, otherwise nil



274
275
276
277
278
279
280
281
282
# File 'lib/discordrb/data/interaction.rb', line 274

def button
  return unless @type == TYPES[:component]

  @message['components'].each do |row|
    Components::ActionRow.new(row, @bot).buttons.each do |button|
      return button if button.custom_id == @data['custom_id']
    end
  end
end

#channelChannel?

Returns:

Raises:



269
270
271
# File 'lib/discordrb/data/interaction.rb', line 269

def channel
  @bot.channel(@channel_id)
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.



121
122
123
124
125
126
# File 'lib/discordrb/data/interaction.rb', line 121

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.



129
130
131
# File 'lib/discordrb/data/interaction.rb', line 129

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.



256
257
258
259
# File 'lib/discordrb/data/interaction.rb', line 256

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.



205
206
207
# File 'lib/discordrb/data/interaction.rb', line 205

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) {|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.

Yield Parameters:

  • builder (Webhooks::Builder)

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



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/discordrb/data/interaction.rb', line 239

def edit_message(message, content: nil, embeds: nil, allowed_mentions: nil, components: nil)
  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_edit_message(
    @token, @application_id, message.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) {|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.

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

    An array of components

Yield Parameters:

  • builder (Webhooks::Builder)

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

Returns:

  • (InteractionMessage)

    The updated response message.



190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/discordrb/data/interaction.rb', line 190

def edit_response(content: nil, embeds: nil, allowed_mentions: nil, components: nil)
  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)

  Interactions::Message.new(JSON.parse(resp), @bot, @interaction)
end

#get_component(custom_id) ⇒ TextInput, ...

Returns:

  • (TextInput, Button, SelectMenu)


290
291
292
293
294
295
# File 'lib/discordrb/data/interaction.rb', line 290

def get_component(custom_id)
  top_level = @components.flat_map(&:components) || []
  message_level = (@message.instance_of?(Hash) ? Message.new(@message, @bot) : @message)&.components&.flat_map(&:components) || []
  components = top_level.concat(message_level)
  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) {|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

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.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/discordrb/data/interaction.rb', line 95

def respond(content: nil, tts: nil, embeds: nil, allowed_mentions: nil, flags: 0, ephemeral: nil, wait: false, components: 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, embeds, 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) {|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.

Yield Parameters:

  • builder (Webhooks::Builder)

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



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

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

  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
  )
  Interactions::Message.new(JSON.parse(resp), @bot, @interaction)
end

#serverServer?

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

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.



263
264
265
# File 'lib/discordrb/data/interaction.rb', line 263

def server
  @bot.server(@server_id)
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:



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/discordrb/data/interaction.rb', line 138

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>

Returns:

  • (Array<TextInput>)


285
286
287
# File 'lib/discordrb/data/interaction.rb', line 285

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) {|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

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.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/discordrb/data/interaction.rb', line 163

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

  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

  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