Module: Discordrb::IDObject

Overview

Mixin for objects that have IDs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idInteger (readonly) Also known as: resolve_id, hash

Returns the ID which uniquely identifies this object across Discord.

Returns:

  • (Integer)

    the ID which uniquely identifies this object across Discord.



7
8
9
# File 'lib/discordrb/id_object.rb', line 7

def id
  @id
end

Class Method Details

.synthesise(time) ⇒ Integer Also known as: synthesize

Creates an artificial snowflake at the given point in time. Useful for comparing against.

Parameters:

  • time (Time)

    The time the snowflake should represent.

Returns:

  • (Integer)

    a snowflake with the timestamp data as the given time



30
31
32
33
# File 'lib/discordrb/id_object.rb', line 30

def self.synthesise(time)
  ms = (time.to_f * 1000).to_i
  (ms - DISCORD_EPOCH) << 22
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

ID based comparison



12
13
14
# File 'lib/discordrb/id_object.rb', line 12

def ==(other)
  Discordrb.id_compare(@id, other)
end

#creation_timeTime

Estimates the time this object was generated on based on the beginning of the ID. This is fairly accurate but shouldn't be relied on as Discord might change its algorithm at any time

Returns:

  • (Time)

    when this object was created at



21
22
23
24
25
# File 'lib/discordrb/id_object.rb', line 21

def creation_time
  # Milliseconds
  ms = (@id >> 22) + DISCORD_EPOCH
  Time.at(ms / 1000.0)
end