class Discordrb::Interaction
Base class for interaction objects.
Constants
- CALLBACK_TYPES
Interaction
response types. @see discord.com/developers/docs/interactions/slash-commands#interaction-response-interactioncallbacktype- TYPES
Interaction
types. @see discord.com/developers/docs/interactions/slash-commands#interaction-interactiontype
Attributes
@return [Integer] The ID of the application associated with this interaction.
@return [Integer] The ID of the channel this interaction originates from.
@return [Array<ActionRow>]
@return [Hash] The interaction data.
@return [Integer] The ID of this interaction.
@return [Integer, nil] The ID of the server this interaction originates from.
@return [String] The interaction token.
@return [Integer] The type of this interaction. @see TYPES
@return [User, Member] The user that initiated the interaction.
@!visibility private @return [Integer] Currently pointless
Public Class Methods
@!visibility private
# File lib/discordrb/data/interaction.rb, line 61 def initialize(data, bot) @bot = bot @id = data['id'].to_i @application_id = data['application_id'].to_i @type = data['type'] @message = data['message'] @data = data['data'] @server_id = data['guild_id']&.to_i @channel_id = data['channel_id']&.to_i @user = if data['member'] data['member']['guild_id'] = @server_id Discordrb::Member.new(data['member'], bot.servers[@server_id], bot) else bot.ensure_user(data['user']) end @token = data['token'] @version = data['version'] @components = @data['components']&.map { |component| Components.from_data(component, @bot) }&.compact || [] end
Public Instance Methods
@return [Channel, nil] @raise [Errors::NoPermission] When the bot is not in the server associated with this interaction.
# File lib/discordrb/data/interaction.rb, line 269 def channel @bot.channel(@channel_id) end
Defer an interaction, setting a temporary response that can be later overriden by {Interaction#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. @param flags [Integer] Message
flags. @param ephemeral [true, false] Whether this message should only be visible to the interaction initiator.
# 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 an update to an interaction. This is can only currently used by Button interactions.
# File lib/discordrb/data/interaction.rb, line 129 def defer_update Discordrb::API::Interaction.create_interaction_response(@token, @id, CALLBACK_TYPES[:deferred_update]) end
@param message [Integer, String
, InteractionMessage, Message] The message created by this interaction to be deleted.
# File lib/discordrb/data/interaction.rb, line 256 def delete_message(message) Discordrb::API::Webhook.token_delete_message(@token, @application_id, message.resolve_id) nil end
Delete the original interaction response.
# File lib/discordrb/data/interaction.rb, line 205 def delete_response Discordrb::API::Interaction.delete_original_interaction_response(@token, @application_id) end
@param message [String, Integer
, InteractionMessage, Message] The message created by this interaction to be edited. @param content [String] The message content. @param embeds [Array<Hash, Webhooks::Embed>] The embeds for the message. @param allowed_mentions [Hash, AllowedMentions] Mentions that can ping on this message. @yieldparam builder [Webhooks::Builder] An optional message builder. Arguments passed to the method overwrite builder data.
# File lib/discordrb/data/interaction.rb, line 239 def edit_message(message, content: nil, embeds: nil, allowed_mentions: nil, components: nil) builder = Discordrb::Webhooks::Builder.new view = Discordrb::Webhooks::View.new prepare_builder(builder, content, embeds, allowed_mentions) yield builder, view if block_given? components ||= view data = builder.to_json_hash resp = Discordrb::API::Webhook.token_edit_message( @token, @application_id, message.resolve_id, data[:content], data[:embeds], data[:allowed_mentions], components.to_a ) Interactions::Message.new(JSON.parse(resp), @bot, @interaction) end
Edit the original response to this interaction. @param content [String] The content of the message. @param embeds [Array<Hash, Webhooks::Embed>] The embeds for the message. @param allowed_mentions [Hash, AllowedMentions] Mentions that can ping on this message. @param components [Array<#to_h>] An array of components @return [InteractionMessage] The updated response message. @yieldparam builder [Webhooks::Builder] An optional message builder. Arguments passed to the method overwrite builder data.
# 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, embeds, 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
@return [TextInput, Button, SelectMenu]
# File lib/discordrb/data/interaction.rb, line 290 def get_component(custom_id) top_level = @components.flat_map(&:components) || [] message_level = (@message.instance_of?(Hash) ? Message.new(@message, @bot) : @message)&.components&.flat_map(&:components) || [] components = top_level.concat(message_level) components.find { |component| component.custom_id == custom_id } end
Respond to the creation of this interaction. An interaction must be responded to or deferred, The response may be modified with {Interaction#edit_response} or deleted with {Interaction#delete_response}. Further messages can be sent with {Interaction#send_message}. @param content [String] The content of the message. @param tts [true, false] @param embeds [Array<Hash, Webhooks::Embed>] The embeds for the message. @param allowed_mentions [Hash, AllowedMentions] Mentions that can ping on this message. @param flags [Integer] Message
flags. @param ephemeral [true, false] Whether this message should only be visible to the interaction initiator. @param wait [true, false] Whether this method should return a Message
object of the interaction response. @param components [Array<#to_h>] An array of components @yieldparam builder [Webhooks::Builder] An optional message builder. Arguments passed to the method overwrite builder data. @yieldparam view [Webhooks::View] A builder for creating interaction components.
# 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, embeds, 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
@param content [String] The content of the message. @param tts [true, false] @param embeds [Array<Hash, Webhooks::Embed>] The embeds for the message. @param allowed_mentions [Hash, AllowedMentions] Mentions that can ping on this message. @param flags [Integer] Message
flags. @param ephemeral [true, false] Whether this message should only be visible to the interaction initiator. @yieldparam builder [Webhooks::Builder] An optional message builder. Arguments passed to the method overwrite builder data.
# File lib/discordrb/data/interaction.rb, line 216 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::Webhooks::View.new prepare_builder(builder, content, embeds, 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
@return [Server, nil] This will be nil for interactions that occur in DM channels or servers where the bot
does not have the `bot` scope.
# File lib/discordrb/data/interaction.rb, line 263 def server @bot.server(@server_id) end
Create a modal as a response. @param title [String] The title of the modal being shown. @param custom_id [String] The custom_id used to identify the modal and store data. @param components [Array<Component, Hash>, nil] An array of components. These can be defined through the block as well. @yieldparam [Discordrb::Webhooks::Modal] A builder for the modal’s components.
# 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
@return [Array<TextInput>]
# File lib/discordrb/data/interaction.rb, line 285 def text_inputs @components&.select { |component| component.is_a? TextInput } | [] end
Respond to the creation of this interaction. An interaction must be responded to or deferred, The response may be modified with {Interaction#edit_response} or deleted with {Interaction#delete_response}. Further messages can be sent with {Interaction#send_message}. @param content [String] The content of the message. @param tts [true, false] @param embeds [Array<Hash, Webhooks::Embed>] The embeds for the message. @param allowed_mentions [Hash, AllowedMentions] Mentions that can ping on this message. @param flags [Integer] Message
flags. @param ephemeral [true, false] Whether this message should only be visible to the interaction initiator. @param wait [true, false] Whether this method should return a Message
object of the interaction response. @param components [Array<#to_h>] An array of components @yieldparam builder [Webhooks::Builder] An optional message builder. Arguments passed to the method overwrite builder data. @yieldparam view [Webhooks::View] A builder for creating interaction components.
# File lib/discordrb/data/interaction.rb, line 163 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::Webhooks::View.new prepare_builder(builder, content, embeds, 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
Private Instance Methods
Set builder defaults from parameters @param builder [Discordrb::Webhooks::Builder] @param content [String, nil] @param embeds [Array<Hash, Discordrb::Webhooks::Embed>, nil] @param allowed_mentions [AllowedMentions, Hash, nil]
# File lib/discordrb/data/interaction.rb, line 304 def prepare_builder(builder, content, embeds, allowed_mentions) builder.content = content builder.allowed_mentions = allowed_mentions embeds&.each { |embed| builder << embed } end