Class: Discordrb::Interaction
- Inherits:
-
Object
- Object
- Discordrb::Interaction
- 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
-
#application_id ⇒ Integer
readonly
The ID of the application associated with this interaction.
-
#channel_id ⇒ Integer
readonly
The ID of the channel this interaction originates from.
- #components ⇒ Array<ActionRow> readonly
-
#data ⇒ Hash
readonly
The interaction data.
-
#id ⇒ Integer
readonly
The ID of this interaction.
-
#server_id ⇒ Integer?
readonly
The ID 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.
Instance Method Summary collapse
-
#button ⇒ Hash?
Returns the button that triggered this interaction if applicable, otherwise nil.
- #channel ⇒ Channel?
-
#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) {|builder| ... } ⇒ Object
-
#edit_response(content: nil, embeds: nil, allowed_mentions: nil, components: nil) {|builder| ... } ⇒ InteractionMessage
Edit the original response to this interaction.
- #get_component(custom_id) ⇒ TextInput, ...
-
#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.
- #send_message(content: nil, embeds: nil, tts: false, allowed_mentions: nil, flags: 0, ephemeral: false, components: nil) {|builder| ... } ⇒ Object
-
#server ⇒ Server?
This will be nil for interactions that occur in DM channels or servers where the bot does not have the
bot
scope. -
#show_modal(title:, custom_id:, components: nil) {|A| ... } ⇒ Object
Create a modal as a response.
- #text_inputs ⇒ Array<TextInput>
-
#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.
Instance Attribute Details
#application_id ⇒ Integer (readonly)
Returns 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_id ⇒ Integer (readonly)
Returns 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 |
#components ⇒ Array<ActionRow> (readonly)
58 59 60 |
# File 'lib/discordrb/data/interaction.rb', line 58 def components @components end |
#data ⇒ Hash (readonly)
Returns The interaction data.
55 56 57 |
# File 'lib/discordrb/data/interaction.rb', line 55 def data @data end |
#id ⇒ Integer (readonly)
Returns The ID of this interaction.
38 39 40 |
# File 'lib/discordrb/data/interaction.rb', line 38 def id @id end |
#server_id ⇒ Integer? (readonly)
Returns 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 |
#token ⇒ String (readonly)
Returns The interaction token.
44 45 46 |
# File 'lib/discordrb/data/interaction.rb', line 44 def token @token end |
#type ⇒ Integer (readonly)
Returns The type of this interaction.
52 53 54 |
# File 'lib/discordrb/data/interaction.rb', line 52 def type @type end |
Instance Method Details
#button ⇒ Hash?
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 return unless @type == TYPES[:component] @message['components'].each do |row| Components::ActionRow.new(row, @bot)..each do || return if .custom_id == @data['custom_id'] end end end |
#channel ⇒ Channel?
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.
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_update ⇒ Object
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
256 257 258 259 |
# File 'lib/discordrb/data/interaction.rb', line 256 def () Discordrb::API::Webhook.(@token, @application_id, .resolve_id) nil end |
#delete_response ⇒ Object
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
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/discordrb/data/interaction.rb', line 239 def (, content: nil, embeds: nil, allowed_mentions: nil, components: nil) 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, @application_id, .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.
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, , 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, ...
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.instance_of?(Hash) ? Message.new(@message, @bot) : @message)&.components&.flat_map(&:components) || [] components = top_level.concat() 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.
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, , 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
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 (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, , 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?
Returns 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.
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_inputs ⇒ 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.
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 (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, , 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 |