Skip to content

Commit c702f73

Browse files
committed
feat(docs): update docs with new info
1 parent be5b81f commit c702f73

File tree

4 files changed

+79
-36
lines changed

4 files changed

+79
-36
lines changed

.changeset/tough-rules-rush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"docs": patch
3+
---
4+
5+
Update docs to have new plugin information

apps/docs/src/pages/docs/core-concepts/adding-plugins.mdx

Lines changed: 67 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,64 @@ import Callout from '~/components/Callout';
99

1010
Plugins are a way to reuse code between multiple different commands, without writing the same code twice.
1111

12+
<Callout type="info">Plugins were heavily inspired by [`Sern`](https://sern.dev)'s system. Check them out!</Callout>
13+
1214
It's recommended that you have a directory for plugins, and you can use that directory to import it to your commands.
1315

1416
For example, you could make an `Owner Only` plugin like this:
1517

1618
```ts filename="src/plugins/ownerOnly.ts" copy
17-
// This is an example plugin for a 'owner only' slash command plugin.
18-
// For a text command plugin, you'd just have to use message instead of interaction.
19+
// This is an example plugin for a 'owner only' command plugin.
20+
21+
// Import needed types from Spark
22+
import { CommandPlugin, CommandType } from '@spark.ts/handler';
1923

20-
import { SlashCommandPlugin } from '@spark.ts/handler';
24+
// Util function to mention users with their ids
25+
function mapUsers(ids: string[]) {
26+
const mention = (s: string) => `<@!${s}>`;
27+
return ids.map((id) => `\` - \` ${mention(id)}`).join('\n');
28+
}
2129

22-
export function ownerOnly(ownerIds: string[]): SlashCommandPlugin {
30+
// The params in this function will be required when you use the plugin.
31+
export function ownerOnly(ownerIds: string[]): CommandPlugin<CommandType.Both> {
2332
return {
24-
// Whether the event should run while the command gets registed & not when the command is executed.
25-
preprocess: false,
33+
name: 'Owner Only',
34+
description: 'Only allows the owner(s) to run the command.',
35+
async run({
36+
controller, command, message, interaction,
37+
}) {
38+
// Checks if the command is a text command.
39+
if (command.type === CommandType.Text) {
40+
// You can use the `!` operator safety as message will be inside of this block.
41+
if (ownerIds.includes(message!.author.id)) {
42+
// Continues with execution, will run other plugins.
43+
// If all plugins are successful, the command is ran.
44+
return controller.next();
45+
}
46+
47+
await message!.reply(`Only these people can run the command!\n${mapUsers(ownerIds)}`);
48+
}
2649

27-
// Name & description of the plugin, this is mainly for documentation.
28-
name: 'Owner only plugin',
29-
description: 'Only allows the owner to run the command.',
50+
if (command.type === CommandType.Slash) {
51+
if (ownerIds.includes(interaction!.user.id)) {
52+
return controller.next();
53+
}
3054

31-
// Logic for the plugin.
32-
async run({ interaction, controller }) {
33-
if (ownerIds.includes(interaction.user.id)) {
34-
return controller.next();
55+
await interaction!.reply({
56+
content: `Only these people can run the command!\n${mapUsers(ownerIds)}`,
57+
ephemeral: true,
58+
});
3559
}
3660

37-
await interaction.reply({
38-
content: `Only these people can run this!\n${mapUsers(ownerIds)}`,
39-
ephemeral: true,
40-
});
41-
61+
// Stops the controller, this stops the command & other plugins from being ran.
4262
return controller.stop();
4363
},
4464
};
4565
}
4666
```
4767

48-
<Callout>
49-
This plugin only works for a slash command. Notice the return signature, `SlashCommandPlugin`.
68+
<Callout type="info">
69+
This plugin works for both text commands & slash commands because of the type used in `CommandPlugin`.
5070
</Callout>
5171

5272
## Command Integration
@@ -57,8 +77,7 @@ To add a plugin to your command, just import the function and then add it to the
5777

5878
```ts filename="src/commands/ownerOnly.ts" {6} copy
5979
import { SparkCommand } from '@spark.ts/handler';
60-
// This import is using a path alias, `~` for the `src` directory.
61-
import { ownerOnly } from '~/plugins/ownerOnly.js';
80+
import { ownerOnly } from '../../plugins/ownerOnly.js';
6281

6382
export default new SparkCommand({
6483
plugins: [ownerOnly('<your user id would go here>')],
@@ -69,22 +88,37 @@ export default new SparkCommand({
6988
});
7089
```
7190

72-
## Controller
91+
## Init Plugins
92+
93+
Init plugins are plugins that run once a command is initialized/loaded.
7394

74-
The controller is what controls your flow of your code. If you do `controller.stop()` in your command, it'll stop the execution of your command.
95+
The pattern of creating these plugins are very similar to creating any other plugin, you just have to use the `InitPlugin` type instead of `CommandPlugin`.
7596

76-
Please make sure to return this upon an error in your plugin. For example, if the user is not permitted to use the command, you return `controller.stop()` in your plugin.
97+
We can create a basic `InitPlugin`, one that logs `${command.name}` whenever a command is loaded.
7798

78-
If you have a success in your plugin, return `controller.next()`. This will continue on with the command, and other plugins.
99+
```ts filename="src/plugins/log.ts" copy
100+
// Import types from Spark
101+
import { CommandType, InitPlugin } from '@spark.ts/handler';
79102

103+
export function logPlugin(): InitPlugin<CommandType.Both> {
104+
return {
105+
name: 'Log Plugin',
106+
description: 'Logs the plugin name with a success log',
107+
run({ client, command, controller }) {
108+
// Logs that the command was loaded
109+
client.logger.success(`${command.name} was successfully loaded.`);
110+
111+
// Continues with execution
112+
return controller.next();
113+
},
114+
};
115+
}
116+
```
80117

81-
## Pre-Process Plugins
118+
## Controller
82119

83-
Plugins that run while your command get registered are called **pre-process plugins.**
120+
The controller is what controls your flow of your code. If you do `controller.stop()` in your command, it stops the execution of your command.
84121

85-
To create a pre-process plugin, just add the `preprocess: true` option to your plugin function's return object.
122+
Please make sure to return this upon an error in your plugin. For example, if the user is not permitted to use the command, return `controller.stop()` in your plugin.
86123

87-
<Callout>
88-
Pre-process plugins run while the command is registered.
89-
This means pre-process plugins don't have access to `interaction` or `message`.
90-
</Callout>
124+
If you have a success in your plugin, return `controller.next()`. This will execute other plugins, if available, and then the command.

apps/docs/src/pages/docs/core-concepts/publishing-plugins.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Here's an example of how you could publish a plugin:
2525

2626
### Name:
2727

28-
`'owner only'`
28+
`'Owner Only'`
2929

3030
### Description:
3131

apps/docs/tsconfig.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@
2828
"next-env.d.ts",
2929
"nextra.d.ts",
3030
"**/*.ts",
31-
"**/*.tsx"
32-
, "src/pages/plugins/index.mdx", "src/pages/plugins.mdx", "src/pages/plugins/[id].mdx", "src/utils/docgen.mjs" ],
31+
"**/*.tsx",
32+
"src/pages/plugins/index.mdx",
33+
"src/pages/plugins.mdx",
34+
"src/pages/plugins/[id].mdx",
35+
"src/utils/docgen.mjs"
36+
],
3337
"exclude": [
3438
"node_modules"
3539
]

0 commit comments

Comments
 (0)