Stricter type checking for string command choices

Hi, was wondering if this is possible. In this example I am doing a command that can change my bot's status from Online to Invisible/Idle/Dnd. I want to ensure that the value choices in addChoices have the PresenceStatusData type in case the string values are changed in a future API.
import { ApplicationCommandRegistry, Command } from "@sapphire/framework";
import { ChatInputCommandInteraction, PresenceStatusData } from "discord.js";

// ...

public override registerApplicationCommands(registry: ApplicationCommandRegistry) {
registry.registerChatInputCommand(builder =>
builder
.setName(this.name)
.setDescription(this.description)
.addStringOption(option =>
option
.setName("status")
.setDescription("Online status")
.setRequired(true)
.addChoices( // possible to do stricter type checking for value?
{ name: "Online", value: "online" },
{ name: "Idle", value: "idle" },
{ name: "Do Not Disturb", value: "dnd" },
{ name: "Offline", value: "invisible" },
)
)
);
}
import { ApplicationCommandRegistry, Command } from "@sapphire/framework";
import { ChatInputCommandInteraction, PresenceStatusData } from "discord.js";

// ...

public override registerApplicationCommands(registry: ApplicationCommandRegistry) {
registry.registerChatInputCommand(builder =>
builder
.setName(this.name)
.setDescription(this.description)
.addStringOption(option =>
option
.setName("status")
.setDescription("Online status")
.setRequired(true)
.addChoices( // possible to do stricter type checking for value?
{ name: "Online", value: "online" },
{ name: "Idle", value: "idle" },
{ name: "Do Not Disturb", value: "dnd" },
{ name: "Offline", value: "invisible" },
)
)
);
}
Solution:
or just type assert
Jump to solution
4 Replies
Oreo ™
Oreo ™7mo ago
you can use a type validation library like @sapphire/shapeshift or zod
Solution
Oreo ™
Oreo ™7mo ago
or just type assert
Oreo ™
Oreo ™7mo ago
... as 'online' | 'dnd' ... since u cant choose something that isnt in the options
Favna
Favna7mo ago
Yeah I'd do the second. Just type cast it.
Want results from more Discord servers?
Add your server