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
- 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
-
#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.
-
#message ⇒ Interactions::Message?
readonly
The message associated with 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.
60 61 62 |
# File 'lib/discordrb/data/interaction.rb', line 60 def application_id @application_id end |
#application_permissions ⇒ Permissions (readonly)
Returns The permissions the application has where this interaction originates from.
83 84 85 |
# File 'lib/discordrb/data/interaction.rb', line 83 def @application_permissions end |
#channel ⇒ Channel (readonly)
Returns The channel where this interaction originates from.
57 58 59 |
# File 'lib/discordrb/data/interaction.rb', line 57 def channel @channel end |
#channel_id ⇒ Integer (readonly)
Returns 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 |
#components ⇒ Array<ActionRow> (readonly)
Returns The modal components associated with this interaction.
80 81 82 |
# File 'lib/discordrb/data/interaction.rb', line 80 def components @components end |
#context ⇒ Integer (readonly)
Returns The context of where this interaction was initiated from.
92 93 94 |
# File 'lib/discordrb/data/interaction.rb', line 92 def context @context end |
#data ⇒ Hash (readonly)
Returns The interaction data.
74 75 76 |
# File 'lib/discordrb/data/interaction.rb', line 74 def data @data end |
#max_attachment_size ⇒ Integer (readonly)
Returns 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 end |
#message ⇒ Interactions::Message? (readonly)
Returns The message associated with this interaction.
77 78 79 |
# File 'lib/discordrb/data/interaction.rb', line 77 def @message end |
#server_features ⇒ Array<Symbol> (readonly)
Returns 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_id ⇒ Integer? (readonly)
Returns 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_locale ⇒ String? (readonly)
Returns 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 |
#token ⇒ String (readonly)
Returns The interaction token.
63 64 65 |
# File 'lib/discordrb/data/interaction.rb', line 63 def token @token end |
#type ⇒ Integer (readonly)
Returns The type of this interaction.
71 72 73 |
# File 'lib/discordrb/data/interaction.rb', line 71 def type @type end |
#user ⇒ User, Member (readonly)
Returns The user that initiated the interaction.
48 49 50 |
# File 'lib/discordrb/data/interaction.rb', line 48 def user @user end |
#user_locale ⇒ String (readonly)
Returns 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
#button ⇒ Components::Button?
Get the button component that triggered the interaction.
341 342 343 |
# File 'lib/discordrb/data/interaction.rb', line 341 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.
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_update ⇒ Object
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
319 320 321 322 |
# File 'lib/discordrb/data/interaction.rb', line 319 def () Discordrb::API::Webhook.(@token, @application_id, .resolve_id) nil end |
#delete_response ⇒ Object
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
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 (, 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, 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.
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, , 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, self) end |
#get_component(custom_id) ⇒ TextInput, ...
Get a component by its custom ID.
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.
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, , 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, , 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
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 (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, self) end |
#server ⇒ Server?
Get the server associated with the interaction.
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.
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.
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.
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_inputs ⇒ Array<TextInput>
Get the text input components associated with the 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.
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 (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 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, , 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.
362 363 364 |
# File 'lib/discordrb/data/interaction.rb', line 362 def user_integration? @integration_owners[1] == @user.id end |