Skip to content

Commit

Permalink
Feature: Support attachment option type (#197)
Browse files Browse the repository at this point in the history
* feat: add new/missing Attachment attributes

* feat: support attachment option type in interaction
  • Loading branch information
Birdie0 authored May 18, 2023
1 parent faace0f commit 4551619
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
15 changes: 15 additions & 0 deletions lib/discordrb/data/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion lib/discordrb/data/interaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions lib/discordrb/events/interactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'] || []
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4551619

Please sign in to comment.