Class: Discorb::UnicodeEmoji

Inherits:
Emoji
  • Object
show all
Defined in:
lib/discorb/emoji.rb

Overview

Represents a unicode emoji (default emoji) in discord.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Emoji

#==, #eql?

Constructor Details

#initialize(name, tone: 0) ⇒ UnicodeEmoji

Initialize a new unicode emoji.

Parameters:

  • name (String)

    The name of the emoji.

  • tone (Integer) (defaults to: 0)

    The skin tone of the emoji.



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/discorb/emoji.rb', line 244

def initialize(name, tone: 0)
  if EmojiTable::DISCORD_TO_UNICODE.key?(name)
    @name = name
    @value = EmojiTable::DISCORD_TO_UNICODE[name]
  elsif EmojiTable::UNICODE_TO_DISCORD.key?(name)
    @name = EmojiTable::UNICODE_TO_DISCORD[name][0]
    @value = name
  elsif EmojiTable::SKIN_TONES.any? { |t| name.include?(t) }
    name2 = name.dup
    EmojiTable::SKIN_TONES.each.with_index do |t, i|
      next unless name2.include?(t)

      @skin_tone = i
      name2.sub!(t, "")
      break
    end
    raise ArgumentError, "Invalid skin tone: #{tone}" unless @skin_tone

    @name = EmojiTable::UNICODE_TO_DISCORD[name2].first
    @value = name
  else
    raise ArgumentError, "No such emoji: #{name}"
  end
  if tone.positive?
    unless @value = EmojiTable::DISCORD_TO_UNICODE["#{name}_tone#{tone}"]
      raise ArgumentError, "Invalid skin tone for emoji: #{name}"
    end

    @name = "#{name}_tone#{tone}"
    @skin_tone = tone
  end
end

Instance Attribute Details

#nameString (readonly)

Returns The name of the emoji. (e.g. :grinning:).

Returns:

  • (String)

    The name of the emoji. (e.g. :grinning:)



232
233
234
# File 'lib/discorb/emoji.rb', line 232

def name
  @name
end

#skin_toneInteger (readonly)

Returns The skin tone of the emoji.

Returns:

  • (Integer)

    The skin tone of the emoji.



236
237
238
# File 'lib/discorb/emoji.rb', line 236

def skin_tone
  @skin_tone
end

#valueString (readonly)

Returns The unicode value of the emoji. (e.g. U+1F600).

Returns:

  • (String)

    The unicode value of the emoji. (e.g. U+1F600)



234
235
236
# File 'lib/discorb/emoji.rb', line 234

def value
  @value
end

Instance Method Details

#inspectObject



291
292
293
# File 'lib/discorb/emoji.rb', line 291

def inspect
  "#<#{self.class} :#{@name}:>"
end

#to_sString

Returns The unicode string of the emoji.

Returns:

  • (String)

    The unicode string of the emoji.



278
279
280
# File 'lib/discorb/emoji.rb', line 278

def to_s
  @value
end

#to_uriString

Format the emoji for URI.

Returns:

  • (String)

    the formatted emoji.



287
288
289
# File 'lib/discorb/emoji.rb', line 287

def to_uri
  URI.encode_www_form_component(@value)
end