Creating a plugin with options
If you don't already know what plugins are, and how to create a basic plugin, see the previous two documents.
If we want a plugin that will also have some settings, we need to make a main file like last time, but with a function.
const { Plugin, Logger } = require('gcommands');
const pluginName = 'plugin-with-options';
module.exports = (options) => {
if (!options.key) return Logger.error('Please define key!', pluginName);
new Plugin(pluginName, (client) => {
client.pluginKey = option.key;
})
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
import { Plugin, Logger } from 'gcommands';
const pluginName = 'plugin-with-options';
// Inject typings for GClient
declare module 'gcommands' {
interface GClient {
pluginKey: string;
}
}
export interface MyOptions {
key: string;
}
export default (options: MyOptions) => {
if (!options.key) return Logger.error('Please define key!', pluginName);
new Plugin(pluginName, (client) => {
client.pluginKey = option.key;
})
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
In the case of plugins, I prefer typescript because you can more easily have typings as well.
Usage Example
const plugin = require('gcommands-plugin-with-options');
plugin({
pluginKey: 'test'
})
1
2
3
4
5
2
3
4
5
import plugin from 'gcommands-plugin-with-options';
plugin({
pluginKey: 'test'
})
1
2
3
4
5
2
3
4
5