Skip to content
Classcustom-idv0.2.7

CustomId

A typed customId. The single source of truth shared by the component that mints it and the handler that reads it. This gives you typed reads on the .customId field in components. Values are packed into a compact wire string, which fits more of them inside Discord's 100-char limit.

class CustomId<
    Prefix extends string,
    Shape extends CustomIdShape = {}
>

Member visibility

Filter class members by access level.

constructor

Constructs a new instance of the CustomId class

CustomId(prefix: Prefix, shape?: Shape)

Showing members with Protected visibility and higher.

Prefix

The stable route prefix, e.g. 'approve'.

Prefix extends string

Shape

The accumulated fields, filled in by the chain.

Shape extends CustomIdShape = {}

prefix

The stable route prefix, e.g. 'approve'.

public readonly prefix: Prefix

routeKey

The prefix plus a short hash of the shape, the part of the wire before the colon.

public readonly routeKey: string

shape

The field definitions accumulated by the chain, keyed by name.

public readonly shape: Shape

decode()

Read a wire string back into values.

Throws two different errors. A wire minted before the shape changed throws the stale one. A corrupt wire, or one minted by a different definition, throws the invalid one. Use setCustomIdErrors to customize the two thrown Error classes.

public decode(wire: string): DecodedParams<Shape>
Parameter: wire

The customId string from the interaction.

encode()

Mint a wire string from values. Throws if a value is out of its field's range or the wire is over 100 chars.

public encode(values: DecodedParams<Shape>): string
Parameter: values

One value per field, typed by the chain.

int()

Add an integer field with no bounds, for a value up to 2^53.

public int<Name, Nullable>(
    name: Name,
    opts?: FieldOptions<Nullable>
): CustomId<
    Prefix,
    Shape &
        Record<
            Name,
            CustomIdField<Nullish<number, Nullable>>
        >
>

oneOf()

Add a field that is one value from a fixed list, decoded as the literal union. No as const needed.

public oneOf<Name, Choices, Nullable>(
    name: Name,
    choices: Choices,
    opts?: FieldOptions<Nullable>
): CustomId<
    Prefix,
    Shape &
        Record<
            Name,
            CustomIdField<
                Nullish<Choices[number], Nullable>
            >
        >
>

owns()

True if this wire was minted from this customId's prefix, ignoring the shape hash.

Use it to pick which definition a click belongs to before decoding. decodeFor runs this loop for you across several definitions.

public owns(wire: string): boolean
Parameter: wire

The customId string from the interaction.

snowflake()

Add a Discord ID field, decoded as a string (the Snowflake type from discord-api-types).

public snowflake<Name, Nullable>(
    name: Name,
    opts?: FieldOptions<Nullable>
): CustomId<
    Prefix,
    Shape &
        Record<
            Name,
            CustomIdField<Nullish<Snowflake, Nullable>>
        >
>

someOf()

Add a field that holds any subset of a fixed list, decoded as an array of the literal union.

A duplicate collapses into one entry. Decode returns the picks in the order the CustomId declares them. One choice costs one bit on the wire.

public someOf<Name, Choices, Nullable>(
    name: Name,
    choices: Choices,
    opts?: FieldOptions<Nullable>
): CustomId<
    Prefix,
    Shape &
        Record<
            Name,
            CustomIdField<
                Nullish<Choices[number][], Nullable>
            >
        >
>

str()

Add a free short text field. Avoid it where possible. It cannot be packed, which makes it the most expensive field on the wire.

public str<Name, Nullable>(
    name: Name,
    opts?: FieldOptions<Nullable>
): CustomId<
    Prefix,
    Shape &
        Record<
            Name,
            CustomIdField<Nullish<string, Nullable>>
        >
>

uuid()

Add a UUID field, decoded as a lowercase uuid string.

public uuid<Name, Nullable>(
    name: Name,
    opts?: FieldOptions<Nullable>
): CustomId<
    Prefix,
    Shape &
        Record<
            Name,
            CustomIdField<Nullish<string, Nullable>>
        >
>