fix: tighten command arg value types

This commit is contained in:
Peter Steinberger
2026-01-15 17:08:09 +00:00
parent 01c43b0b0c
commit 05658b6609
3 changed files with 37 additions and 2 deletions

View File

@@ -28,7 +28,8 @@ export type CommandArgMenuSpec = {
title?: string;
};
export type CommandArgValues = Record<string, unknown>;
export type CommandArgValue = string | number | boolean | bigint;
export type CommandArgValues = Record<string, CommandArgValue>;
export type CommandArgs = {
raw?: string;

View File

@@ -0,0 +1,34 @@
import { describe, expect, it } from "vitest";
import { applyTemplate, type TemplateContext } from "./templating.js";
describe("applyTemplate", () => {
it("renders primitive values", () => {
const ctx = { MessageSid: "sid", IsNewSession: "no" } as TemplateContext;
const overrides = ctx as Record<string, unknown>;
overrides.MessageSid = 42;
overrides.IsNewSession = true;
expect(applyTemplate("sid={{MessageSid}} new={{IsNewSession}}", ctx)).toBe(
"sid=42 new=true",
);
});
it("renders arrays of primitives", () => {
const ctx = { MediaPaths: ["a"] } as TemplateContext;
(ctx as Record<string, unknown>).MediaPaths = ["a", 2, true, null, { ok: false }];
expect(applyTemplate("paths={{MediaPaths}}", ctx)).toBe("paths=a,2,true");
});
it("drops object values", () => {
const ctx: TemplateContext = { CommandArgs: { raw: "go" } };
expect(applyTemplate("args={{CommandArgs}}", ctx)).toBe("args=");
});
it("renders missing placeholders as empty", () => {
const ctx: TemplateContext = {};
expect(applyTemplate("missing={{Missing}}", ctx)).toBe("missing=");
});
});

View File

@@ -118,7 +118,7 @@ function readDiscordCommandArgs(
if (!definitions || definitions.length === 0) return undefined;
const values: CommandArgValues = {};
for (const definition of definitions) {
let value: unknown;
let value: string | number | boolean | null;
if (definition.type === "number") {
value = interaction.options.getNumber(definition.name);
} else if (definition.type === "boolean") {