# Commands, menus and settings

> Name the actions your code carries out, place them in Songstress's menus, and declare the settings Songstress draws for you.

Page: https://songstress.app/docs/develop/commands/

## Commands [#commands]

A command is a named action your code carries out. Declare it, then register its handler under the same id in the
entry `run` names. Up to 50.

A named action the extension's code carries out, run from a menu, a button or another command.

| Field | Type | Description |
|---|---|---|
| `id` | `string` | The command's name. Code registers its handler under it. |
| `title` | `string` | The label on the menu item or button that runs it. |
| `run` | `Side` | The entry that holds the command's handler. |
| `description?` | `string \| undefined` | What the command does. |
| `icon?` | `string \| undefined` | The icon beside the title. |

```ts title="src/server.ts"
import { songstress } from 'songstress/server'

songstress.commands.register('test-connection', async (_input, context) => {
  const token = await songstress.settings.get<string>('token', { userId: context.userId })
  return { action: { type: 'toast', message: token ? 'Connected.' : 'Add your token first.', tone: token ? 'success' : 'warning' } }
})
```

A handler is handed the command's input (the song, album or artist a menu was opened on, a form's values, the
answer to a question) and its context: who ran it (`userId`), from where (`source`: `menu`, `action`, `form`,
`settings`, `pick`, `input` or `code`), and in which app (`client`). It may answer with an `action` for the app to
carry out, and a `value` for code that called it.

What a command's handler is told beside its input.

| Field | Type | Description |
|---|---|---|
| `userId` | `string` | The member who ran it. |
| `source` | `"action" \| "code" \| "form" \| "input" \| "menu" \| "pick" \| "settings"` | How it was run: from a menu, by an action of a page, by a form, by a button of the settings page, by a pick or an input action with the person's answer, or by client code (`commands.execute`). |
| `location?` | `keyof Menus \| undefined` | The menu it was picked from, when it was. |
| `client?` | `ClientInfo \| undefined` | The app it was run from, when a person ran it. |
| `values?` | `FormValues \| undefined` | The form's fields, when a form ran it. |
| `answer?` | `string \| string[] \| undefined` | The person's answer, when a pick or an input action ran it: the id chosen (the ids, for a pick of several), or the text written. |

What a command leaves behind. Both parts are optional.

| Field | Type | Description |
|---|---|---|
| `action?` | `Action \| undefined` | What the app does next, when a person ran the command. |
| `value?` | `unknown` | What `commands.execute` resolves to, when code ran it. JSON. |

**Commands cross sides.** Client code may run one of the extension's server commands with
`songstress.commands.execute(id, input)` and gets back the `value` its handler left: that is how the two halves of an
extension talk. Server code has no `execute`; it calls its own functions.

## Menus [#menus]

