Skip to content

Commit e28cf46

Browse files
committed
Update cookbook for slash commands
1 parent f3621c1 commit e28cf46

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/content/docs/ext-dev/cookbook.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,57 @@ Commands.registerCommand({
133133
});
134134
```
135135

136+
### Adding options to a slash command
137+
138+
Specify the option in the `options` array, then use a [type predicate](https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates) to get the value with the correct type:
139+
140+
```ts
141+
import Commands from "@moonlight-mod/wp/commands_commands";
142+
import { CommandType, InputType, OptionType, StringCommandOption } from "@moonlight-mod/types/coreExtensions/commands";
143+
144+
const logger = moonlight.getLogger("your extension");
145+
146+
Commands.registerCommand({
147+
type: CommandType.CHAT,
148+
inputType: InputType.BUILT_IN,
149+
id: "print",
150+
description: "Print a message to the logger",
151+
options: [
152+
{
153+
name: "text",
154+
description: "What to print",
155+
type: OptionType.STRING
156+
}
157+
],
158+
execute: (options) => {
159+
const textOption = options.find((o): o is StringCommandOption => o.name === "text")!;
160+
logger.info(textOption.value);
161+
}
162+
});
163+
```
164+
165+
### Sending a message with a slash command
166+
167+
Use `InputType.BUILT_IN_TEXT` to send the command in chat, then return an object with `content` to override the message:
168+
169+
```ts
170+
import Commands from "@moonlight-mod/wp/commands_commands";
171+
import { CommandType, InputType } from "@moonlight-mod/types/coreExtensions/commands";
172+
173+
Commands.registerCommand({
174+
type: CommandType.CHAT,
175+
inputType: InputType.BUILT_IN_TEXT,
176+
id: "colonThree",
177+
description: "Post :3 in chat",
178+
options: [],
179+
execute: () => {
180+
return { content: ":3" };
181+
}
182+
});
183+
```
184+
185+
Note that this sends an actual message into chat - it is *not* clientside or [ephemeral](https://discord.com/developers/docs/resources/message#message-object-message-flags).
186+
136187
### Modifying message content before it is sent
137188

138189
The legacy command system can also be used to replace text in messages before they are sent.

0 commit comments

Comments
 (0)