/dice always rolls six sides. Let people choose: /dice sides:20. That is an option: something the person fills in after the name of the command, which Discord asks for in its menu and sends to your bot with the command.
Declaring an option
Addoptions to the command. Each key is the name of an option, each value says what it is:
src/commands/dice.ts
/dice again: Discord now offers sides, refuses a value under 2 or over 100 before it even reaches your bot, and lets you leave it out.
Three things to notice in run:
optionsis new in the objectrunreceives. It holds what the person filled in, under the names you declared.options.sidesis a number, and the editor knows it. Hover it:number | undefined. You declaredtype: 'integer', so the framework typed it for you. Nothing to convert, nothing to check.undefinedis there because the option is optional. An option is optional unless it saysrequired: true, and your code has to handle its absence:?? 6does. Remove the?? 6and the editor underlinessidesinMath.random() * sides: “possibly undefined”. That is the point of the types: the mistake is caught in the editor, not by a person using the command.
Every kind of option
type says what Discord asks for, and decides what run receives:
Every option has a
type and a description (1 to 100 characters, shown under its name in the menu). Names follow the same rules as command names: lowercase, letters, digits, - and _.
A user option: /whois
An option of typeuser gives you the person as an object, not as a text. Create src/commands/whois.ts:
src/commands/whois.ts
required: true means Discord refuses to send the command without it, so options.who is always there: hover it, it is a User, with no undefined. A User has a name, an avatar, the date the account was created, and more: the next page is about what these objects give you.
Choices
When the possible values are known in advance,choices turns the option into a list the person picks from, and the value your code receives is typed as one of the choices:
src/commands/dice.ts
choices: ['red', 'green']) uses the same text for both. Between 1 and 25 choices.
Suggestions while typing
choices needs the values in advance. When they come from somewhere else, or change, autocomplete lets your bot suggest them while the person types: Discord asks your function every few keystrokes, and shows what it returns. Let /dice know the dice of a few games:
src/commands/dice-game.ts
{ name, value } to show one text and receive another. Discord shows 25 at most, and waits 3 seconds for them.
A suggestion is only a help: the person may type something else, and run receives what they typed. Check it, when it matters.
Points of attention
- Required options first. Discord wants them before the optional ones. You can write them in any order: the framework sorts them for you.
- Only the person’s input is validated by Discord.
min,max,minLength,maxLengthandchoicesare enforced by Discord before your code runs. What you do with the value is yours to check. - 25 options at most per command. If you need more, you probably want several commands.
- A mistake in a declaration is caught when the file loads, with a message saying what to write. A
typethat does not exist, a field the type does not have, aminabove themax: each has its own message, and the editor underlines most of them first.
Recap
optionsdeclares what people fill in;runreceivesoptions.name, typed from the declaration.- Optional by default: handle
undefined, or sayrequired: true. user,roleandchanneloptions give you objects, not ids.choicesmakes a list, and types the value as one of the choices.autocompletesuggests values while the person types, from your own code.
Answering
Private answers, slow answers, and what the person sees when your code
fails.
Guide: Options
Every field of every kind of option.