fix: tighten command arg value types
This commit is contained in:
@@ -28,7 +28,8 @@ export type CommandArgMenuSpec = {
|
|||||||
title?: string;
|
title?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CommandArgValues = Record<string, unknown>;
|
export type CommandArgValue = string | number | boolean | bigint;
|
||||||
|
export type CommandArgValues = Record<string, CommandArgValue>;
|
||||||
|
|
||||||
export type CommandArgs = {
|
export type CommandArgs = {
|
||||||
raw?: string;
|
raw?: string;
|
||||||
|
|||||||
34
src/auto-reply/templating.test.ts
Normal file
34
src/auto-reply/templating.test.ts
Normal 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=");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -118,7 +118,7 @@ function readDiscordCommandArgs(
|
|||||||
if (!definitions || definitions.length === 0) return undefined;
|
if (!definitions || definitions.length === 0) return undefined;
|
||||||
const values: CommandArgValues = {};
|
const values: CommandArgValues = {};
|
||||||
for (const definition of definitions) {
|
for (const definition of definitions) {
|
||||||
let value: unknown;
|
let value: string | number | boolean | null;
|
||||||
if (definition.type === "number") {
|
if (definition.type === "number") {
|
||||||
value = interaction.options.getNumber(definition.name);
|
value = interaction.options.getNumber(definition.name);
|
||||||
} else if (definition.type === "boolean") {
|
} else if (definition.type === "boolean") {
|
||||||
|
|||||||
Reference in New Issue
Block a user