A client configuration provider. It accepts configuration from both ENV variables and JSON config file. Should be created before initializing client.

  • Environment variables should be prepended with DISCORD_ all upper-cased.
  • JSON config properties should be snake_cased.
  • You may also specify default values, for which they need to be upper-cased as well.

If multiple sources are specified, the configuration will be processed in the following order:

  1. The default values.
  2. The JSON config file.
  3. The environment variables.

If config is repeated on multiple sources, they'll be overwritten by the latest config source.

The ConfigProvider supports array types, you can supply them through the JSON config as a direct array or through environment variables through comma-separated values.

Comma escaping is not supported yet, so if you specify a type of string[] and insert the value Hi, my name is., the value will correspond to ['Hi', ' my name is.'].

An example environment variable file would be:

DISCORD_TOKEN=MY_TOKEN
DISCORD_MY_VARIABLE=$
DISCORD_MY_NUMS=123,234,345

An example JSON config file would be:

{
"token": "MY_TOKEN",
"my_variable": "$",
"my_nums": [123, 234, 345]
}

An example default object would be:

{
TOKEN: 'MY_TOKEN',
MY_VARIABLE: '$',
MY_NUMS: [1, 2, 3]
}

Getting values from this config would be done by:

const token = client.config.get('TOKEN');
const myVariable = client.config.get('MY_VARIABLE');
const myNums = client.config.get('MY_NUMS');

It is also recommended specifying the types of the config. Check ConfigValidator for more information.

Hierarchy

  • ConfigProvider

Constructors

Properties

config: Record<string, ConfigValue>

The processed config object.

Memberof

ConfigProvider

default?: Record<string, ConfigValue>

The default config values.

Memberof

ConfigProvider

The options for this config provider.

Memberof

ConfigProvider

validator: ConfigValidator

The validator for this config.

Memberof

ConfigProvider

Methods

  • Get the value corresponding to the provided key.

    Type Parameters

    Parameters

    • key: string

      The key of the configuration. Keys are upper-cased.

    Returns undefined | Extract<null, T> | Extract<string, T> | Extract<number, T> | Extract<false, T> | Extract<true, T> | Extract<string[], T> | Extract<boolean[], T> | Extract<number[], T>

    The corresponding value.

  • Process the configuration provided by the specified JSON file.

    Parameters

    • Optional configPath: string

      The path where the JSON configuration file is located.

    Returns void

  • Process the environment variables object for configuration. Keys must begin with DISCORD_ to be added to the configuration provider.

    Parameters

    • Optional env: ProcessEnv | Record<string, ConfigValue>

      The environment variables object.

    Returns void

    Throws

    Throws if it is not possible to cast a value to its given type.

Generated using TypeDoc