@seedcord/kit 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +190 -0
- package/README.md +13 -0
- package/dist/CustomId-5Zl_LdzZ.mjs +648 -0
- package/dist/CustomId-5Zl_LdzZ.mjs.map +1 -0
- package/dist/CustomId-BuIoGHXw.cjs +695 -0
- package/dist/CustomId-BuIoGHXw.cjs.map +1 -0
- package/dist/CustomId-CbTZuUup.d.mts +313 -0
- package/dist/index.cjs +84 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1 -0
- package/dist/index.d.mts +135 -0
- package/dist/index.mjs +77 -0
- package/dist/index.mjs.map +1 -0
- package/dist/internal.index.cjs +19 -0
- package/dist/internal.index.cjs.map +1 -0
- package/dist/internal.index.d.cts +1 -0
- package/dist/internal.index.d.mts +36 -0
- package/dist/internal.index.mjs +14 -0
- package/dist/internal.index.mjs.map +1 -0
- package/package.json +73 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CustomId-BuIoGHXw.cjs","names":["SlashCommandBuilder","ContextMenuCommandBuilder","SlashCommandSubcommandBuilder","SlashCommandSubcommandGroupBuilder","EmbedBuilder","ModalBuilder","LabelBuilder","TextInputBuilder","FileUploadBuilder","CheckboxBuilder","CheckboxGroupBuilder","CheckboxGroupOptionBuilder","RadioGroupBuilder","RadioGroupOptionBuilder","ButtonBuilder","StringSelectMenuBuilder","StringSelectMenuOptionBuilder","UserSelectMenuBuilder","ChannelSelectMenuBuilder","MentionableSelectMenuBuilder","RoleSelectMenuBuilder","ContainerBuilder","TextDisplayBuilder","FileBuilder","MediaGalleryBuilder","SectionBuilder","SeparatorBuilder","ActionRowBuilder","SlashCommandBuilder","ContextMenuCommandBuilder","InteractionContextType","EmbedBuilder","ContainerBuilder","TextDisplayBuilder","SeedcordRangeError","SeedcordErrorCode","SeedcordError","SeedcordErrorCode","SeedcordRangeError"],"sources":["../src/botColorHolder.ts","../src/components/builderTypes.ts","../src/components/Component.ts","../src/stops/Notice.ts","../src/stops/NoticeCard.ts","../src/customId/Errors.ts","../src/customId/codec.ts","../src/customId/CustomId.ts"],"sourcesContent":["import type { ColorResolvable } from 'discord.js';\n\n// 'Default' is the value discord.js reads as \"no explicit color\".\nconst DEFAULT_COLOR: ColorResolvable = 'Default';\n\nlet current: ColorResolvable = DEFAULT_COLOR;\n\n/** @internal */\nexport function setBotColor(color: ColorResolvable | undefined): void {\n current = color ?? DEFAULT_COLOR;\n}\n\n/** @internal */\nexport function getBotColor(): ColorResolvable {\n return current;\n}\n","import {\n ActionRowBuilder,\n ButtonBuilder,\n ChannelSelectMenuBuilder,\n CheckboxBuilder,\n CheckboxGroupBuilder,\n CheckboxGroupOptionBuilder,\n ContainerBuilder,\n ContextMenuCommandBuilder,\n EmbedBuilder,\n FileBuilder,\n FileUploadBuilder,\n LabelBuilder,\n MediaGalleryBuilder,\n MentionableSelectMenuBuilder,\n ModalBuilder,\n RadioGroupBuilder,\n RadioGroupOptionBuilder,\n RoleSelectMenuBuilder,\n SectionBuilder,\n SeparatorBuilder,\n SlashCommandBuilder,\n SlashCommandSubcommandBuilder,\n SlashCommandSubcommandGroupBuilder,\n StringSelectMenuBuilder,\n StringSelectMenuOptionBuilder,\n TextDisplayBuilder,\n TextInputBuilder,\n UserSelectMenuBuilder\n} from 'discord.js';\n\n/**\n * Available Discord.js builder classes for use with BuilderComponent for commands, embeds, modals, etc.\n *\n * @internal\n */\nexport const BuilderTypes = {\n // Command Components\n command: SlashCommandBuilder,\n context_menu: ContextMenuCommandBuilder,\n subcommand: SlashCommandSubcommandBuilder,\n group: SlashCommandSubcommandGroupBuilder,\n\n // Embed Components\n embed: EmbedBuilder,\n\n // Modal Components\n modal: ModalBuilder,\n label: LabelBuilder,\n text_input: TextInputBuilder,\n file_upload: FileUploadBuilder,\n checkbox: CheckboxBuilder,\n checkbox_group: CheckboxGroupBuilder,\n checkbox_group_option: CheckboxGroupOptionBuilder,\n radio_group: RadioGroupBuilder,\n radio_group_option: RadioGroupOptionBuilder,\n\n // Action Row Components\n button: ButtonBuilder,\n menu_string: StringSelectMenuBuilder,\n menu_option_string: StringSelectMenuOptionBuilder,\n menu_user: UserSelectMenuBuilder,\n menu_channel: ChannelSelectMenuBuilder,\n menu_mentionable: MentionableSelectMenuBuilder,\n menu_role: RoleSelectMenuBuilder,\n\n // ComponentsV2\n container: ContainerBuilder,\n text_display: TextDisplayBuilder,\n file: FileBuilder,\n media: MediaGalleryBuilder,\n section: SectionBuilder,\n separator: SeparatorBuilder\n};\n\n/**\n * Available Discord.js action row classes for use with RowComponent for Select Menus and Buttons\n *\n * @internal\n */\nexport const RowTypes: {\n button: typeof ActionRowBuilder<ButtonBuilder>;\n menu_string: typeof ActionRowBuilder<StringSelectMenuBuilder>;\n menu_user: typeof ActionRowBuilder<UserSelectMenuBuilder>;\n menu_channel: typeof ActionRowBuilder<ChannelSelectMenuBuilder>;\n menu_mentionable: typeof ActionRowBuilder<MentionableSelectMenuBuilder>;\n menu_role: typeof ActionRowBuilder<RoleSelectMenuBuilder>;\n} = {\n button: ActionRowBuilder<ButtonBuilder>,\n menu_string: ActionRowBuilder<StringSelectMenuBuilder>,\n menu_user: ActionRowBuilder<UserSelectMenuBuilder>,\n menu_channel: ActionRowBuilder<ChannelSelectMenuBuilder>,\n menu_mentionable: ActionRowBuilder<MentionableSelectMenuBuilder>,\n menu_role: ActionRowBuilder<RoleSelectMenuBuilder>\n};\n\n/**\n * Available Discord.js builder types for use with BuilderComponent\n */\nexport type BuilderType = keyof typeof BuilderTypes;\n\n/**\n * @internal\n */\nexport type InstantiatedBuilder<BuilderKey extends BuilderType> = InstanceType<(typeof BuilderTypes)[BuilderKey]>;\n\n/**\n * Available Discord.js action row types for use with RowComponent\n */\nexport type RowType = keyof typeof RowTypes;\n\n/**\n * @internal\n */\nexport type InstantiatedActionRow<RowKey extends RowType> = InstanceType<(typeof RowTypes)[RowKey]>;\n","import {\n ContainerBuilder,\n ContextMenuCommandBuilder,\n EmbedBuilder,\n InteractionContextType,\n resolveColor,\n SlashCommandBuilder\n} from 'discord.js';\n\nimport { getBotColor } from '@src/botColorHolder';\n\nimport { BuilderTypes, RowTypes } from './builderTypes';\n\nimport type { BuilderType, InstantiatedActionRow, InstantiatedBuilder, RowType } from './builderTypes';\n\n/**\n * Base class for Discord component wrappers.\n *\n * @typeParam TComponent - The Discord.js component type being wrapped\n *\n * @internal\n */\nabstract class BaseComponent<TComponent> {\n private readonly _component: TComponent;\n\n protected constructor(ComponentClass: new () => TComponent) {\n this._component = new ComponentClass();\n }\n\n /**\n * Returns the live builder, ready to send in a Discord message or nest in another component.\n *\n * Configure it through `this.instance`, not here. Reading this can apply the bot color (see\n * {@link BuilderComponent}), so a read is not side-effect free.\n * @example new SomeComponent().component\n */\n public abstract get component(): InstantiatedBuilder<BuilderType> | InstantiatedActionRow<RowType>;\n\n /**\n * The wrapped builder, for calling Discord.js methods like setTitle() and setDescription() inside a subclass.\n *\n * @example this.instance.setTitle('My Modal')\n */\n protected get instance(): TComponent {\n return this._component;\n }\n}\n\n/**\n * Base class for Discord.js builder components\n *\n * Wraps Discord.js builders (SlashCommandBuilder, EmbedBuilder, etc.) with\n * Seedcord-specific defaults and helper methods.\n *\n * @typeParam BuilderKey - The type of Discord.js builder being wrapped\n */\nexport abstract class BuilderComponent<BuilderKey extends BuilderType> extends BaseComponent<\n InstantiatedBuilder<BuilderKey>\n> {\n private colorApplied = false;\n\n protected constructor(public readonly type: BuilderKey) {\n const ComponentClass = BuilderTypes[type] as unknown;\n super(ComponentClass as new () => InstantiatedBuilder<BuilderKey>);\n\n if (this.instance instanceof SlashCommandBuilder || this.instance instanceof ContextMenuCommandBuilder) {\n this.instance.setContexts(InteractionContextType.Guild);\n }\n }\n\n get component(): InstantiatedBuilder<BuilderKey> {\n this.applyBotColor();\n return this.instance;\n }\n\n // Resolving in the constructor would capture the default for a component built before setBotColor()\n // ran. The unset check keeps a color the subclass set for itself.\n private applyBotColor(): void {\n if (this.colorApplied) return;\n this.colorApplied = true;\n\n const color = getBotColor();\n if (this.instance instanceof EmbedBuilder) {\n if (this.instance.data.color === undefined) this.instance.setColor(color);\n } else if (this.instance instanceof ContainerBuilder) {\n const accent = this.instance.data.accent_color;\n if (accent === null || accent === undefined) {\n this.instance.setAccentColor(color === 'Default' ? undefined : resolveColor(color));\n }\n }\n }\n}\n\n/**\n * Base class for Discord action row components\n *\n * Wraps Discord.js action row builder with Seedcord-specific defaults and helper methods.\n *\n * @typeParam RowKey - The Discord.js action row type being wrapped\n */\nexport abstract class RowComponent<RowKey extends RowType> extends BaseComponent<InstantiatedActionRow<RowKey>> {\n protected constructor(public readonly type: RowKey) {\n const ComponentClass = RowTypes[type] as unknown;\n super(ComponentClass as new () => InstantiatedActionRow<RowKey>);\n }\n\n get component(): InstantiatedActionRow<RowKey> {\n return this.instance;\n }\n}\n","import type { RenderContext, ReplyResponse } from '@seedcord/types';\n\n/**\n * Base class for a user-facing refusal or a reported fault.\n *\n * Throw a `Notice` to stop a handler and reply to the user. The framework catches it at the controller\n * boundary and renders {@link Notice.render}, which always decides what the user sees. With `report`\n * false that render is all that happens. With `report` true the framework also logs the fault and\n * publishes it to the `handledException` bus. A raw, non-Notice throw shows the generic message.\n *\n * @example\n * ```ts\n * import { Notice, BuilderComponent, type RenderContext, type ReplyResponse } from 'seedcord';\n * import { TextDisplayBuilder } from 'discord.js';\n *\n * // reading `.component` applies the configured bot color to the container accent\n * class TooPoorCard extends BuilderComponent<'container'> {\n * constructor(balance: number) {\n * super('container');\n * this.instance.addTextDisplayComponents(\n * new TextDisplayBuilder().setContent(`### Insufficient balance\\nYou need more than ${balance} coins.`)\n * );\n * }\n * }\n *\n * class TooPoor extends Notice {\n * constructor(private readonly balance: number) {\n * super(`balance ${balance} is below the cost`);\n * }\n *\n * render(_ctx: RenderContext): ReplyResponse {\n * return { components: [new TooPoorCard(this.balance).component] };\n * }\n * }\n *\n * // in a handler, throwing stops the handler and replies with render(ctx)\n * if (wallet.balance < cost) throw new TooPoor(wallet.balance);\n * ```\n */\nexport abstract class Notice extends Error {\n /**\n * Whether this denial is a reported fault. True also logs it and publishes it to the `handledException`\n * bus. The user always sees {@link Notice.render} either way.\n *\n * @defaultValue `false`\n */\n public report = false;\n\n /**\n * Whether the reply is ephemeral, so only the invoking user sees it. Set it false for a refusal the\n * whole channel should see.\n *\n * @defaultValue `true`\n */\n public ephemeral = true;\n\n /**\n * A short one-line reason. When every arm of an `or` gate refuses and each refusal sets this, `or`\n * lists them instead of showing a neutral message.\n */\n public summary?: string;\n\n protected constructor(message: string, options?: ErrorOptions) {\n super(message, options);\n\n // Error sets name to 'Error', so stamp the concrete subclass name for logs and the fault report\n this.name = new.target.name;\n Error.captureStackTrace(this, this.constructor);\n }\n\n /**\n * Builds what the user sees. Called fresh each time the denial is shown, so the builders are new\n * and the bot color resolves at render time rather than at construction time.\n */\n public abstract render(ctx: RenderContext): ReplyResponse;\n}\n","import { TextDisplayBuilder } from 'discord.js';\n\nimport { BuilderComponent } from '@components/Component';\n\n/**\n * Built fresh inside a {@link Notice}'s `render` to back its ComponentsV2 reply. The title renders as\n * an h3 line with the description on the next line.\n */\nexport class NoticeCard extends BuilderComponent<'container'> {\n public constructor(description: string, title = 'Cannot Proceed') {\n super('container');\n this.instance.addTextDisplayComponents(new TextDisplayBuilder().setContent(`### ${title}\\n${description}`));\n }\n}\n","import { Notice } from '@stops/Notice';\nimport { NoticeCard } from '@stops/NoticeCard';\n\nimport type { ReplyResponse } from '@seedcord/types';\n\n/**\n * Thrown when a customId was minted by an older version of its shape.\n *\n * This is normal after the shape changes. The reply tells the user to run the command again.\n */\nexport class StaleCustomId extends Notice {\n constructor(prefix: string) {\n super(`Stale customId for \"${prefix}\".`);\n }\n\n render(): ReplyResponse {\n const card = new NoticeCard(\n 'This button or menu is from an older version. Please run the command again.',\n 'Outdated'\n );\n return { components: [card.component] };\n }\n}\n\n/**\n * Thrown when a customId wire is corrupt or tampered with and cannot be trusted.\n *\n * This should not happen in normal use, so it reports.\n */\nexport class InvalidCustomId extends Notice {\n constructor(detail: string) {\n super(`Invalid customId. ${detail}`);\n this.report = true;\n }\n\n render(): ReplyResponse {\n const card = new NoticeCard('Something went wrong. Please try again.');\n return { components: [card.component] };\n }\n}\n","/* eslint-disable no-magic-numbers -- lots of bigints */\n\nimport { SeedcordErrorCode } from '@seedcord/errors';\nimport { SeedcordRangeError } from '@seedcord/errors/internal';\n\nimport { InvalidCustomId } from './Errors';\n\nimport type { CustomIdField, CustomIdShape } from './Field';\n\n// wire is routeKey, a colon, then the body. the routeKey is the stable prefix plus a short shape\n// hash, so a shape change moves the routeKey and decode catches an old wire as stale. bounded\n// fields (known range) fold into one base64 integer by mixed-radix packing, unbounded ones (free\n// string, unbounded int) trail it as delimited tokens.\n//\n// works on runtime values (unknown), the typed facade in CustomId.ts guarantees the types.\n\n// url-safe base64, one utf-16 unit per char so discord never rewrites it.\nconst ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';\nconst BASE = 64n;\nconst CHAR_TO_VALUE = new Map([...ALPHABET].map((char, index) => [char, index] as const));\n\n// unbounded fields trail after the packed block, split by this char and escaped by the next.\nconst DELIMITER = '\\x1f';\nconst ESCAPE = '\\x1b';\n\n/** @internal */\nexport const HASH_LENGTH = 3;\n\nconst SAFE_MAX = BigInt(Number.MAX_SAFE_INTEGER);\nconst SAFE_MIN = BigInt(Number.MIN_SAFE_INTEGER);\n\n// manual accumulator, never parseInt, which loses precision past 2^53.\nfunction bigintToBase64(value: bigint): string {\n if (value === 0n) return ALPHABET.charAt(0);\n let text = '';\n for (let remaining = value; remaining > 0n; remaining /= BASE) {\n text = ALPHABET.charAt(Number(remaining % BASE)) + text;\n }\n return text;\n}\n\nfunction base64ToBigint(text: string): bigint {\n let value = 0n;\n for (const char of text) {\n const digit = CHAR_TO_VALUE.get(char);\n if (digit === undefined) throw new InvalidCustomId(`bad character ${JSON.stringify(char)}`);\n value = value * BASE + BigInt(digit);\n }\n return value;\n}\n\n// zigzag keeps a small negative number short on the wire.\nfunction zigzagEncode(value: number): bigint {\n const big = BigInt(value);\n return big >= 0n ? big << 1n : (-big << 1n) - 1n;\n}\nfunction zigzagDecode(encoded: bigint): bigint {\n return (encoded & 1n) === 1n ? -((encoded + 1n) >> 1n) : encoded >> 1n;\n}\n\nfunction escapeToken(text: string): string {\n return text.replace(/[\\x1b\\x1f]/g, (char) => ESCAPE + char);\n}\nfunction unescapeToken(text: string): string {\n let out = '';\n for (let i = 0; i < text.length; i++) {\n if (text.charAt(i) !== ESCAPE) {\n out += text.charAt(i);\n continue;\n }\n const next = text.charAt(i + 1);\n if (next === '') throw new InvalidCustomId('dangling escape at end of token');\n out += next;\n i++;\n }\n return out;\n}\nfunction splitTokens(body: string): string[] {\n const pieces: string[] = [];\n let current = '';\n for (let i = 0; i < body.length; i++) {\n const char = body.charAt(i);\n if (char === ESCAPE) {\n current += char + body.charAt(i + 1);\n i++;\n } else if (char === DELIMITER) {\n pieces.push(current);\n current = '';\n } else {\n current += char;\n }\n }\n pieces.push(current);\n return pieces;\n}\n\n// bounded means the full range is known, so the field can fold into the shared packed integer.\nfunction isBounded(field: CustomIdField<unknown>): boolean {\n if (field.kind === 'int') return field.min !== undefined && field.max !== undefined;\n return field.kind === 'snowflake' || field.kind === 'uuid' || field.kind === 'bool' || field.kind === 'oneOf';\n}\n\n// how many distinct values the field has. mixed-radix packing uses this as the field's base.\nfunction radixOf(field: CustomIdField<unknown>): bigint {\n switch (field.kind) {\n case 'snowflake':\n return 1n << 64n;\n case 'uuid':\n return 1n << 128n;\n case 'bool':\n return 2n;\n case 'oneOf':\n // oneOf() rejects an empty list at define time, so no choices here means a hand-built\n // corrupt shape rather than a real state.\n if (!field.choices?.length) throw new InvalidCustomId('oneOf field has no choices');\n return BigInt(field.choices.length);\n case 'int':\n // isBounded only routes a min-and-max int here, so a missing bound means a corrupt shape.\n if (field.min === undefined || field.max === undefined)\n throw new InvalidCustomId('bounded int field is missing a bound');\n // bigint before the math, max - min + 1 in float64 drops the +1 at 2^53.\n return BigInt(field.max) - BigInt(field.min) + 1n;\n default:\n throw new InvalidCustomId(`field kind ${field.kind} has no radix`);\n }\n}\n\nfunction boundedToBigint(field: CustomIdField<unknown>, name: string, value: unknown): bigint {\n const slot = boundedSlot(field, name, value);\n // out of range would carry into the neighbouring field on decode.\n if (slot < 0n || slot >= radixOf(field)) outOfRange(name, value);\n return slot;\n}\n\n// map a value to its slot, an integer in [0, radix). each kind maps differently.\nfunction boundedSlot(field: CustomIdField<unknown>, name: string, value: unknown): bigint {\n switch (field.kind) {\n case 'snowflake': {\n // a discord id is a non-negative integer string. reject non-strings here so a bad value\n // throws the branded out-of-range error rather than a raw BigInt() TypeError.\n if (typeof value !== 'string' || !/^\\d+$/.test(value)) return outOfRange(name, value);\n return BigInt(value);\n }\n case 'uuid': {\n if (typeof value !== 'string') return outOfRange(name, value);\n const hex = value.replace(/-/g, '');\n if (!/^[0-9a-fA-F]{32}$/.test(hex)) return outOfRange(name, value);\n return BigInt(`0x${hex}`);\n }\n case 'bool':\n return value ? 1n : 0n;\n case 'oneOf': {\n const index = (field.choices ?? []).indexOf(value as string);\n return index < 0 ? outOfRange(name, value) : BigInt(index);\n }\n case 'int': {\n if (!Number.isInteger(value)) return outOfRange(name, value);\n return BigInt((value as number) - (field.min ?? 0));\n }\n default:\n return outOfRange(name, value);\n }\n}\n\nfunction outOfRange(name: string, value: unknown): never {\n throw new SeedcordRangeError(SeedcordErrorCode.CustomIdValueOutOfRange, [name, String(value)]);\n}\n\n// inverse of boundedSlot, turn the slot back into the field's value.\nfunction bigintToBoundedValue(field: CustomIdField<unknown>, slot: bigint): unknown {\n switch (field.kind) {\n case 'snowflake':\n return slot.toString();\n case 'uuid':\n return bigintToUuid(slot);\n case 'bool':\n return slot === 1n;\n case 'oneOf':\n return (field.choices ?? [])[Number(slot)];\n case 'int':\n return Number(slot) + (field.min ?? 0);\n default:\n throw new InvalidCustomId(`field kind ${field.kind} is not bounded`);\n }\n}\n\nfunction bigintToUuid(value: bigint): string {\n const hex = value.toString(16).padStart(32, '0');\n return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;\n}\n\nfunction encodeUnboundedToken(field: CustomIdField<unknown>, name: string, value: unknown): string {\n if (field.kind === 'int') {\n if (!Number.isSafeInteger(value)) outOfRange(name, value);\n return bigintToBase64(zigzagEncode(value as number));\n }\n return escapeToken(value as string);\n}\nfunction decodeUnboundedToken(field: CustomIdField<unknown>, piece: string): unknown {\n if (field.kind !== 'int') return unescapeToken(piece);\n // an int always encodes to at least one char, so an empty piece is a truncated wire\n if (piece === '') throw new InvalidCustomId('empty integer token');\n const decoded = zigzagDecode(base64ToBigint(piece));\n // an unbounded int is authored as a js number, so anything past 2^53 was tampered with.\n if (decoded > SAFE_MAX || decoded < SAFE_MIN) throw new InvalidCustomId('integer out of safe range');\n return Number(decoded);\n}\n\n/**\n * A short fingerprint of the shape. Change the shape and the hash changes, so an old customId no\n * longer matches the current routeKey and decode catches it as stale.\n *\n * @internal\n */\nexport function computeLayoutHash(shape: CustomIdShape): string {\n // a structured json signature, never a joined string, so a value holding a separator cannot collide.\n const signature = JSON.stringify(\n Object.entries(shape).map(([name, field]) => [\n name,\n field.kind,\n isBounded(field),\n field.kind === 'oneOf' ? (field.choices ?? []) : null,\n field.kind === 'int' ? [field.min ?? null, field.max ?? null] : null\n ])\n );\n const modulus = BASE ** BigInt(HASH_LENGTH);\n let hash = 0n;\n for (const char of signature) hash = (hash * 131n + BigInt(char.charCodeAt(0))) % modulus;\n\n let text = '';\n for (let i = 0; i < HASH_LENGTH; i++) {\n text = ALPHABET.charAt(Number(hash % BASE)) + text;\n hash /= BASE;\n }\n return text;\n}\n\n/**\n * Pack values into a body. Bounded fields fold into one integer, unbounded fields trail after it.\n *\n * @internal\n */\nexport function encodeBody(shape: CustomIdShape, values: Record<string, unknown>): string {\n const fields = Object.entries(shape);\n const pieces: string[] = [];\n\n const bounded = fields.filter(([, field]) => isBounded(field));\n if (bounded.length > 0) {\n let packed = 0n;\n // fold each field in, multiply the running value by the field's radix then add its slot.\n for (const [name, field] of bounded)\n packed = packed * radixOf(field) + boundedToBigint(field, name, values[name]);\n pieces.push(bigintToBase64(packed));\n }\n for (const [name, field] of fields) {\n if (!isBounded(field)) pieces.push(encodeUnboundedToken(field, name, values[name]));\n }\n return pieces.join(DELIMITER);\n}\n\n// unpack the single bounded block back into result, reversing the field order.\nfunction unpackBounded(\n bounded: [string, CustomIdField<unknown>][],\n blob: string | undefined,\n result: Record<string, unknown>\n): void {\n // zero packs to one char, so an empty block means the body was truncated.\n if (blob === undefined || blob === '') throw new InvalidCustomId('empty packed block');\n let packed = base64ToBigint(blob);\n // last field packed is the first one back out.\n for (const [name, field] of [...bounded].reverse()) {\n const radix = radixOf(field);\n result[name] = bigintToBoundedValue(field, packed % radix);\n packed /= radix;\n }\n // leftover bits after every field is out means a corrupt block.\n if (packed !== 0n) throw new InvalidCustomId('leftover bits after unpacking');\n}\n\n/**\n * Reverse of encodeBody. Rejects any malformed or truncated body.\n *\n * @internal\n */\nexport function decodeBody(shape: CustomIdShape, body: string): Record<string, unknown> {\n const fields = Object.entries(shape);\n const bounded = fields.filter(([, field]) => isBounded(field));\n const unbounded = fields.filter(([, field]) => !isBounded(field));\n\n // a shape with no fields encodes to an empty body, so there is nothing to split or unpack.\n const expected = (bounded.length > 0 ? 1 : 0) + unbounded.length;\n if (expected === 0) {\n if (body !== '') throw new InvalidCustomId(`expected an empty body, got ${JSON.stringify(body)}`);\n return {};\n }\n\n const pieces = splitTokens(body);\n if (pieces.length !== expected) throw new InvalidCustomId(`expected ${expected} piece(s), got ${pieces.length}`);\n\n const result: Record<string, unknown> = {};\n let cursor = 0;\n\n if (bounded.length > 0) {\n unpackBounded(bounded, pieces[cursor], result);\n cursor++;\n }\n\n for (const [name, field] of unbounded) {\n const piece = pieces[cursor];\n cursor++;\n if (piece === undefined) throw new InvalidCustomId('missing trailing piece');\n result[name] = decodeUnboundedToken(field, piece);\n }\n\n return result;\n}\n","import { SeedcordErrorCode } from '@seedcord/errors';\nimport { SeedcordError, SeedcordRangeError } from '@seedcord/errors/internal';\n\nimport { computeLayoutHash, decodeBody, encodeBody, HASH_LENGTH } from './codec';\nimport { InvalidCustomId, StaleCustomId } from './Errors';\n\nimport type { CustomIdField, CustomIdShape, DecodedParams } from './Field';\nimport type { Snowflake } from 'discord.js';\nimport type { NonEmptyTuple } from 'type-fest';\n\n// discord caps a customId at 100 chars.\nconst MAX_WIRE_LENGTH = 100;\n\nfunction routeKeyOf(wire: string): string {\n const colon = wire.indexOf(':');\n return colon < 0 ? '' : wire.slice(0, colon);\n}\n\n/** Strip the layout hash off the routeKey to recover the stable prefix the controller routes by. @internal */\nexport function prefixOf(wire: string): string {\n const key = routeKeyOf(wire);\n return key.length <= HASH_LENGTH ? '' : key.slice(0, key.length - HASH_LENGTH);\n}\n\n/**\n * A typed customId. The single source of truth shared by the component that mints it and the handler\n * that reads it. This gives you typed reads on the `.customId` field in components. Values are packed into a compact wire string rather than plain stringified tokens, so the 100-char Discord limit goes further. More string per string, basically.\n *\n * @typeParam Prefix - The stable route prefix, e.g. 'approve'.\n * @typeParam Shape - The accumulated fields, filled in by the chain.\n *\n * @example\n * ```ts\n * const ApproveId = new CustomId('approve')\n * .snowflake('userId')\n * .oneOf('action', ['approve', 'deny']);\n *\n * // Set the custom id on a button when creating it.\n * new ButtonBuilder().setCustomId(ApproveId.encode({ userId: '123', action: 'approve' }));\n *\n * // reading in the handler: userId comes back a string\n * const { userId, action } = this.params; // userId: string, action: 'approve' | 'deny'\n * await this.event.guild?.members.fetch(userId);\n * ```\n */\nexport class CustomId<Prefix extends string, Shape extends CustomIdShape = {}> {\n readonly prefix: Prefix;\n readonly shape: Shape;\n /** The prefix plus a short hash of the shape, the part of the wire before the colon. */\n readonly routeKey: string;\n\n constructor(prefix: Prefix, shape: Shape = {} as Shape) {\n // an empty prefix would make the routeKey all-hash so prefixOf strips it to nothing and the\n // controller cannot route it, and a colon or control char would break the wire framing.\n if (!prefix || /[:\\x1b\\x1f]/.test(prefix)) {\n throw new SeedcordError(SeedcordErrorCode.CustomIdInvalidPrefix, [prefix]);\n }\n this.prefix = prefix;\n this.shape = shape;\n this.routeKey = prefix + computeLayoutHash(shape);\n }\n\n // a fresh immutable CustomId with one more field so we don't need a `as unknown as this` cast.\n private add<Name extends string, Decoded>(\n name: Name,\n field: CustomIdField<Decoded>\n ): CustomId<Prefix, Shape & Record<Name, CustomIdField<Decoded>>> {\n // integer-like keys get reordered by js, which would scramble the field order.\n if (/^(?:0|[1-9]\\d*)$/.test(name)) throw new SeedcordError(SeedcordErrorCode.CustomIdReservedFieldName, [name]);\n // a repeat name collapses the field's decoded type to never and overwrites the earlier field\n // at runtime, so reject it here at define time.\n if (name in this.shape) throw new SeedcordError(SeedcordErrorCode.CustomIdDuplicateFieldName, [name]);\n // justified, the spread plus a computed key cannot be proven to the exact intersection\n const shape = { ...this.shape, [name]: field } as Shape & Record<Name, CustomIdField<Decoded>>;\n return new CustomId(this.prefix, shape);\n }\n\n /**\n * Add a Discord ID field, decoded as a string (the discord.js `Snowflake` type).\n *\n * @example\n * ```ts\n * new CustomId('ban').snowflake('userId');\n * ```\n */\n snowflake<Name extends string>(name: Name): CustomId<Prefix, Shape & Record<Name, CustomIdField<Snowflake>>> {\n return this.add<Name, Snowflake>(name, { kind: 'snowflake' });\n }\n\n /**\n * Add a UUID field, decoded as a lowercase uuid string.\n *\n * @example\n * ```ts\n * new CustomId('ticket').uuid('ticketId');\n * ```\n */\n uuid<Name extends string>(name: Name): CustomId<Prefix, Shape & Record<Name, CustomIdField<string>>> {\n return this.add<Name, string>(name, { kind: 'uuid' });\n }\n\n /**\n * Add an integer field with no bounds, for a value up to 2^53.\n *\n * @example\n * ```ts\n * new CustomId('shop').int('amount');\n * ```\n */\n int<Name extends string>(name: Name): CustomId<Prefix, Shape & Record<Name, CustomIdField<number>>>;\n /**\n * Add an integer field bounded by min and max, so it packs into fewer characters on the wire.\n *\n * @example\n * ```ts\n * new CustomId('paginate').int('page', 1, 50);\n * ```\n */\n int<Name extends string>(\n name: Name,\n min: number,\n max: number\n ): CustomId<Prefix, Shape & Record<Name, CustomIdField<number>>>;\n int<Name extends string>(\n name: Name,\n min?: number,\n max?: number\n ): CustomId<Prefix, Shape & Record<Name, CustomIdField<number>>> {\n if (min !== undefined && max !== undefined && min > max) {\n throw new SeedcordError(SeedcordErrorCode.CustomIdInvalidBounds, [name, min, max]);\n }\n const field: CustomIdField<number> =\n min === undefined || max === undefined ? { kind: 'int' } : { kind: 'int', min, max };\n return this.add<Name, number>(name, field);\n }\n\n /**\n * Add a boolean flag.\n *\n * @example\n * ```ts\n * new CustomId('settings').bool('silent');\n * ```\n */\n bool<Name extends string>(name: Name): CustomId<Prefix, Shape & Record<Name, CustomIdField<boolean>>> {\n return this.add<Name, boolean>(name, { kind: 'bool' });\n }\n\n /**\n * Add a field that is one value from a fixed list, decoded as the literal union. No `as const` needed.\n *\n * @example\n * ```ts\n * new CustomId('poll').oneOf('choice', ['yes', 'no', 'abstain']);\n * ```\n */\n oneOf<Name extends string, const Choices extends NonEmptyTuple<string>>(\n name: Name,\n choices: Choices\n ): CustomId<Prefix, Shape & Record<Name, CustomIdField<Choices[number]>>> {\n if (choices.length === 0) throw new SeedcordError(SeedcordErrorCode.CustomIdEmptyChoices, [name]);\n return this.add<Name, Choices[number]>(name, { kind: 'oneOf', choices });\n }\n\n /**\n * Add a free short text field. Avoid it where possible, it cannot be packed so it costs the most wire space.\n *\n * @example\n * ```ts\n * new CustomId('note').str('message');\n * ```\n */\n str<Name extends string>(name: Name): CustomId<Prefix, Shape & Record<Name, CustomIdField<string>>> {\n return this.add<Name, string>(name, { kind: 'string' });\n }\n\n /**\n * Mint a wire string from values. Throws if a value is out of its field's range or the wire is over 100 chars.\n *\n * @param values - One value per field, typed by the chain.\n * @returns The wire string to put on the component's customId.\n */\n encode(values: DecodedParams<Shape>): string {\n const wire = `${this.routeKey}:${encodeBody(this.shape, values)}`;\n if (wire.length > MAX_WIRE_LENGTH) {\n throw new SeedcordRangeError(SeedcordErrorCode.CustomIdWireTooLong, [wire.length]);\n }\n return wire;\n }\n\n /**\n * Read a wire string back into values.\n *\n * @param wire - The customId string from the interaction.\n * @returns The decoded values, typed by the chain.\n * @throws A {@link StaleCustomId} when the shape changed since the wire was minted.\n * @throws An {@link InvalidCustomId} on a corrupt or foreign wire.\n */\n decode(wire: string): DecodedParams<Shape> {\n const key = routeKeyOf(wire);\n if (key !== this.routeKey) {\n // same prefix but a different hash means the shape changed since this wire was minted.\n if (prefixOf(wire) === this.prefix) throw new StaleCustomId(this.prefix);\n throw new InvalidCustomId(`routeKey ${JSON.stringify(key)} is not ${JSON.stringify(this.routeKey)}`);\n }\n // justified, the codec returns runtime values and the shape guarantees their decoded types.\n return decodeBody(this.shape, wire.slice(key.length + 1)) as DecodedParams<Shape>;\n }\n\n /** True if this wire was minted from this customId's prefix, ignoring the shape hash. */\n owns(wire: string): boolean {\n return prefixOf(wire) === this.prefix;\n }\n}\n\n/**\n * Any customId, for places where the exact prefix and shape do not matter.\n *\n * @internal\n */\nexport type AnyCustomId = CustomId<string, CustomIdShape>;\n\n/**\n * The outcome of decoding a wire against several customIds, the matched prefix paired with its values.\n *\n * @internal\n */\nexport type DecodedRoute<Defs extends readonly AnyCustomId[]> = {\n [Index in keyof Defs]: Defs[Index] extends AnyCustomId\n ? { readonly prefix: Defs[Index]['prefix']; readonly params: DecodedParams<Defs[Index]['shape']> }\n : never;\n}[number];\n\n/**\n * Find the customId whose prefix owns this wire, decode against it, and report which one matched.\n *\n * @internal\n */\nexport function decodeFor<Defs extends readonly AnyCustomId[]>(defs: Defs, wire: string): DecodedRoute<Defs> {\n const match = defs.find((def) => def.owns(wire));\n if (!match) throw new InvalidCustomId(`no customId owns ${JSON.stringify(routeKeyOf(wire))}`);\n // justified, the matched customId fixes both prefix and params together but find() loses that link.\n return { prefix: match.prefix, params: match.decode(wire) } as DecodedRoute<Defs>;\n}\n"],"mappings":";;;;;AAGA,MAAM,gBAAiC;AAEvC,IAAI,UAA2B;;AAG/B,SAAgB,YAAY,OAA0C;CAClE,UAAU,SAAS;AACvB;;AAGA,SAAgB,cAA+B;CAC3C,OAAO;AACX;;;;;;;;;ACqBA,MAAa,eAAe;CAExB,SAASA;CACT,cAAcC;CACd,YAAYC;CACZ,OAAOC;CAGP,OAAOC;CAGP,OAAOC;CACP,OAAOC;CACP,YAAYC;CACZ,aAAaC;CACb,UAAUC;CACV,gBAAgBC;CAChB,uBAAuBC;CACvB,aAAaC;CACb,oBAAoBC;CAGpB,QAAQC;CACR,aAAaC;CACb,oBAAoBC;CACpB,WAAWC;CACX,cAAcC;CACd,kBAAkBC;CAClB,WAAWC;CAGX,WAAWC;CACX,cAAcC;CACd,MAAMC;CACN,OAAOC;CACP,SAASC;CACT,WAAWC;AACf;;;;;;AAOA,MAAa,WAOT;CACA,QAAQC;CACR,aAAaA;CACb,WAAWA;CACX,cAAcA;CACd,kBAAkBA;CAClB,WAAWA;AACf;;;;;;;;;;;ACxEA,IAAe,gBAAf,MAAyC;CACrC,AAAiB;CAEjB,AAAU,YAAY,gBAAsC;EACxD,KAAK,aAAa,IAAI,eAAe;CACzC;;;;;;CAgBA,IAAc,WAAuB;EACjC,OAAO,KAAK;CAChB;AACJ;;;;;;;;;AAUA,IAAsB,mBAAtB,cAA+E,cAE7E;CAGwC;CAFtC,AAAQ,eAAe;CAEvB,AAAU,YAAY,AAAgB,MAAkB;EACpD,MAAM,iBAAiB,aAAa;EACpC,MAAM,cAA2D;EAF/B;EAIlC,IAAI,KAAK,oBAAoBC,kCAAuB,KAAK,oBAAoBC,sCACzE,KAAK,SAAS,YAAYC,kCAAuB,KAAK;CAE9D;CAEA,IAAI,YAA6C;EAC7C,KAAK,cAAc;EACnB,OAAO,KAAK;CAChB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uilder, ContextMenuCommandBuilder, EmbedBuilder, FileBuilder, FileUploadBuilder, LabelBuilder, MediaGalleryBuilder, MentionableSelectMenuBuilder, ModalBuilder, RadioGroupBuilder, RadioGroupOptionBuilder, RoleSelectMenuBuilder, SectionBuilder, SeparatorBuilder, SlashCommandBuilder, SlashCommandSubcommandBuilder, SlashCommandSubcommandGroupBuilder, Snowflake, StringSelectMenuBuilder, StringSelectMenuOptionBuilder, TextDisplayBuilder, TextInputBuilder, UserSelectMenuBuilder } from "discord.js";
|
|
2
|
+
|
|
3
|
+
//#region src/components/builderTypes.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Available Discord.js builder classes for use with BuilderComponent for commands, embeds, modals, etc.
|
|
6
|
+
*
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
declare const BuilderTypes: {
|
|
10
|
+
command: typeof SlashCommandBuilder;
|
|
11
|
+
context_menu: typeof ContextMenuCommandBuilder;
|
|
12
|
+
subcommand: typeof SlashCommandSubcommandBuilder;
|
|
13
|
+
group: typeof SlashCommandSubcommandGroupBuilder;
|
|
14
|
+
embed: typeof EmbedBuilder;
|
|
15
|
+
modal: typeof ModalBuilder;
|
|
16
|
+
label: typeof LabelBuilder;
|
|
17
|
+
text_input: typeof TextInputBuilder;
|
|
18
|
+
file_upload: typeof FileUploadBuilder;
|
|
19
|
+
checkbox: typeof CheckboxBuilder;
|
|
20
|
+
checkbox_group: typeof CheckboxGroupBuilder;
|
|
21
|
+
checkbox_group_option: typeof CheckboxGroupOptionBuilder;
|
|
22
|
+
radio_group: typeof RadioGroupBuilder;
|
|
23
|
+
radio_group_option: typeof RadioGroupOptionBuilder;
|
|
24
|
+
button: typeof ButtonBuilder;
|
|
25
|
+
menu_string: typeof StringSelectMenuBuilder;
|
|
26
|
+
menu_option_string: typeof StringSelectMenuOptionBuilder;
|
|
27
|
+
menu_user: typeof UserSelectMenuBuilder;
|
|
28
|
+
menu_channel: typeof ChannelSelectMenuBuilder;
|
|
29
|
+
menu_mentionable: typeof MentionableSelectMenuBuilder;
|
|
30
|
+
menu_role: typeof RoleSelectMenuBuilder;
|
|
31
|
+
container: typeof ContainerBuilder;
|
|
32
|
+
text_display: typeof TextDisplayBuilder;
|
|
33
|
+
file: typeof FileBuilder;
|
|
34
|
+
media: typeof MediaGalleryBuilder;
|
|
35
|
+
section: typeof SectionBuilder;
|
|
36
|
+
separator: typeof SeparatorBuilder;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Available Discord.js action row classes for use with RowComponent for Select Menus and Buttons
|
|
40
|
+
*
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
declare const RowTypes: {
|
|
44
|
+
button: typeof ActionRowBuilder<ButtonBuilder>;
|
|
45
|
+
menu_string: typeof ActionRowBuilder<StringSelectMenuBuilder>;
|
|
46
|
+
menu_user: typeof ActionRowBuilder<UserSelectMenuBuilder>;
|
|
47
|
+
menu_channel: typeof ActionRowBuilder<ChannelSelectMenuBuilder>;
|
|
48
|
+
menu_mentionable: typeof ActionRowBuilder<MentionableSelectMenuBuilder>;
|
|
49
|
+
menu_role: typeof ActionRowBuilder<RoleSelectMenuBuilder>;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Available Discord.js builder types for use with BuilderComponent
|
|
53
|
+
*/
|
|
54
|
+
type BuilderType = keyof typeof BuilderTypes;
|
|
55
|
+
/**
|
|
56
|
+
* @internal
|
|
57
|
+
*/
|
|
58
|
+
type InstantiatedBuilder<BuilderKey extends BuilderType> = InstanceType<(typeof BuilderTypes)[BuilderKey]>;
|
|
59
|
+
/**
|
|
60
|
+
* Available Discord.js action row types for use with RowComponent
|
|
61
|
+
*/
|
|
62
|
+
type RowType = keyof typeof RowTypes;
|
|
63
|
+
/**
|
|
64
|
+
* @internal
|
|
65
|
+
*/
|
|
66
|
+
type InstantiatedActionRow<RowKey extends RowType> = InstanceType<(typeof RowTypes)[RowKey]>;
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/components/Component.d.ts
|
|
69
|
+
/**
|
|
70
|
+
* Base class for Discord component wrappers.
|
|
71
|
+
*
|
|
72
|
+
* @typeParam TComponent - The Discord.js component type being wrapped
|
|
73
|
+
*
|
|
74
|
+
* @internal
|
|
75
|
+
*/
|
|
76
|
+
declare abstract class BaseComponent<TComponent> {
|
|
77
|
+
private readonly _component;
|
|
78
|
+
protected constructor(ComponentClass: new () => TComponent);
|
|
79
|
+
/**
|
|
80
|
+
* Returns the live builder, ready to send in a Discord message or nest in another component.
|
|
81
|
+
*
|
|
82
|
+
* Configure it through `this.instance`, not here. Reading this can apply the bot color (see
|
|
83
|
+
* {@link BuilderComponent}), so a read is not side-effect free.
|
|
84
|
+
* @example new SomeComponent().component
|
|
85
|
+
*/
|
|
86
|
+
abstract get component(): InstantiatedBuilder<BuilderType> | InstantiatedActionRow<RowType>;
|
|
87
|
+
/**
|
|
88
|
+
* The wrapped builder, for calling Discord.js methods like setTitle() and setDescription() inside a subclass.
|
|
89
|
+
*
|
|
90
|
+
* @example this.instance.setTitle('My Modal')
|
|
91
|
+
*/
|
|
92
|
+
protected get instance(): TComponent;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Base class for Discord.js builder components
|
|
96
|
+
*
|
|
97
|
+
* Wraps Discord.js builders (SlashCommandBuilder, EmbedBuilder, etc.) with
|
|
98
|
+
* Seedcord-specific defaults and helper methods.
|
|
99
|
+
*
|
|
100
|
+
* @typeParam BuilderKey - The type of Discord.js builder being wrapped
|
|
101
|
+
*/
|
|
102
|
+
declare abstract class BuilderComponent<BuilderKey extends BuilderType> extends BaseComponent<InstantiatedBuilder<BuilderKey>> {
|
|
103
|
+
readonly type: BuilderKey;
|
|
104
|
+
private colorApplied;
|
|
105
|
+
protected constructor(type: BuilderKey);
|
|
106
|
+
get component(): InstantiatedBuilder<BuilderKey>;
|
|
107
|
+
private applyBotColor;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Base class for Discord action row components
|
|
111
|
+
*
|
|
112
|
+
* Wraps Discord.js action row builder with Seedcord-specific defaults and helper methods.
|
|
113
|
+
*
|
|
114
|
+
* @typeParam RowKey - The Discord.js action row type being wrapped
|
|
115
|
+
*/
|
|
116
|
+
declare abstract class RowComponent<RowKey extends RowType> extends BaseComponent<InstantiatedActionRow<RowKey>> {
|
|
117
|
+
readonly type: RowKey;
|
|
118
|
+
protected constructor(type: RowKey);
|
|
119
|
+
get component(): InstantiatedActionRow<RowKey>;
|
|
120
|
+
}
|
|
121
|
+
//#endregion
|
|
122
|
+
//#region src/customId/Field.d.ts
|
|
123
|
+
/**
|
|
124
|
+
* One field in a customId shape, its wire kind plus the type it decodes to.
|
|
125
|
+
*
|
|
126
|
+
* @internal
|
|
127
|
+
*/
|
|
128
|
+
interface CustomIdField<Decoded> {
|
|
129
|
+
/** Which wire encoding this field uses. */
|
|
130
|
+
readonly kind: 'snowflake' | 'uuid' | 'int' | 'bool' | 'oneOf' | 'string';
|
|
131
|
+
/** Lower bound, for a bounded int field. */
|
|
132
|
+
readonly min?: number;
|
|
133
|
+
/** Upper bound, for a bounded int field. */
|
|
134
|
+
readonly max?: number;
|
|
135
|
+
/** The allowed values, for a oneOf field. */
|
|
136
|
+
readonly choices?: readonly string[];
|
|
137
|
+
/** Phantom only, carries the decoded type and is never set at runtime. */
|
|
138
|
+
readonly decoded?: Decoded;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The set of fields a customId carries, keyed by name.
|
|
142
|
+
*
|
|
143
|
+
* @internal
|
|
144
|
+
*/
|
|
145
|
+
type CustomIdShape = Record<string, CustomIdField<unknown>>;
|
|
146
|
+
/**
|
|
147
|
+
* The decoded result, each field name mapped to its decoded type.
|
|
148
|
+
*
|
|
149
|
+
* @internal
|
|
150
|
+
*/
|
|
151
|
+
type DecodedParams<Shape extends CustomIdShape> = { [Name in keyof Shape]: Shape[Name] extends CustomIdField<infer Decoded> ? Decoded : never };
|
|
152
|
+
//#endregion
|
|
153
|
+
//#region ../../node_modules/.pnpm/type-fest@5.7.0/node_modules/type-fest/source/non-empty-tuple.d.ts
|
|
154
|
+
/**
|
|
155
|
+
Matches any non-empty tuple.
|
|
156
|
+
|
|
157
|
+
@example
|
|
158
|
+
```
|
|
159
|
+
import type {NonEmptyTuple} from 'type-fest';
|
|
160
|
+
|
|
161
|
+
const sum = (...numbers: NonEmptyTuple<number>) => numbers.reduce((total, value) => total + value, 0);
|
|
162
|
+
|
|
163
|
+
sum(1, 2, 3);
|
|
164
|
+
// Ok
|
|
165
|
+
|
|
166
|
+
// @ts-expect-error
|
|
167
|
+
sum();
|
|
168
|
+
// Error: Expected at least 1 arguments, but got 0.
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
@see {@link RequireAtLeastOne} for objects
|
|
172
|
+
|
|
173
|
+
@category Array
|
|
174
|
+
*/
|
|
175
|
+
type NonEmptyTuple<T = unknown> = readonly [T, ...T[]];
|
|
176
|
+
//#endregion
|
|
177
|
+
//#region src/customId/CustomId.d.ts
|
|
178
|
+
/** Strip the layout hash off the routeKey to recover the stable prefix the controller routes by. @internal */
|
|
179
|
+
declare function prefixOf(wire: string): string;
|
|
180
|
+
/**
|
|
181
|
+
* A typed customId. The single source of truth shared by the component that mints it and the handler
|
|
182
|
+
* that reads it. This gives you typed reads on the `.customId` field in components. Values are packed into a compact wire string rather than plain stringified tokens, so the 100-char Discord limit goes further. More string per string, basically.
|
|
183
|
+
*
|
|
184
|
+
* @typeParam Prefix - The stable route prefix, e.g. 'approve'.
|
|
185
|
+
* @typeParam Shape - The accumulated fields, filled in by the chain.
|
|
186
|
+
*
|
|
187
|
+
* @example
|
|
188
|
+
* ```ts
|
|
189
|
+
* const ApproveId = new CustomId('approve')
|
|
190
|
+
* .snowflake('userId')
|
|
191
|
+
* .oneOf('action', ['approve', 'deny']);
|
|
192
|
+
*
|
|
193
|
+
* // Set the custom id on a button when creating it.
|
|
194
|
+
* new ButtonBuilder().setCustomId(ApproveId.encode({ userId: '123', action: 'approve' }));
|
|
195
|
+
*
|
|
196
|
+
* // reading in the handler: userId comes back a string
|
|
197
|
+
* const { userId, action } = this.params; // userId: string, action: 'approve' | 'deny'
|
|
198
|
+
* await this.event.guild?.members.fetch(userId);
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
declare class CustomId<Prefix extends string, Shape extends CustomIdShape = {}> {
|
|
202
|
+
readonly prefix: Prefix;
|
|
203
|
+
readonly shape: Shape;
|
|
204
|
+
/** The prefix plus a short hash of the shape, the part of the wire before the colon. */
|
|
205
|
+
readonly routeKey: string;
|
|
206
|
+
constructor(prefix: Prefix, shape?: Shape);
|
|
207
|
+
private add;
|
|
208
|
+
/**
|
|
209
|
+
* Add a Discord ID field, decoded as a string (the discord.js `Snowflake` type).
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```ts
|
|
213
|
+
* new CustomId('ban').snowflake('userId');
|
|
214
|
+
* ```
|
|
215
|
+
*/
|
|
216
|
+
snowflake<Name extends string>(name: Name): CustomId<Prefix, Shape & Record<Name, CustomIdField<Snowflake>>>;
|
|
217
|
+
/**
|
|
218
|
+
* Add a UUID field, decoded as a lowercase uuid string.
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```ts
|
|
222
|
+
* new CustomId('ticket').uuid('ticketId');
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
225
|
+
uuid<Name extends string>(name: Name): CustomId<Prefix, Shape & Record<Name, CustomIdField<string>>>;
|
|
226
|
+
/**
|
|
227
|
+
* Add an integer field with no bounds, for a value up to 2^53.
|
|
228
|
+
*
|
|
229
|
+
* @example
|
|
230
|
+
* ```ts
|
|
231
|
+
* new CustomId('shop').int('amount');
|
|
232
|
+
* ```
|
|
233
|
+
*/
|
|
234
|
+
int<Name extends string>(name: Name): CustomId<Prefix, Shape & Record<Name, CustomIdField<number>>>;
|
|
235
|
+
/**
|
|
236
|
+
* Add an integer field bounded by min and max, so it packs into fewer characters on the wire.
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* ```ts
|
|
240
|
+
* new CustomId('paginate').int('page', 1, 50);
|
|
241
|
+
* ```
|
|
242
|
+
*/
|
|
243
|
+
int<Name extends string>(name: Name, min: number, max: number): CustomId<Prefix, Shape & Record<Name, CustomIdField<number>>>;
|
|
244
|
+
/**
|
|
245
|
+
* Add a boolean flag.
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```ts
|
|
249
|
+
* new CustomId('settings').bool('silent');
|
|
250
|
+
* ```
|
|
251
|
+
*/
|
|
252
|
+
bool<Name extends string>(name: Name): CustomId<Prefix, Shape & Record<Name, CustomIdField<boolean>>>;
|
|
253
|
+
/**
|
|
254
|
+
* Add a field that is one value from a fixed list, decoded as the literal union. No `as const` needed.
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* ```ts
|
|
258
|
+
* new CustomId('poll').oneOf('choice', ['yes', 'no', 'abstain']);
|
|
259
|
+
* ```
|
|
260
|
+
*/
|
|
261
|
+
oneOf<Name extends string, const Choices extends NonEmptyTuple<string>>(name: Name, choices: Choices): CustomId<Prefix, Shape & Record<Name, CustomIdField<Choices[number]>>>;
|
|
262
|
+
/**
|
|
263
|
+
* Add a free short text field. Avoid it where possible, it cannot be packed so it costs the most wire space.
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* ```ts
|
|
267
|
+
* new CustomId('note').str('message');
|
|
268
|
+
* ```
|
|
269
|
+
*/
|
|
270
|
+
str<Name extends string>(name: Name): CustomId<Prefix, Shape & Record<Name, CustomIdField<string>>>;
|
|
271
|
+
/**
|
|
272
|
+
* Mint a wire string from values. Throws if a value is out of its field's range or the wire is over 100 chars.
|
|
273
|
+
*
|
|
274
|
+
* @param values - One value per field, typed by the chain.
|
|
275
|
+
* @returns The wire string to put on the component's customId.
|
|
276
|
+
*/
|
|
277
|
+
encode(values: DecodedParams<Shape>): string;
|
|
278
|
+
/**
|
|
279
|
+
* Read a wire string back into values.
|
|
280
|
+
*
|
|
281
|
+
* @param wire - The customId string from the interaction.
|
|
282
|
+
* @returns The decoded values, typed by the chain.
|
|
283
|
+
* @throws A {@link StaleCustomId} when the shape changed since the wire was minted.
|
|
284
|
+
* @throws An {@link InvalidCustomId} on a corrupt or foreign wire.
|
|
285
|
+
*/
|
|
286
|
+
decode(wire: string): DecodedParams<Shape>;
|
|
287
|
+
/** True if this wire was minted from this customId's prefix, ignoring the shape hash. */
|
|
288
|
+
owns(wire: string): boolean;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Any customId, for places where the exact prefix and shape do not matter.
|
|
292
|
+
*
|
|
293
|
+
* @internal
|
|
294
|
+
*/
|
|
295
|
+
type AnyCustomId = CustomId<string, CustomIdShape>;
|
|
296
|
+
/**
|
|
297
|
+
* The outcome of decoding a wire against several customIds, the matched prefix paired with its values.
|
|
298
|
+
*
|
|
299
|
+
* @internal
|
|
300
|
+
*/
|
|
301
|
+
type DecodedRoute<Defs extends readonly AnyCustomId[]> = { [Index in keyof Defs]: Defs[Index] extends AnyCustomId ? {
|
|
302
|
+
readonly prefix: Defs[Index]['prefix'];
|
|
303
|
+
readonly params: DecodedParams<Defs[Index]['shape']>;
|
|
304
|
+
} : never }[number];
|
|
305
|
+
/**
|
|
306
|
+
* Find the customId whose prefix owns this wire, decode against it, and report which one matched.
|
|
307
|
+
*
|
|
308
|
+
* @internal
|
|
309
|
+
*/
|
|
310
|
+
declare function decodeFor<Defs extends readonly AnyCustomId[]>(defs: Defs, wire: string): DecodedRoute<Defs>;
|
|
311
|
+
//#endregion
|
|
312
|
+
export { DecodedParams as a, BuilderType as c, prefixOf as i, RowType as l, CustomId as n, BuilderComponent as o, decodeFor as r, RowComponent as s, AnyCustomId as t };
|
|
313
|
+
//# sourceMappingURL=CustomId-CbTZuUup.d.mts.map
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_CustomId = require('./CustomId-BuIoGHXw.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/stops/Fault.ts
|
|
5
|
+
/**
|
|
6
|
+
* A generic fault you throw after catching an error you do not have a specific message for.
|
|
7
|
+
*
|
|
8
|
+
* The user sees a fixed generic reply with the tracking uuid, never the cause. `report` defaults to
|
|
9
|
+
* `true`, so the framework logs it and publishes it to the `handledException` bus. Pass `report: false`
|
|
10
|
+
* to show the generic reply without the bus publish. The original error is stored as the standard
|
|
11
|
+
* `cause`, so the real stack reaches the webhook. For a fault the user should see a real message,
|
|
12
|
+
* subclass {@link Notice} and write your own `render` instead.
|
|
13
|
+
*
|
|
14
|
+
* The framework also renders this for an unhandled throw, where it points the user at
|
|
15
|
+
* `ctx.developerUsername`.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { Fault } from '@seedcord/kit';
|
|
20
|
+
*
|
|
21
|
+
* try {
|
|
22
|
+
* await db.write(record);
|
|
23
|
+
* } catch (cause) {
|
|
24
|
+
* // user sees the generic reply with the uuid, the real error rides along as cause for the webhook
|
|
25
|
+
* throw new Fault({ cause });
|
|
26
|
+
*
|
|
27
|
+
* // pass report: false to show the same reply without publishing to the handledException bus
|
|
28
|
+
* // throw new Fault({ cause, report: false });
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
var Fault = class extends require_CustomId.Notice {
|
|
33
|
+
constructor(options) {
|
|
34
|
+
super("A fault occurred", options?.cause === void 0 ? void 0 : { cause: options.cause });
|
|
35
|
+
this.report = options?.report ?? true;
|
|
36
|
+
}
|
|
37
|
+
render(ctx) {
|
|
38
|
+
return { components: [new require_CustomId.NoticeCard(`Something went wrong. Please reach out to ${ctx.developerUsername ?? "the developer"} with a way to reproduce the error and the following:\n### UUID: \`${ctx.uuid}\``, "Error").component] };
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/stops/Silence.ts
|
|
44
|
+
/**
|
|
45
|
+
* Throw to stop a handler with no reply and no report.
|
|
46
|
+
*
|
|
47
|
+
* The framework boundary catches `Silence` before {@link Notice}, makes zero Discord calls, and stops.
|
|
48
|
+
* Ideally you'd only throw this in `EventHandlers` (or `Gates` for those), because it doesn't make sense to
|
|
49
|
+
* leave the user with no reply for an interaction.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* import { Silence } from '@seedcord/kit';
|
|
54
|
+
*
|
|
55
|
+
* // before any reply or defer, drop the interaction with no reply and no report
|
|
56
|
+
* if (await isBlacklisted(interaction.user.id)) throw new Silence('blacklisted user');
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
var Silence = class extends Error {
|
|
60
|
+
reason;
|
|
61
|
+
/**
|
|
62
|
+
* @param reason - Optional note written only to a debug log, never shown to the user or reported.
|
|
63
|
+
*/
|
|
64
|
+
constructor(reason) {
|
|
65
|
+
super(reason ?? "Silence");
|
|
66
|
+
this.reason = reason;
|
|
67
|
+
Error.captureStackTrace(this, this.constructor);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/index.ts
|
|
73
|
+
/** Package version */
|
|
74
|
+
const version = "0.0.1";
|
|
75
|
+
|
|
76
|
+
//#endregion
|
|
77
|
+
exports.BuilderComponent = require_CustomId.BuilderComponent;
|
|
78
|
+
exports.CustomId = require_CustomId.CustomId;
|
|
79
|
+
exports.Fault = Fault;
|
|
80
|
+
exports.Notice = require_CustomId.Notice;
|
|
81
|
+
exports.RowComponent = require_CustomId.RowComponent;
|
|
82
|
+
exports.Silence = Silence;
|
|
83
|
+
exports.version = version;
|
|
84
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["Notice","NoticeCard"],"sources":["../src/stops/Fault.ts","../src/stops/Silence.ts","../src/index.ts"],"sourcesContent":["import { Notice } from './Notice';\nimport { NoticeCard } from './NoticeCard';\n\nimport type { RenderContext, ReplyResponse } from '@seedcord/types';\n\n/**\n * A generic fault you throw after catching an error you do not have a specific message for.\n *\n * The user sees a fixed generic reply with the tracking uuid, never the cause. `report` defaults to\n * `true`, so the framework logs it and publishes it to the `handledException` bus. Pass `report: false`\n * to show the generic reply without the bus publish. The original error is stored as the standard\n * `cause`, so the real stack reaches the webhook. For a fault the user should see a real message,\n * subclass {@link Notice} and write your own `render` instead.\n *\n * The framework also renders this for an unhandled throw, where it points the user at\n * `ctx.developerUsername`.\n *\n * @example\n * ```ts\n * import { Fault } from '@seedcord/kit';\n *\n * try {\n * await db.write(record);\n * } catch (cause) {\n * // user sees the generic reply with the uuid, the real error rides along as cause for the webhook\n * throw new Fault({ cause });\n *\n * // pass report: false to show the same reply without publishing to the handledException bus\n * // throw new Fault({ cause, report: false });\n * }\n * ```\n */\nexport class Fault extends Notice {\n public constructor(options?: { cause?: unknown; report?: boolean }) {\n super('A fault occurred', options?.cause === undefined ? undefined : { cause: options.cause });\n this.report = options?.report ?? true;\n }\n\n public render(ctx: RenderContext): ReplyResponse {\n const contact = ctx.developerUsername ?? 'the developer';\n const card = new NoticeCard(\n `Something went wrong. Please reach out to ${contact} with a way to reproduce the error and the following:\\n### UUID: \\`${ctx.uuid}\\``,\n 'Error'\n );\n return { components: [card.component] };\n }\n}\n","/**\n * Throw to stop a handler with no reply and no report.\n *\n * The framework boundary catches `Silence` before {@link Notice}, makes zero Discord calls, and stops.\n * Ideally you'd only throw this in `EventHandlers` (or `Gates` for those), because it doesn't make sense to\n * leave the user with no reply for an interaction.\n *\n * @example\n * ```ts\n * import { Silence } from '@seedcord/kit';\n *\n * // before any reply or defer, drop the interaction with no reply and no report\n * if (await isBlacklisted(interaction.user.id)) throw new Silence('blacklisted user');\n * ```\n */\nexport class Silence extends Error {\n /**\n * @param reason - Optional note written only to a debug log, never shown to the user or reported.\n */\n public constructor(public readonly reason?: string) {\n super(reason ?? 'Silence');\n\n Error.captureStackTrace(this, this.constructor);\n }\n}\n","export { BuilderComponent, RowComponent } from '@components/Component';\nexport { type RowType, type BuilderType } from '@components/builderTypes';\nexport { Notice } from '@stops/Notice';\nexport { Fault } from '@stops/Fault';\nexport { Silence } from '@stops/Silence';\nexport { CustomId } from '@customId/CustomId';\n\n/** Package version */\nexport const version = process.env.PACKAGE_VERSION ?? '0.0.0';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCA,IAAa,QAAb,cAA2BA,wBAAO;CAC9B,AAAO,YAAY,SAAiD;EAChE,MAAM,oBAAoB,SAAS,UAAU,SAAY,SAAY,EAAE,OAAO,QAAQ,MAAM,CAAC;EAC7F,KAAK,SAAS,SAAS,UAAU;CACrC;CAEA,AAAO,OAAO,KAAmC;EAM7C,OAAO,EAAE,YAAY,CAAC,IAJLC,4BACb,6CAFY,IAAI,qBAAqB,gBAEgB,qEAAqE,IAAI,KAAK,KACnI,OAEqB,CAAC,CAAC,SAAS,EAAE;CAC1C;AACJ;;;;;;;;;;;;;;;;;;;AC/BA,IAAa,UAAb,cAA6B,MAAM;CAII;;;;CAAnC,AAAO,YAAY,AAAgB,QAAiB;EAChD,MAAM,UAAU,SAAS;EADM;EAG/B,MAAM,kBAAkB,MAAM,KAAK,WAAW;CAClD;AACJ;;;;;AChBA,MAAa"}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type * from './index.d.mts'
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { c as BuilderType, l as RowType, n as CustomId, o as BuilderComponent, s as RowComponent } from "./CustomId-CbTZuUup.mjs";
|
|
2
|
+
import { RenderContext, ReplyResponse } from "@seedcord/types";
|
|
3
|
+
|
|
4
|
+
//#region src/stops/Notice.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Base class for a user-facing refusal or a reported fault.
|
|
7
|
+
*
|
|
8
|
+
* Throw a `Notice` to stop a handler and reply to the user. The framework catches it at the controller
|
|
9
|
+
* boundary and renders {@link Notice.render}, which always decides what the user sees. With `report`
|
|
10
|
+
* false that render is all that happens. With `report` true the framework also logs the fault and
|
|
11
|
+
* publishes it to the `handledException` bus. A raw, non-Notice throw shows the generic message.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { Notice, BuilderComponent, type RenderContext, type ReplyResponse } from 'seedcord';
|
|
16
|
+
* import { TextDisplayBuilder } from 'discord.js';
|
|
17
|
+
*
|
|
18
|
+
* // reading `.component` applies the configured bot color to the container accent
|
|
19
|
+
* class TooPoorCard extends BuilderComponent<'container'> {
|
|
20
|
+
* constructor(balance: number) {
|
|
21
|
+
* super('container');
|
|
22
|
+
* this.instance.addTextDisplayComponents(
|
|
23
|
+
* new TextDisplayBuilder().setContent(`### Insufficient balance\nYou need more than ${balance} coins.`)
|
|
24
|
+
* );
|
|
25
|
+
* }
|
|
26
|
+
* }
|
|
27
|
+
*
|
|
28
|
+
* class TooPoor extends Notice {
|
|
29
|
+
* constructor(private readonly balance: number) {
|
|
30
|
+
* super(`balance ${balance} is below the cost`);
|
|
31
|
+
* }
|
|
32
|
+
*
|
|
33
|
+
* render(_ctx: RenderContext): ReplyResponse {
|
|
34
|
+
* return { components: [new TooPoorCard(this.balance).component] };
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
*
|
|
38
|
+
* // in a handler, throwing stops the handler and replies with render(ctx)
|
|
39
|
+
* if (wallet.balance < cost) throw new TooPoor(wallet.balance);
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
declare abstract class Notice extends Error {
|
|
43
|
+
/**
|
|
44
|
+
* Whether this denial is a reported fault. True also logs it and publishes it to the `handledException`
|
|
45
|
+
* bus. The user always sees {@link Notice.render} either way.
|
|
46
|
+
*
|
|
47
|
+
* @defaultValue `false`
|
|
48
|
+
*/
|
|
49
|
+
report: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Whether the reply is ephemeral, so only the invoking user sees it. Set it false for a refusal the
|
|
52
|
+
* whole channel should see.
|
|
53
|
+
*
|
|
54
|
+
* @defaultValue `true`
|
|
55
|
+
*/
|
|
56
|
+
ephemeral: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* A short one-line reason. When every arm of an `or` gate refuses and each refusal sets this, `or`
|
|
59
|
+
* lists them instead of showing a neutral message.
|
|
60
|
+
*/
|
|
61
|
+
summary?: string;
|
|
62
|
+
protected constructor(message: string, options?: ErrorOptions);
|
|
63
|
+
/**
|
|
64
|
+
* Builds what the user sees. Called fresh each time the denial is shown, so the builders are new
|
|
65
|
+
* and the bot color resolves at render time rather than at construction time.
|
|
66
|
+
*/
|
|
67
|
+
abstract render(ctx: RenderContext): ReplyResponse;
|
|
68
|
+
}
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/stops/Fault.d.ts
|
|
71
|
+
/**
|
|
72
|
+
* A generic fault you throw after catching an error you do not have a specific message for.
|
|
73
|
+
*
|
|
74
|
+
* The user sees a fixed generic reply with the tracking uuid, never the cause. `report` defaults to
|
|
75
|
+
* `true`, so the framework logs it and publishes it to the `handledException` bus. Pass `report: false`
|
|
76
|
+
* to show the generic reply without the bus publish. The original error is stored as the standard
|
|
77
|
+
* `cause`, so the real stack reaches the webhook. For a fault the user should see a real message,
|
|
78
|
+
* subclass {@link Notice} and write your own `render` instead.
|
|
79
|
+
*
|
|
80
|
+
* The framework also renders this for an unhandled throw, where it points the user at
|
|
81
|
+
* `ctx.developerUsername`.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* import { Fault } from '@seedcord/kit';
|
|
86
|
+
*
|
|
87
|
+
* try {
|
|
88
|
+
* await db.write(record);
|
|
89
|
+
* } catch (cause) {
|
|
90
|
+
* // user sees the generic reply with the uuid, the real error rides along as cause for the webhook
|
|
91
|
+
* throw new Fault({ cause });
|
|
92
|
+
*
|
|
93
|
+
* // pass report: false to show the same reply without publishing to the handledException bus
|
|
94
|
+
* // throw new Fault({ cause, report: false });
|
|
95
|
+
* }
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
declare class Fault extends Notice {
|
|
99
|
+
constructor(options?: {
|
|
100
|
+
cause?: unknown;
|
|
101
|
+
report?: boolean;
|
|
102
|
+
});
|
|
103
|
+
render(ctx: RenderContext): ReplyResponse;
|
|
104
|
+
}
|
|
105
|
+
//#endregion
|
|
106
|
+
//#region src/stops/Silence.d.ts
|
|
107
|
+
/**
|
|
108
|
+
* Throw to stop a handler with no reply and no report.
|
|
109
|
+
*
|
|
110
|
+
* The framework boundary catches `Silence` before {@link Notice}, makes zero Discord calls, and stops.
|
|
111
|
+
* Ideally you'd only throw this in `EventHandlers` (or `Gates` for those), because it doesn't make sense to
|
|
112
|
+
* leave the user with no reply for an interaction.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```ts
|
|
116
|
+
* import { Silence } from '@seedcord/kit';
|
|
117
|
+
*
|
|
118
|
+
* // before any reply or defer, drop the interaction with no reply and no report
|
|
119
|
+
* if (await isBlacklisted(interaction.user.id)) throw new Silence('blacklisted user');
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
declare class Silence extends Error {
|
|
123
|
+
readonly reason?: string | undefined;
|
|
124
|
+
/**
|
|
125
|
+
* @param reason - Optional note written only to a debug log, never shown to the user or reported.
|
|
126
|
+
*/
|
|
127
|
+
constructor(reason?: string | undefined);
|
|
128
|
+
}
|
|
129
|
+
//#endregion
|
|
130
|
+
//#region src/index.d.ts
|
|
131
|
+
/** Package version */
|
|
132
|
+
declare const version: string;
|
|
133
|
+
//#endregion
|
|
134
|
+
export { BuilderComponent, type BuilderType, CustomId, Fault, Notice, RowComponent, type RowType, Silence, version };
|
|
135
|
+
//# sourceMappingURL=index.d.mts.map
|