Class: DiscordRDA::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/discord_rda/event/base.rb

Overview

Base event class for all Discord events. Events are immutable data objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, data, shard_id: 0) ⇒ Event

Initialize event

Parameters:

  • type (String)

    Event type

  • data (Hash)

    Raw data

  • shard_id (Integer) (defaults to: 0)

    Shard ID



24
25
26
27
28
29
# File 'lib/discord_rda/event/base.rb', line 24

def initialize(type, data, shard_id: 0)
  @type = type.to_s
  @data = data.each_with_object({}) { |(key, value), hash| hash[key.to_s] = value }.freeze
  @shard_id = shard_id
  @timestamp = Time.now.utc.freeze
end

Instance Attribute Details

#dataHash (readonly)

Returns Raw event data.

Returns:

  • (Hash)

    Raw event data



15
16
17
# File 'lib/discord_rda/event/base.rb', line 15

def data
  @data
end

#shard_idInteger (readonly)

Returns Shard ID where event originated.

Returns:

  • (Integer)

    Shard ID where event originated



12
13
14
# File 'lib/discord_rda/event/base.rb', line 12

def shard_id
  @shard_id
end

#timestampTime (readonly)

Returns Event timestamp.

Returns:

  • (Time)

    Event timestamp



18
19
20
# File 'lib/discord_rda/event/base.rb', line 18

def timestamp
  @timestamp
end

#typeString (readonly)

Returns Event type.

Returns:

  • (String)

    Event type



9
10
11
# File 'lib/discord_rda/event/base.rb', line 9

def type
  @type
end

Instance Method Details

#created_atTime?

Get creation time from data if available

Returns:

  • (Time, nil)

    Creation time



33
34
35
36
37
# File 'lib/discord_rda/event/base.rb', line 33

def created_at
  return nil unless @data['id']

  Snowflake.new(@data['id']).timestamp
end

#inspectString

Inspect

Returns:

  • (String)

    Inspect string



52
53
54
# File 'lib/discord_rda/event/base.rb', line 52

def inspect
  "#<#{self.class.name} type=#{@type} shard=#{@shard_id}>"
end

#to_hHash

Convert to hash

Returns:

  • (Hash)

    Event as hash



41
42
43
44
45
46
47
48
# File 'lib/discord_rda/event/base.rb', line 41

def to_h
  {
    type: @type,
    shard_id: @shard_id,
    timestamp: @timestamp.iso8601,
    data: @data
  }
end