Class: DiscordStore::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/discord_store.rb

Overview

Ties the layers together: one transport, one rate limiter, and the stores built on top of them.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config: DiscordStore.configuration, http: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • config (Configuration) (defaults to: DiscordStore.configuration)
  • http (#call, nil) (defaults to: nil) —

    injectable HTTP backend; pass a Transport::Fake to run the whole stack without a token



81
82
83
84
# File 'lib/discord_store.rb', line 81

def initialize(config: DiscordStore.configuration, http: nil)
  @config = config.validate!
  @rest = Transport::REST.new(config: @config, http: http)
end

Instance Attribute Details

#config ⇒ Object (readonly)

Returns the value of attribute config.



76
77
78
# File 'lib/discord_store.rb', line 76

def config
  @config
end

#rest ⇒ Object (readonly)

Returns the value of attribute rest.



76
77
78
# File 'lib/discord_store.rb', line 76

def rest
  @rest
end

Instance Method Details

#blobs ⇒ BlobStore

Returns:



92
93
94
# File 'lib/discord_store.rb', line 92

def blobs
  @blobs ||= BlobStore.new(rest: @rest, config: @config)
end

#close ⇒ void

This method returns an undefined value.



137
# File 'lib/discord_store.rb', line 137

def close = @rest.close

#kv ⇒ KV

Returns:



97
98
99
# File 'lib/discord_store.rb', line 97

def kv
  @kv ||= KV.new(rest: @rest, config: @config)
end

#limits ⇒ GuildLimits

Returns:



113
114
115
# File 'lib/discord_store.rb', line 113

def limits
  @limits ||= GuildLimits.new(rest: @rest, config: @config)
end

#log ⇒ Log

Returns:



87
88
89
# File 'lib/discord_store.rb', line 87

def log
  @log ||= Log.new(rest: @rest, config: @config)
end

#search ⇒ Search

Discord's message index.

Not the read path -- see Search for why at length -- and needs the MESSAGE_CONTENT privileged intent. Exists for the queries that a channel scan answers badly, such as finding chunks whose manifest is gone.

Returns:



108
109
110
# File 'lib/discord_store.rb', line 108

def search
  @search ||= Search.new(rest: @rest, config: @config)
end

#verify! ⇒ Hash

Confirms the token works and that the configured application_id matches the token's actual identity. Worth calling at boot: a mismatched application_id silently makes every read return nothing, because the own-messages guard rejects our own writes.

Returns:

  • (Hash) —

    the bot user

Raises:



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/discord_store.rb', line 124

def verify!
  user = @rest.current_user

  if @config.own_messages_only && user["id"].to_s != @config.application_id.to_s
    raise ConfigurationError,
          "configured application_id #{@config.application_id.inspect} is not this token's " \
          "identity (#{user["id"].inspect}). Every read would silently return nothing."
  end

  user
end