feat: Add slashcommand api
This commit is contained in:
@@ -621,7 +621,7 @@ interface PluginApiCustomStreamRequest {
|
||||
label?: string;
|
||||
}
|
||||
|
||||
type PluginApiActionSource = 'composerAction' | 'toolbarAction' | 'profileAction' | 'manual';
|
||||
type PluginApiActionSource = 'composerAction' | 'toolbarAction' | 'profileAction' | 'slashCommand' | 'manual';
|
||||
interface PluginApiActionContext {
|
||||
source: PluginApiActionSource;
|
||||
user: User | null;
|
||||
@@ -821,6 +821,10 @@ interface TojuClientPluginApi {
|
||||
registerEmbedRenderer: (id: string, contribution: PluginApiEmbedRendererContribution) => TojuPluginDisposable;
|
||||
mountElement: (id: string, request: PluginApiDomMountRequest) => TojuPluginDisposable;
|
||||
};
|
||||
readonly commands: {
|
||||
register: (id: string, contribution: PluginApiSlashCommandContribution) => TojuPluginDisposable;
|
||||
list: () => PluginApiSlashCommandContribution[];
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1178,6 +1182,8 @@ Capabilities:
|
||||
| `registerEmbedRenderer` | `ui.embeds` |
|
||||
| `mountElement` | `ui.dom` |
|
||||
|
||||
For `/` slash commands, use `api.commands.register` (capability `ui.commands`). See the Slash Commands subsection below.
|
||||
|
||||
Register side panel:
|
||||
|
||||
```js
|
||||
@@ -1310,6 +1316,36 @@ context.subscriptions.push(
|
||||
|
||||
`mountElement` tags the element with plugin ownership metadata, replaces duplicate mounts for the same plugin/id, and removes it on disposal/unload.
|
||||
|
||||
### Slash Commands
|
||||
|
||||
Capability: `commands.register` and `commands.list` both require `ui.commands`.
|
||||
|
||||
Register `/` slash commands that appear in the chat composer's autocomplete menu. Set `scope: 'global'` (default) for commands available in chat servers and direct messages, or `scope: 'server'` for commands only offered while a chat server is active. Declare `options` to parse arguments into `context.args` (use `type: 'rest'` to capture all trailing text). The `run` callback receives a context with `source: 'slashCommand'`, the parsed `args`, the invoked `command` name, the raw `rawArgs`, and the current user/server/channel.
|
||||
|
||||
```js
|
||||
context.subscriptions.push(
|
||||
api.commands.register('announce', {
|
||||
name: 'announce',
|
||||
description: 'Post an announcement to the current channel',
|
||||
icon: 'megaphone',
|
||||
scope: 'server',
|
||||
options: [{ name: 'message', type: 'rest', required: true }],
|
||||
run: (slash) => api.messages.send(`Announcement: ${slash.args.message}`, slash.textChannel?.id)
|
||||
})
|
||||
);
|
||||
|
||||
context.subscriptions.push(
|
||||
api.commands.register('shrug', {
|
||||
name: 'shrug',
|
||||
description: 'Append the shrug emoticon',
|
||||
scope: 'global',
|
||||
run: () => api.messages.send('shrug')
|
||||
})
|
||||
);
|
||||
```
|
||||
|
||||
A command with no `options` runs immediately when picked; a command with options fills `/name ` so the user can type arguments before sending. Slash input is never posted as a chat message; unmatched `/text` falls through as a normal message.
|
||||
|
||||
## Capability Cheat Sheet
|
||||
|
||||
| API call group | Capabilities |
|
||||
@@ -1351,6 +1387,7 @@ context.subscriptions.push(
|
||||
| `ui.registerChannelSection` | `ui.channelsSection` |
|
||||
| `ui.registerEmbedRenderer` | `ui.embeds` |
|
||||
| `ui.mountElement` | `ui.dom` |
|
||||
| `commands.register`, `commands.list` | `ui.commands` |
|
||||
|
||||
## Complete Example Plugin
|
||||
|
||||
|
||||
Reference in New Issue
Block a user