Class: Discordrb::Interaction
- Inherits:
-
Object
- Object
- Discordrb::Interaction
- 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
Instance Attribute Summary collapse
-
#application_id ⇒ Integer
readonly
The ID of the application associated with this interaction.
-
#application_permissions ⇒ Permissions
readonly
The permissions the application has where this interaction originates from.
-
#channel ⇒ Channel
readonly
The channel where this interaction originates from.
-
#channel_id ⇒ Integer
readonly
The ID of the channel this interaction originates from.
-
#components ⇒ Array<ActionRow>
readonly
The modal components associated with this interaction.
-
#context ⇒ Integer
readonly
The context of where this interaction was initiated from.
-
#data ⇒ Hash
readonly
The interaction data.
-
#max_attachment_size ⇒ Integer
readonly
The maximum number of bytes an attachment can have when responding to this interaction.
-
#server_features ⇒ Array<Symbol>
readonly
The features of the server where this interaction was initiated from.
-
#server_id ⇒ Integer?
readonly
The ID of the server this interaction originates from.
-
#server_locale ⇒ String?
readonly
The selected language of the server this interaction originates from.
-
#token ⇒ String
readonly
The interaction token.
-
#type ⇒ Integer
readonly
The type of this interaction.
-
#user ⇒ User, Member
readonly
The user that initiated the interaction.
-
#user_locale ⇒ String
readonly
The selected language of the user that initiated this interaction.
Attributes included from IDObject
Instance Method Summary collapse
-
#button ⇒ Components::Button?
Get the button component that triggered the interaction.
-
#defer(flags: 0, ephemeral: true) ⇒ Object
Defer an interaction, setting a temporary response that can be later overriden by #send_message.
-
#defer_update ⇒ Object
Defer an update to an interaction.
- #delete_message(message) ⇒ Object
-
#delete_response ⇒ Object
Delete the original interaction response.
- #edit_message(message, content: nil, embeds: nil, allowed_mentions: nil, components: nil, attachments: nil, flags: 0, has_components: false) {|builder| ... } ⇒ Object
-
#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.
-
#get_component(custom_id) ⇒ TextInput, ...
Get a component by its custom ID.
-
#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.
- #send_message(content: nil, embeds: nil, tts: false, allowed_mentions: nil, flags: 0, ephemeral: false, components: nil, attachments: nil, has_components: false) {|builder| ... } ⇒ Object
-
#server ⇒ Server?
Get the server associated with the interaction.
-
#server_integration? ⇒ true, false
Whether the application was installed by the server where this interaction originates from.
-
#show_autocomplete_choices(choices) ⇒ Object
Show autocomplete choices as a response.
-
#show_modal(title:, custom_id:, components: nil) {|A| ... } ⇒ Object
Create a modal as a response.
-
#text_inputs ⇒ Array<TextInput>
Get the text input components associated with the interaction.
-
#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.
-
#user_integration? ⇒ true, false
Whether the application was installed by the user who initiated this interaction.
Methods included from IDObject
#==, #creation_time, synthesise
Instance Attribute Details
#application_id ⇒ Integer (readonly)
Returns The ID of the application associated with this interaction.
45 46 47 |
# File 'lib/discordrb/data/interaction.rb', line 45 def application_id @application_id end |
#application_permissions ⇒ Permissions (readonly)
Returns The permissions the application has where this interaction originates from.
65 66 67 |
# File 'lib/discordrb/data/interaction.rb', line 65 def @application_permissions end |
#channel ⇒ Channel (readonly)
Returns The channel where this interaction originates from.
42 43 44 |
# File 'lib/discordrb/data/interaction.rb', line 42 def channel @channel end |
#channel_id ⇒ Integer (readonly)
Returns The ID of the channel this interaction originates from.
39 40 41 |
# File 'lib/discordrb/data/interaction.rb', line 39 def channel_id @channel_id end |
#components ⇒ Array<ActionRow> (readonly)
Returns The modal components associated with this interaction.
62 63 64 |
# File 'lib/discordrb/data/interaction.rb', line 62 def components @components end |
#context ⇒ Integer (readonly)
Returns The context of where this interaction was initiated from.
74 75 76 |
# File 'lib/discordrb/data/interaction.rb', line 74 def context @context end |
#data ⇒ Hash (readonly)
Returns The interaction data.
59 60 61 |
# File 'lib/discordrb/data/interaction.rb', line 59 def data @data end |
#max_attachment_size ⇒ Integer (readonly)
Returns The maximum number of bytes an attachment can have when responding to this interaction.
77 78 79 |
# File 'lib/discordrb/data/interaction.rb', line 77 def @max_attachment_size end |
#server_features ⇒ Array<Symbol> (readonly)
Returns The features of the server where this interaction was initiated from.
80 81 82 |
# File 'lib/discordrb/data/interaction.rb', line 80 def server_features @server_features end |
#server_id ⇒ Integer? (readonly)
Returns The ID of the server this interaction originates from.
36 37 38 |
# File 'lib/discordrb/data/interaction.rb', line 36 def server_id @server_id end |
#server_locale ⇒ String? (readonly)
Returns The selected language of the server this interaction originates from.
71 72 73 |
# File 'lib/discordrb/data/interaction.rb', line 71 def server_locale @server_locale end |
#token ⇒ String (readonly)
Returns The interaction token.
48 49 50 |
# File 'lib/discordrb/data/interaction.rb', line 48 def token @token end |
#type ⇒ Integer (readonly)
Returns The type of this interaction.
56 57 58 |
# File 'lib/discordrb/data/interaction.rb', line 56 def type @type end |
#user ⇒ User, Member (readonly)
Returns The user that initiated the interaction.
33 34 35 |
# File 'lib/discordrb/data/interaction.rb', line 33 def user @user end |
#user_locale ⇒ String (readonly)
Returns The selected language of the user that initiated this interaction.
68 69 70 |
# File 'lib/discordrb/data/interaction.rb', line 68 def user_locale @user_locale end |
Instance Method Details
#button ⇒ Components::Button?
Get the button component that triggered the interaction.
327 328 329 |
# File 'lib/discordrb/data/interaction.rb', line 327 def @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.
154 155 156 157 158 159 |
# File 'lib/discordrb/data/interaction.rb', line 154 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_update ⇒ Object
Defer an update to an interaction. This is can only currently used by Button interactions.
162 163 164 |
# File 'lib/discordrb/data/interaction.rb', line 162 def defer_update Discordrb::API::Interaction.create_interaction_response(@token, @id, CALLBACK_TYPES[:deferred_update]) end |
#delete_message(message) ⇒ Object
305 306 307 308 |
# File 'lib/discordrb/data/interaction.rb', line 305 def () Discordrb::API::Webhook.(@token, @application_id, .resolve_id) nil end |
#delete_response ⇒ Object
Delete the original interaction response.
246 247 248 |
# File 'lib/discordrb/data/interaction.rb', line 246 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
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/discordrb/data/interaction.rb', line 286 def (, 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, , allowed_mentions) yield builder, view if block_given? components ||= view data = builder.to_json_hash resp = Discordrb::API::Webhook.( @token, @application_id, .resolve_id, data[:content], data[:embeds], data[:allowed_mentions], components.to_a, , flags ) Interactions::Message.new(JSON.parse(resp), @bot, @interaction) 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.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/discordrb/data/interaction.rb', line 229 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, , 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, , flags) Interactions::Message.new(JSON.parse(resp), @bot, @interaction) end |
#get_component(custom_id) ⇒ TextInput, ...
Get a component by its custom ID.
342 343 344 345 |
# File 'lib/discordrb/data/interaction.rb', line 342 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.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/discordrb/data/interaction.rb', line 127 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, , 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, attachments: nil, has_components: false) {|builder| ... } ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/discordrb/data/interaction.rb', line 259 def (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, , 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 |
#server ⇒ Server?
Get the server associated with the interaction.
321 322 323 |
# File 'lib/discordrb/data/interaction.rb', line 321 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.
353 354 355 |
# File 'lib/discordrb/data/interaction.rb', line 353 def server_integration? @server_id ? @integration_owners[0] == @server_id : false end |
#show_autocomplete_choices(choices) ⇒ Object
Show autocomplete choices as a response.
312 313 314 315 316 |
# File 'lib/discordrb/data/interaction.rb', line 312 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.
171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/discordrb/data/interaction.rb', line 171 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_inputs ⇒ Array<TextInput>
Get the text input components associated with the interaction.
333 334 335 336 337 |
# File 'lib/discordrb/data/interaction.rb', line 333 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.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/discordrb/data/interaction.rb', line 198 def (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, , 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 |
#user_integration? ⇒ true, false
Returns whether the application was installed by the user who initiated this interaction.
348 349 350 |
# File 'lib/discordrb/data/interaction.rb', line 348 def user_integration? @integration_owners[1] == @user.id end |