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

Returns The user that initiated the interaction.

Returns:

  • (User)

    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



242
243
244
245
246
247
248
249
250
# File 'lib/discordrb/data/interaction.rb', line 242

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:



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

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.



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

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.



119
120
121
# File 'lib/discordrb/data/interaction.rb', line 119

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.



224
225
226
227
# File 'lib/discordrb/data/interaction.rb', line 224

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.



175
176
177
# File 'lib/discordrb/data/interaction.rb', line 175

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.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/discordrb/data/interaction.rb', line 208

def edit_message(message, content: nil, embeds: nil, allowed_mentions: nil, components: nil)
  builder = Discordrb::Webhooks::Builder.new
  view = Discordrb::Components::View.new

  yield builder, view if block_given?

  components ||= view
  data = builder.to_json_hash.merge({ content: content, embeds: embeds, allowed_mentions: allowed_mentions, componenets: components.to_a }.compact)

  resp = Discordrb::API::Webhook.token_edit_message(
    @token, @application_id, message.resolve_id, data[:content], data[:embeds], data[:allowed_mentions], data[:components]
  )
  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.



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/discordrb/data/interaction.rb', line 161

def edit_response(content: nil, embeds: nil, allowed_mentions: nil, components: nil)
  builder = Discordrb::Webhooks::Builder.new
  view = Discordrb::Components::View.new

  yield(builder, view) if block_given?

  components ||= view
  data = builder.to_json_hash.merge({ content: content, embeds: embeds, allowed_mentions: allowed_mentions }.compact)
  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 (Components::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
# 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::Components::View.new

  yield(builder, view) if block_given?

  components ||= view
  data = builder.to_json_hash.merge({ content: content, embeds: embeds, allowed_mentions: allowed_mentions }.compact)

  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.



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

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::Components::View.new

  yield builder, view if block_given?

  components ||= view
  data = builder.to_json_hash.merge({ content: content, embeds: embeds, allowed_mentions: allowed_mentions, tts: tts, componenets: components.to_a }.compact)

  resp = Discordrb::API::Webhook.token_execute_webhook(
    @token, @application_id, true, data[:content], nil, nil, data[:tts], nil, data[:embeds], data[:allowed_mentions], flags, data[:components]
  )
  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.



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

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 (Components::View)

    A builder for creating interaction components.



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

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::Components::View.new

  yield(builder, view) if block_given?

  components ||= view
  data = builder.to_json_hash.merge({ content: content, embeds: embeds, allowed_mentions: allowed_mentions }.compact)

  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