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 }.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
-
#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.
-
#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.
-
#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. -
#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.
37 38 39 |
# File 'lib/discordrb/data/interaction.rb', line 37 def application_id @application_id end |
#channel_id ⇒ Integer (readonly)
Returns 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 |
#data ⇒ Hash (readonly)
Returns The interaction data.
51 52 53 |
# File 'lib/discordrb/data/interaction.rb', line 51 def data @data end |
#id ⇒ Integer (readonly)
Returns The ID of this interaction.
34 35 36 |
# File 'lib/discordrb/data/interaction.rb', line 34 def id @id end |
#server_id ⇒ Integer? (readonly)
Returns 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 |
#token ⇒ String (readonly)
Returns The interaction token.
40 41 42 |
# File 'lib/discordrb/data/interaction.rb', line 40 def token @token end |
#type ⇒ Integer (readonly)
Returns The type of this interaction.
48 49 50 |
# File 'lib/discordrb/data/interaction.rb', line 48 def type @type end |
Instance Method Details
#button ⇒ Hash?
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 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?
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.
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_update ⇒ Object
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
230 231 232 233 |
# File 'lib/discordrb/data/interaction.rb', line 230 def () Discordrb::API::Webhook.(@token, @application_id, .resolve_id) nil end |
#delete_response ⇒ Object
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
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/discordrb/data/interaction.rb', line 213 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.
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, , 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.
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, , 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
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 (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.
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.
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 (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 |