Module: LibDiscord::Formatter

Defined in:
lib/lib_discord/formatter.rb

Class Method Summary collapse

Class Method Details

.channel(channel_id) ⇒ String

Format a channel ID for use in a message.

Examples:

LibDiscord.formatter.channel(1234) # => "<#1234>"

Parameters:

  • channel_id (#to_s)

    Channel ID

Returns:

  • (String)

    Formatted string



25
# File 'lib/lib_discord/formatter.rb', line 25

def self.channel(channel_id) = "<##{channel_id}>"

.guild_navigation(type) ⇒ String

Format a Guild navigation link for use in a message.

Parameters:

  • type (#to_s)

    Guild Navigation Type

Returns:

  • (String)

    Formatted string

Raises:

  • (RuntimeError)

    if type is not an acceptable guild navigation type.

See Also:



86
87
88
89
90
91
92
93
# File 'lib/lib_discord/formatter.rb', line 86

def self.guild_navigation(type)
  accepted = MAPS.fetch(:GuildNavigationTypes).values
  unless accepted.include?(type)
    raise "Unknown guild navigation type: #{type}"
  end

  "<id:#{type}>"
end

.role(role_id) ⇒ String

Format a role ID for use in a message.

Examples:

LibDiscord.formatter.role(1234) # => "<@&1234>"

Parameters:

  • role_id (#to_s)

    Role ID

Returns:

  • (String)

    Formatted string



35
# File 'lib/lib_discord/formatter.rb', line 35

def self.role(role_id) = "<@&#{role_id}>"

.timestamp(unix_ts, style = nil) ⇒ String

Format a Unix timestamp for use in a message.

Examples:

LibDiscord.formatter.timestamp(0) # => "<t:0>"

Set a style

last_week = Date.today.to_time.to_i - (60 * 60 * 24 * 7)
LibDiscord.formatter.timestamp(last_week, "R") # => "<t:1747972800:R>"

Parameters:

  • unix_ts (#to_i)

    Unix timestamp

  • style (String) (defaults to: nil)

    Single character denoting presentation style.

Returns:

  • (String)

    Formatted string

Raises:

  • (RuntimeError)

    if style is not an acceptable timestamp presentation style.

See Also:



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/lib_discord/formatter.rb', line 64

def self.timestamp(unix_ts, style = nil)
  ts = unix_ts.to_i
  return "<t:#{ts}>" unless style

  accepted = MAPS.fetch(:TimestampStyles).values
  unless accepted.include?(style)
    raise "Unknown timestamp style: #{style}"
  end

  "<t:#{ts}:#{style}>"
end

.user(user_id) ⇒ String

Format a user ID for use in a message.

Examples:

LibDiscord.formatter.user(1234) # => "<@1234>"

Parameters:

  • user_id (#to_s)

    User ID

Returns:

  • (String)

    Formatted string



15
# File 'lib/lib_discord/formatter.rb', line 15

def self.user(user_id) = "<@#{user_id}>"