`menus` places commands, keyed by location. Each location is a list of up to ten `{ "command": "<id>" }` items, each
naming a declared command (`MENU_COMMAND_UNKNOWN`), none twice in one menu (`DUPLICATE_ID`). An item may have a
`when`: a [condition](/docs/develop/interface/#conditions) it shows under.

```json title="songstress.json"
"menus": {
  "song/context": [{ "command": "add-to-chart", "when": "signedIn && !favorite" }],
  "player/actions": [{ "command": "open-visualiser", "when": "views && platform != 'ios'" }]
}
```

Where the extension's commands appear, by menu.

| Field | Type | Description |
|---|---|---|
| `song/context?` | `MenuItems \| undefined` | A song's menu. The command receives the song. |
| `album/context?` | `MenuItems \| undefined` | An album's menu. The command receives the album. |
| `artist/context?` | `MenuItems \| undefined` | An artist's menu. The command receives the artist. |
| `playlist/context?` | `MenuItems \| undefined` | A playlist's menu. The command receives the playlist. |
| `queue/item?` | `MenuItems \| undefined` | The menu of a song waiting in the queue. The command receives the song. |
| `player/actions?` | `MenuItems \| undefined` | The player's own menu. The command receives the song that is playing. |
| `sidebar?` | `MenuItems \| undefined` | The sidebar of the web and desktop apps. |
| `settings/actions?` | `MenuItems \| undefined` | Buttons on the extension's own settings page. |

Each item:

One menu item: a command, and when it shows.

| Field | Type | Description |
|---|---|---|
| `command` | `string` | The id of one of the extension's commands. |
| `when?` | `string \| undefined` | The item shows only while this holds. |

On phones, an extension's items in a menu sit together in one "Extensions" submenu, and there is no sidebar.

## Settings [#settings]

Songstress draws and keeps your settings for you: a list, in the order they're shown, of up to 50. Every setting has a
`key` (the name code reads it by), a `type`, a `title`, an optional `description`, and a `scope`.

| `scope` | Whose it is | Who reads it |
| --- | --- | --- |
| `server` | The whole server's; the admin sets it | The server entry, and client code unless it is a secret |
| `user` | Each member's own | The server entry, naming the member, and that member's client code |
| `device` | Each device's own | Client code on that device |

Each type takes its own fields. Text:

A line of text.

| Field | Type | Description |
|---|---|---|
| `key` | `string` | The name code reads the setting by. |
| `type` | `"string"` | A line of text. |
| `title` | `string` | The setting's label. |
| `description?` | `string \| undefined` | A line of help under the label. |
| `section?` | `string \| undefined` | The heading the setting is drawn under, with the other settings that name it. |
| `scope` | `SettingScope` | Whose setting it is: the server's, each member's or each device's. |
| `required?` | `boolean \| undefined` | Songstress asks for it, and does not start the extension for whoever lacks it: anyone, a member or a device, by its scope. |
| `default?` | `string \| undefined` | The value until someone sets one. |
| `placeholder?` | `string \| undefined` | The hint shown while the field is empty. |
| `maxLength?` | `number \| undefined` | The longest value accepted, in characters. |

A secret, drawn as a password field. Songstress seals it, never shows it again and never sends it to an app; only the
server entry reads it:

A password, key or token. Songstress seals it, never shows it again and never sends it to an app. A device cannot keep one.

| Field | Type | Description |
|---|---|---|
| `key` | `string` | The name code reads the setting by. |
| `type` | `"secret"` | A password field: Songstress seals what is typed and never shows it again. |
| `title` | `string` | The setting's label. |
| `description?` | `string \| undefined` | A line of help under the label. |
| `section?` | `string \| undefined` | The heading the setting is drawn under, with the other settings that name it. |
| `scope` | `"server" \| "user"` | Who the secret belongs to: the whole server or each member. |
| `required?` | `boolean \| undefined` | Songstress asks for it, and does not start the extension for whoever lacks it: anyone, a member or a device, by its scope. |
| `placeholder?` | `string \| undefined` | The hint shown while the field is empty. |

A number:

A number.

| Field | Type | Description |
|---|---|---|
| `key` | `string` | The name code reads the setting by. |
| `type` | `"number"` | A number. |
| `title` | `string` | The setting's label. |
| `description?` | `string \| undefined` | A line of help under the label. |
| `section?` | `string \| undefined` | The heading the setting is drawn under, with the other settings that name it. |
| `scope` | `SettingScope` | Whose setting it is: the server's, each member's or each device's. |
| `required?` | `boolean \| undefined` | Songstress asks for it, and does not start the extension for whoever lacks it: anyone, a member or a device, by its scope. |
| `default?` | `number \| undefined` | The value until someone sets one. |
| `minimum?` | `number \| undefined` | The smallest value accepted. |
| `maximum?` | `number \| undefined` | The largest value accepted. |
| `step?` | `number \| undefined` | The distance between two values the control offers. |

A switch:

A switch.

| Field | Type | Description |
|---|---|---|
| `key` | `string` | The name code reads the setting by. |
| `type` | `"boolean"` | A switch. |
| `title` | `string` | The setting's label. |
| `description?` | `string \| undefined` | A line of help under the label. |
| `section?` | `string \| undefined` | The heading the setting is drawn under, with the other settings that name it. |
| `scope` | `SettingScope` | Whose setting it is: the server's, each member's or each device's. |
| `default?` | `boolean \| undefined` | The value until someone sets one. |

One choice from a list:

One choice from a list.

| Field | Type | Description |
|---|---|---|
| `key` | `string` | The name code reads the setting by. |
| `type` | `"select"` | One choice from a list of options. |
| `title` | `string` | The setting's label. |
| `description?` | `string \| undefined` | A line of help under the label. |
| `section?` | `string \| undefined` | The heading the setting is drawn under, with the other settings that name it. |
| `scope` | `SettingScope` | Whose setting it is: the server's, each member's or each device's. |
| `required?` | `boolean \| undefined` | Songstress asks for it, and does not start the extension for whoever lacks it: anyone, a member or a device, by its scope. |
| `default?` | `string \| undefined` | The value of the option chosen until someone picks one. |
| `options` | `SettingOption[]` | The choices, in the order they are shown. |

Each option:

One choice of a select setting.

| Field | Type | Description |
|---|---|---|
| `value` | `string` | What code reads when this option is chosen. |
| `title` | `string` | The option's label. |

`required` means Songstress asks for the setting and doesn't start the extension for whoever lacks it: for anyone
while a `server` setting is empty, for a member while their `user` setting is, on a device while its `device` setting
is. A default must be a value the setting itself accepts (`SETTING_DEFAULT_INVALID`), a `minimum` may not be above
its `maximum` (`SETTING_RANGE_INVALID`), and no two options share a value (`SETTING_OPTION_DUPLICATE`).

### Reading a setting [#reading-a-setting]

```ts title="src/server.ts"
import { songstress } from 'songstress/server'

declare const userId: string // the member a call concerns, from a hook or a command

const token = await songstress.settings.get<string>('token', { userId }) // a member's own
const mode = await songstress.settings.get<string>('mode') // the server's
songstress.settings.onChange((change) => console.log('a setting changed', change))
```

### The settings page [#the-settings-page]

Every app draws the settings on the extension's own page, each scope where its owner sets it: the server's for the
admin, a member's own, and this device's under "This device" ("This phone" on a phone). A setting may name a
`section`, up to 40 characters: settings that name the same section are drawn together under it as a heading, in the
order the sections first come, and those that name none come first. The buttons of `menus["settings/actions"]` stand
under them.
