class CustomId<
Prefix extends string,
Shape extends CustomIdShape = {}
>import { ButtonHandler, ButtonRoute, CustomId } from '@seedcord/gateway';
// declare this in the component file that mints the button
export const Approve = new CustomId('approve').snowflake('userId').oneOf('action', ['approve', 'deny']);
\@ButtonRoute(Approve)
export class ApproveButton extends ButtonHandler<[typeof Approve]> {
public async execute(): Promise<void> {
const { userId, action } = this.params;
// userId: string, action: 'approve' | 'deny'
await this.reply(`${action} for <@${userId}>`);
}
}// Using it outside seedcord:
import { ButtonBuilder, Events } from 'discord.js';
import { CustomId } from '@seedcord/custom-id';
// one declaration, imported by both sides
const Approve = new CustomId('approve').snowflake('userId').oneOf('action', ['approve', 'deny']);
// minting, on the button you send
new ButtonBuilder().setCustomId(Approve.encode({ userId: '123', action: 'deny' })).setLabel('Deny');
// reading, wherever your bot receives the click
client.on(Events.InteractionCreate, (interaction) => {
if (!interaction.isButton()) return;
if (!Approve.owns(interaction.customId)) return;
const { userId, action } = Approve.decode(interaction.customId);
// userId: string, action: 'approve' | 'deny'
});Member visibility
Filter class members by access level.
Prefix
The stable route prefix, e.g. 'approve'.
Prefix extends stringShape
The accumulated fields, filled in by the chain.
Shape extends CustomIdShape = {}public bool<Name, Nullable>(
name: Name,
opts?: FieldOptions<Nullable>
): CustomId<
Prefix,
Shape &
Record<
Name,
CustomIdField<Nullish<boolean, Nullable>>
>
>new CustomId('settings').bool('silent');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>wireThe customId string from the interaction.
const Ticket = new CustomId('ticket').snowflake('ownerId').int('page', 1, 50);
const { ownerId, page } = Ticket.decode(interaction.customId);
// ownerId: string, page: number
// a message from an older deploy still carries its old button. catch the throw and answer it
try {
Ticket.decode(interaction.customId);
} catch {
await interaction.reply('This button is out of date. Run the command again.');
}public encode(values: DecodedParams<Shape>): stringvaluesOne value per field, typed by the chain.
const Ticket = new CustomId('ticket').snowflake('ownerId').int('page', 1, 50);
new ButtonBuilder().setCustomId(Ticket.encode({ ownerId: user.id, page: 3 })).setLabel('Next');
// 900 falls outside the declared 1 to 50. this throws at runtime
Ticket.encode({ ownerId: user.id, page: 900 });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>>
>
>new CustomId('shop').int('amount');public oneOf<Name, Choices, Nullable>(
name: Name,
choices: Choices,
opts?: FieldOptions<Nullable>
): CustomId<
Prefix,
Shape &
Record<
Name,
CustomIdField<
Nullish<Choices[number], Nullable>
>
>
>new CustomId('poll').oneOf('choice', ['yes', 'no', 'abstain']);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 snowflake<Name, Nullable>(
name: Name,
opts?: FieldOptions<Nullable>
): CustomId<
Prefix,
Shape &
Record<
Name,
CustomIdField<Nullish<Snowflake, Nullable>>
>
>new CustomId('ban').snowflake('userId');public someOf<Name, Choices, Nullable>(
name: Name,
choices: Choices,
opts?: FieldOptions<Nullable>
): CustomId<
Prefix,
Shape &
Record<
Name,
CustomIdField<
Nullish<Choices[number][], Nullable>
>
>
>// a member picks roles in a select menu, and the confirm button carries them
// roles: ('reader' | 'artist' | 'vip')[]
new CustomId('assign').someOf('roles', ['reader', 'artist', 'vip']);public str<Name, Nullable>(
name: Name,
opts?: FieldOptions<Nullable>
): CustomId<
Prefix,
Shape &
Record<
Name,
CustomIdField<Nullish<string, Nullable>>
>
>new CustomId('note').str('message');public uuid<Name, Nullable>(
name: Name,
opts?: FieldOptions<Nullable>
): CustomId<
Prefix,
Shape &
Record<
Name,
CustomIdField<Nullish<string, Nullable>>
>
>new CustomId('ticket').uuid('ticketId');