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
}.freeze
CALLBACK_TYPES =

Interaction response types.

{
  pong: 1,
  channel_message: 4,
  deferred_message: 5,
  deferred_update: 6,
  update_message: 7
}.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.



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

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.



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

def channel_id
  @channel_id
end

#dataHash (readonly)

Returns The interaction data.

Returns:

  • (Hash)

    The interaction data.



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

def data
  @data
end

#idInteger (readonly)

Returns The ID of this interaction.

Returns:

  • (Integer)

    The ID of this interaction.



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

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.



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

def server_id
  @server_id
end

#tokenString (readonly)

Returns The interaction token.

Returns:

  • (String)

    The interaction token.



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

def token
  @token
end

#typeInteger (readonly)

Returns The type of this interaction.

Returns:

  • (Integer)

    The type of this interaction.

See Also:



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

def type
  @type
end

#userUser, Member (readonly)

Returns The user that initiated the interaction.

Returns:

  • (User, Member)

    The user that initiated the interaction.



25
26
27
# File 'lib/discordrb/data/interaction.rb', line 25

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



248
249
250
251
252
253
254
255
256
# File 'lib/discordrb/data/interaction.rb', line 248

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:



243
244
245
# File 'lib/discordrb/data/interaction.rb', line 243

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.



113
114
115
116
117
118
# File 'lib/discordrb/data/interaction.rb', line 113

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.



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

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.



230
231
232
233
# File 'lib/discordrb/data/interaction.rb', line 230

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.



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

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.



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

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.

Yield Parameters:

  • builder (Webhooks::Builder)

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

Returns:

  • (InteractionMessage)

    The updated response message.



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

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

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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/discordrb/data/interaction.rb', line 87

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.



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

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.



237
238
239
# File 'lib/discordrb/data/interaction.rb', line 237

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



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/discordrb/data/interaction.rb', line 138

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