Nubis / Options
Construire
Options
Command options, the types Nubis infers from them, choices, limits and autocomplete.
La documentation de Nubis est en anglais, comme l’API du framework et ses messages.
Options are declared by name, and their values arrive typed. There is no getString("name"): the name you wrote is the property you read.
options: {
user: option.user({ required: true }),
reason: option.string({ maxLength: 512 }),
days: option.integer({
choices: [
{ name: "One day", value: 1 },
{ name: "A week", value: 7 },
],
}),
},
run: ({ options }) => {
options.user; // APIUser
options.reason; // string | undefined
options.days; // 1 | 7 | undefined
},Kinds
| Function | Value |
|---|---|
option.string() | string |
option.integer() | number, whole |
option.number() | number |
option.boolean() | boolean |
option.user() | APIUser |
option.channel() | The resolved channel |
option.role() | APIRole |
Required and optional
required: true makes the value its type; otherwise it can be undefined. Nubis orders required options before optional ones when it registers the command, as Discord requires, so you can declare them in whatever order reads best.
Settings
Every kind takes description (defaults to the option's name) and required. Then:
| Setting | Kinds | What it does |
|---|---|---|
choices | string, integer, number | A fixed list. The value's type becomes the union of the choice values. |
minLength, maxLength | string | Length limits Discord enforces in its picker. |
minValue, maxValue | integer, number | Range limits. |
channelTypes | channel | Which kinds of channel can be picked. |
autocomplete | string, integer, number | Suggests values as the user types. |
Choices become types
A choice list narrows the value to exactly its values, so a switch over them is checked by TypeScript:
options: {
size: option.string({
required: true,
choices: [
{ name: "Small", value: "s" },
{ name: "Large", value: "l" },
],
}),
},
run: ({ options, reply }) => {
// options.size: "s" | "l"
switch (options.size) {
case "s":
return reply("Small it is.");
case "l":
return reply("Large it is.");
}
},Autocomplete
autocomplete returns suggestions for what the user has typed so far; Nubis sends Discord the first 25. Unlike choices, the user can still send something that wasn't suggested, so the value's type stays string (or number).
const FRUIT = ["apple", "apricot", "banana", "blueberry", "cherry"];
options: {
fruit: option.string({
required: true,
autocomplete: ({ value }) =>
FRUIT.filter((name) => name.startsWith(value.toLowerCase()))
.slice(0, 25)
.map((name) => ({ name, value: name })),
}),
},The handler gets value (always a string while typing), the other options as typed so far, interaction and api. An option can have choices or autocomplete, not both.
Checked when you define them
Nubis applies Discord's rules when the command is loaded, with the command and option named in the error:
NubisError: /mod ban option "days": integer choices must be whole numbers.It checks names, description lengths, at most 25 options and 25 choices, choice name and value lengths, minLength against maxLength and minValue against maxValue.