From 4551619ecdfacf438ad1dd18b55e1906effaf80a Mon Sep 17 00:00:00 2001 From: Birdie Date: Thu, 18 May 2023 16:43:07 +0300 Subject: [PATCH] Feature: Support attachment option type (#197) * feat: add new/missing Attachment attributes * feat: support attachment option type in interaction --- lib/discordrb/data/attachment.rb | 15 +++++++++++++++ lib/discordrb/data/interaction.rb | 11 ++++++++++- lib/discordrb/events/interactions.rb | 8 ++++++-- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/discordrb/data/attachment.rb b/lib/discordrb/data/attachment.rb index f83ddde97..66c8c4a8d 100644 --- a/lib/discordrb/data/attachment.rb +++ b/lib/discordrb/data/attachment.rb @@ -27,6 +27,16 @@ class Attachment # @return [Integer, nil] the height of an image file, in pixels, or `nil` if the file is not an image. attr_reader :height + # @return [String, nil] the attachment's description. + attr_reader :description + + # @return [String, nil] the attachment's media type. + attr_reader :content_type + + # @return [true, false] whether this attachment is ephemeral. + attr_reader :ephemeral + alias_method :ephemeral?, :ephemeral + # @!visibility private def initialize(data, message, bot) @bot = bot @@ -41,6 +51,11 @@ def initialize(data, message, bot) @width = data['width'] @height = data['height'] + + @description = data['description'] + @content_type = data['content_type'] + + @ephemeral = data['ephemeral'] end # @return [true, false] whether this file is an image file. diff --git a/lib/discordrb/data/interaction.rb b/lib/discordrb/data/interaction.rb index 4f0937599..2e3b16561 100644 --- a/lib/discordrb/data/interaction.rb +++ b/lib/discordrb/data/interaction.rb @@ -400,7 +400,8 @@ class OptionBuilder channel: 7, role: 8, mentionable: 9, - number: 10 + number: 10, + attachment: 11 }.freeze # Channel types that can be provided to #channel @@ -531,6 +532,14 @@ def number(name, description, required: nil, min_value: nil, max_value: nil, cho required: required, min_value: min_value, max_value: max_value, choices: choices) end + # @param name [String, Symbol] The name of the argument. + # @param description [String] A description of the argument. + # @param required [true, false] Whether this option must be provided. + # @return (see #option) + def attachment(name, description, required: nil) + option(TYPES[:attachment], name, description, required: required) + end + # @!visibility private # @param type [Integer] The argument type. # @param name [String, Symbol] The name of the argument. diff --git a/lib/discordrb/events/interactions.rb b/lib/discordrb/events/interactions.rb index ed0a0c9a2..c10a11e9e 100644 --- a/lib/discordrb/events/interactions.rb +++ b/lib/discordrb/events/interactions.rb @@ -130,7 +130,7 @@ def matches?(event) # Event for ApplicationCommand interactions. class ApplicationCommandEvent < InteractionCreateEvent # Struct to allow accessing data via [] or methods. - Resolved = Struct.new('Resolved', :channels, :members, :messages, :roles, :users) # rubocop:disable Lint/StructNewOverride + Resolved = Struct.new('Resolved', :channels, :members, :messages, :roles, :users, :attachments) # rubocop:disable Lint/StructNewOverride # @return [String] The name of the command. attr_reader :command_name @@ -162,7 +162,7 @@ def initialize(data, bot) @command_name = command_data['name'].to_sym @target_id = command_data['target_id']&.to_i - @resolved = Resolved.new({}, {}, {}, {}, {}) + @resolved = Resolved.new({}, {}, {}, {}, {}, {}) process_resolved(command_data['resolved']) if command_data['resolved'] options = command_data['options'] || [] @@ -219,6 +219,10 @@ def process_resolved(resolved_data) resolved_data['messages']&.each do |id, data| @resolved[:messages][id.to_i] = Discordrb::Message.new(data, @bot) end + + resolved_data['attachments']&.each do |id, data| + @resolved[:attachments][id.to_i] = Discordrb::Attachment.new(data, nil, @bot) + end end def transform_options_hash(hash)