Skip to content

Commit 4551619

Browse files
authored
Feature: Support attachment option type (#197)
* feat: add new/missing Attachment attributes * feat: support attachment option type in interaction
1 parent faace0f commit 4551619

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

lib/discordrb/data/attachment.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ class Attachment
2727
# @return [Integer, nil] the height of an image file, in pixels, or `nil` if the file is not an image.
2828
attr_reader :height
2929

30+
# @return [String, nil] the attachment's description.
31+
attr_reader :description
32+
33+
# @return [String, nil] the attachment's media type.
34+
attr_reader :content_type
35+
36+
# @return [true, false] whether this attachment is ephemeral.
37+
attr_reader :ephemeral
38+
alias_method :ephemeral?, :ephemeral
39+
3040
# @!visibility private
3141
def initialize(data, message, bot)
3242
@bot = bot
@@ -41,6 +51,11 @@ def initialize(data, message, bot)
4151

4252
@width = data['width']
4353
@height = data['height']
54+
55+
@description = data['description']
56+
@content_type = data['content_type']
57+
58+
@ephemeral = data['ephemeral']
4459
end
4560

4661
# @return [true, false] whether this file is an image file.

lib/discordrb/data/interaction.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@ class OptionBuilder
400400
channel: 7,
401401
role: 8,
402402
mentionable: 9,
403-
number: 10
403+
number: 10,
404+
attachment: 11
404405
}.freeze
405406

406407
# 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
531532
required: required, min_value: min_value, max_value: max_value, choices: choices)
532533
end
533534

535+
# @param name [String, Symbol] The name of the argument.
536+
# @param description [String] A description of the argument.
537+
# @param required [true, false] Whether this option must be provided.
538+
# @return (see #option)
539+
def attachment(name, description, required: nil)
540+
option(TYPES[:attachment], name, description, required: required)
541+
end
542+
534543
# @!visibility private
535544
# @param type [Integer] The argument type.
536545
# @param name [String, Symbol] The name of the argument.

lib/discordrb/events/interactions.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def matches?(event)
130130
# Event for ApplicationCommand interactions.
131131
class ApplicationCommandEvent < InteractionCreateEvent
132132
# Struct to allow accessing data via [] or methods.
133-
Resolved = Struct.new('Resolved', :channels, :members, :messages, :roles, :users) # rubocop:disable Lint/StructNewOverride
133+
Resolved = Struct.new('Resolved', :channels, :members, :messages, :roles, :users, :attachments) # rubocop:disable Lint/StructNewOverride
134134

135135
# @return [String] The name of the command.
136136
attr_reader :command_name
@@ -162,7 +162,7 @@ def initialize(data, bot)
162162
@command_name = command_data['name'].to_sym
163163

164164
@target_id = command_data['target_id']&.to_i
165-
@resolved = Resolved.new({}, {}, {}, {}, {})
165+
@resolved = Resolved.new({}, {}, {}, {}, {}, {})
166166
process_resolved(command_data['resolved']) if command_data['resolved']
167167

168168
options = command_data['options'] || []
@@ -219,6 +219,10 @@ def process_resolved(resolved_data)
219219
resolved_data['messages']&.each do |id, data|
220220
@resolved[:messages][id.to_i] = Discordrb::Message.new(data, @bot)
221221
end
222+
223+
resolved_data['attachments']&.each do |id, data|
224+
@resolved[:attachments][id.to_i] = Discordrb::Attachment.new(data, nil, @bot)
225+
end
222226
end
223227

224228
def transform_options_hash(hash)

0 commit comments

Comments
 (0)