Class: DiscordRDA::Attachment

Inherits:
Entity
  • Object
show all
Defined in:
lib/discord_rda/entity/attachment.rb

Overview

Represents a Discord message attachment.

Instance Attribute Summary

Attributes inherited from Entity

#id

Instance Method Summary collapse

Methods inherited from Entity

#==, attribute, #created_at, from_hash, #hash, #initialize, #inspect, #to_h, #to_json

Constructor Details

This class inherits a constructor from DiscordRDA::Entity

Instance Method Details

#audio?Boolean

Check if attachment is audio

Returns:

  • (Boolean)

    True if audio



34
35
36
# File 'lib/discord_rda/entity/attachment.rb', line 34

def audio?
  content_type&.start_with?('audio/')
end

#dimensionsArray<Integer>?

Get dimensions as [width, height]

Returns:

  • (Array<Integer>, nil)

    Dimensions



52
53
54
# File 'lib/discord_rda/entity/attachment.rb', line 52

def dimensions
  [width, height] if has_dimensions?
end

#duration_formattedString?

Get duration formatted (for voice messages)

Returns:

  • (String, nil)

    Formatted duration



74
75
76
77
78
79
80
# File 'lib/discord_rda/entity/attachment.rb', line 74

def duration_formatted
  return nil unless duration_secs

  minutes = (duration_secs / 60).floor
  seconds = (duration_secs % 60).floor
  "#{minutes}:#{seconds.to_s.rjust(2, '0')}"
end

#ephemeral?Boolean

Check if attachment is ephemeral (will be deleted)

Returns:

  • (Boolean)

    True if ephemeral



58
59
60
# File 'lib/discord_rda/entity/attachment.rb', line 58

def ephemeral?
  ephemeral
end

#has_dimensions?Boolean

Check if attachment has dimensions (image/video)

Returns:

  • (Boolean)

    True if has dimensions



46
47
48
# File 'lib/discord_rda/entity/attachment.rb', line 46

def has_dimensions?
  !height.nil? && !width.nil?
end

#image?Boolean

Check if attachment is an image

Returns:

  • (Boolean)

    True if image



22
23
24
# File 'lib/discord_rda/entity/attachment.rb', line 22

def image?
  content_type&.start_with?('image/')
end

#size_humanString

Get file size in human readable format

Returns:

  • (String)

    Human readable size



64
65
66
67
68
69
70
# File 'lib/discord_rda/entity/attachment.rb', line 64

def size_human
  bytes = size.to_i
  return "#{bytes}B" if bytes < 1024
  return "#{(bytes / 1024.0).round(1)}KB" if bytes < 1024 * 1024
  return "#{(bytes / (1024.0 * 1024)).round(1)}MB" if bytes < 1024 * 1024 * 1024
  "#{(bytes / (1024.0 * 1024 * 1024)).round(1)}GB"
end

#text?Boolean

Check if attachment is a text file

Returns:

  • (Boolean)

    True if text



40
41
42
# File 'lib/discord_rda/entity/attachment.rb', line 40

def text?
  content_type == 'text/plain' || filename.end_with?('.txt')
end

#video?Boolean

Check if attachment is a video

Returns:

  • (Boolean)

    True if video



28
29
30
# File 'lib/discord_rda/entity/attachment.rb', line 28

def video?
  content_type&.start_with?('video/')
end

#voice_message?Boolean

Check if this is a voice message

Returns:

  • (Boolean)

    True if voice message



84
85
86
# File 'lib/discord_rda/entity/attachment.rb', line 84

def voice_message?
  !waveform.nil? || filename == 'voice-message.ogg'
end