/
modules.ts
327 lines (300 loc) · 10.3 KB
/
modules.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import { ApplicationCommandType, ButtonInteraction, ChatInputCommandInteraction, ContextMenuCommandBuilder, GatewayIntentBits, Message, MessageContextMenuCommandInteraction, PermissionResolvable, SlashCommandBuilder, StringSelectMenuInteraction, UserContextMenuCommandInteraction } from "discord.js";
import Artibot from ".";
import { ModulePartResolvable, Trigger } from "./types";
/** Base config for a module */
export interface ModuleConfig {
/** Name of the module */
name: string;
/** ID of this module */
id: string;
/** Version of the module (ex.: "1.2.3"). You should use the same as in your package.json if you want to publish it. */
version?: string;
/** List of supported languages (ex.: ["en", "fr"]). If this does not apply, set to "any". */
langs?: string[] | "any";
/** List of parts of the module */
parts: ModulePartResolvable[];
/** List of required intents */
intents?: GatewayIntentBits[];
/** GitHub repository of the module (ex.: "Artivain/artibot") */
repo?: string;
/** Package name of the module on NPM (ex.: "artibot") */
packageName?: string;
}
/** Base class for Artibot modules */
export class Module {
/** Name of the module */
name: string;
/** ID of this module */
id: string;
/** Version of the module (ex.: "1.2.3"). You should use the same as in your package.json if you want to publish it. */
version: string = "0.0.0";
/** List of supported languages (ex.: ["en", "fr"]). If this does not apply, set to "any". */
langs: string[] | "any" = "any";
/** List of parts of the module */
parts: ModulePartResolvable[];
/** List of required intents */
additionalIntents: GatewayIntentBits[] = [];
/** GitHub repository of the module (ex.: "Artivain/artibot") */
repo?: string;
/** Package name of the module on NPM (ex.: "artibot") */
packageName?: string;
/**
* @param config - Module configuration
*/
constructor({ name, id, version, langs = "any", parts, intents = [], repo, packageName }: ModuleConfig) {
if (!name || !id || !version || !langs || !parts) throw new Error("Missing module informations!");
this.name = name;
this.id = id;
this.version = version;
this.langs = langs;
this.parts = parts;
this.additionalIntents = intents;
this.repo = repo;
this.packageName = packageName;
}
}
/** Base configuration for module parts */
export interface BasePartConfig {
/** ID of the part */
id: string;
/** The function when the part is executed */
mainFunction: (...args: any[]) => Promise<void>;
/** The function executed on bot startup */
initFunction?: (artibot: Artibot) => Promise<void>;
}
/**
* Base class for module parts
*/
export class BasePart {
/** ID of the part */
id: string;
/** The function when the part is executed */
execute: BasePartConfig["mainFunction"];
/** The function executed on bot startup */
init: BasePartConfig["initFunction"];
/**
* @param config - Part configuration
*/
constructor({ id, mainFunction, initFunction }: BasePartConfig) {
if (!id || !mainFunction) throw new Error("Missing parameter(s)");
this.id = id;
this.execute = mainFunction;
this.init = initFunction;
}
}
export interface CommandConfig extends BasePartConfig {
/** Name of the command */
name: string;
/** Description of the command */
description?: string;
/** List of alternative names */
aliases?: string[];
/** Help text on how to use the command */
usage?: string;
/** Minimum time (in seconds) between usages */
cooldown?: number;
/** If the command can only be executed by the owner of the bot */
ownerOnly?: boolean;
/** If the command can only be executed in a guild */
guildOnly?: boolean;
/** Required permissions */
permissions?: PermissionResolvable;
/**
* Set to true if the command needs at least one argument
* @deprecated Use {@link requiredArgs} instead.
*/
requiresArgs?: boolean;
/**
* Minimum amount of arguments
* @since 5.0.0
*/
requiredArgs?: number;
/** Function executed when command is ran */
mainFunction: (message: Message, args: string[], artibot: Artibot) => Promise<void>;
}
/**
* Command part for a module
* @extends BasePart
*/
export class Command extends BasePart {
/** Name of the command */
name: string;
/** Description of the command */
description?: string;
/** List of alternative names */
aliases: string[] = [];
/** Help text on how to use the command */
usage?: string;
/** Minimum time (in seconds) between usages */
cooldown: number = 0;
/** If the command can only be executed by the owner of the bot */
ownerOnly: boolean = false;
/** Required permissions */
permissions?: PermissionResolvable;
/** Minimum amount of arguments */
args: number = 0;
/** If the command can only be executed in a guild */
guildOnly: boolean = true;
/**
* @param config - Command configuration
*/
constructor(config: CommandConfig) {
super(config);
this.name = config.name;
this.description = config.description;
if (config.aliases) this.aliases = config.aliases;
this.usage = config.usage;
if (config.cooldown) this.cooldown = config.cooldown;
if (config.ownerOnly) this.ownerOnly = config.ownerOnly;
this.permissions = config.permissions;
if (config.requiresArgs) this.args = 1;
if (config.requiredArgs) this.args = config.requiredArgs;
if (config.guildOnly) this.guildOnly = config.guildOnly;
}
}
/** Configuration for a slash command */
export interface SlashCommandConfig extends BasePartConfig {
/** Data to register into the Discord API */
data: Partial<SlashCommandBuilder>;
/** Minimum time (in seconds) between usages */
cooldown?: number;
/** Function to execute when the command is ran */
mainFunction: (interaction: ChatInputCommandInteraction<"cached">, artibot: Artibot) => Promise<void>;
}
/**
* Slash command part for a module
* @extends BasePart
*/
export class SlashCommand extends BasePart {
/** Data to register into the Discord API */
data: Partial<SlashCommandBuilder>;
/** Minimum time (in seconds) between usages */
cooldown: number = 0;
/**
* @param config - Slash command configuration
*/
constructor({ id, data, cooldown = 0, mainFunction, initFunction }: SlashCommandConfig) {
if (!data) throw new Error("Missing data parameter");
super({ id, mainFunction, initFunction });
this.data = data;
this.cooldown = cooldown;
}
}
/** Configuration for a button */
export interface ButtonConfig extends BasePartConfig {
/** Function to execute when the button is clicked */
mainFunction: (interaction: ButtonInteraction<"cached">, artibot: Artibot) => Promise<void>;
}
/**
* Button interaction part for a module
* @extends BasePart
*/
export class Button extends BasePart {
/**
* @param config - Button configuration
*/
constructor({ id, mainFunction, initFunction }: ButtonConfig) {
super({ id, mainFunction, initFunction });
}
}
/** Configuration for a message context menu option */
export interface MessageContextMenuOptionConfig extends BasePartConfig {
/** Name of this option */
name: string;
/** Function to execute when the menu option is clicked */
mainFunction: (interaction: MessageContextMenuCommandInteraction<"cached">, artibot: Artibot) => Promise<void>;
}
/**
* Message context menu option part for a module
* @extends BasePart
*/
export class MessageContextMenuOption extends BasePart {
/** Data to register into the Discord API */
data: ContextMenuCommandBuilder = new ContextMenuCommandBuilder()
.setType(ApplicationCommandType.Message);
/**
* @param config - Message context menu option configuration
*/
constructor({ id, name, mainFunction, initFunction }: MessageContextMenuOptionConfig) {
if (!name) throw new Error("Missing name parameter!");
super({ id, mainFunction, initFunction });
this.data.setName(name);
}
}
/** Configuration for a user context menu option */
export interface UserContextMenuOptionConfig extends BasePartConfig {
/** Name of this option */
name: string;
/** Function to execute when the menu option is clicked */
mainFunction: (interaction: UserContextMenuCommandInteraction<"cached">, artibot: Artibot) => Promise<void>;
}
/**
* User context menu option part for a module
* @extends BasePart
*/
export class UserContextMenuOption extends BasePart {
/** Data to register into the Discord API */
data: ContextMenuCommandBuilder = new ContextMenuCommandBuilder()
.setType(ApplicationCommandType.User);
/**
* @param config - User context menu option configuration
*/
constructor({ id, name, mainFunction, initFunction }: UserContextMenuOptionConfig) {
if (!name) throw new Error("Missing name parameter!");
super({ id, mainFunction, initFunction });
this.data.setName(name);
}
}
/** Configuration for a select menu option */
export interface SelectMenuOptionConfig extends BasePartConfig {
/** Function executed when this option is selected */
mainFunction: (interaction: StringSelectMenuInteraction<"cached">, artibot: Artibot) => Promise<void>;
}
/**
* Select menu option part for a module
* @extends BasePart
*/
export class SelectMenuOption extends BasePart {
/**
* @param config - Select menu option configuration
*/
constructor({ id, mainFunction, initFunction }: SelectMenuOptionConfig) {
super({ id, mainFunction, initFunction });
}
}
/** Configuration for a trigger group */
export interface TriggerGroupConfig extends BasePartConfig {
/** List of triggers */
triggers: Trigger[];
/** Function executed on trigger found */
mainFunction: (message: Message, trigger: Trigger, artibot: Artibot) => Promise<void>;
}
/** Configuration for a trigger group */
export class TriggerGroup extends BasePart {
/** List of triggers */
triggers: Trigger[];
/**
* @param config - Trigger group configuration
*/
constructor({ id, triggers, mainFunction, initFunction }: TriggerGroupConfig) {
if (!triggers || !triggers.length) throw new Error("Triggers cannot be empty!");
super({ id, mainFunction, initFunction });
this.triggers = triggers;
}
}
/**
* Global part for a module
* - Special part which is not managed by a event handler and only ran at startup
* @extends BasePart
*/
export class Global extends BasePart {
/**
* @param {Object} config - Config for this global
* @param {string} config.id - ID of this global
* @param {function(Artibot): void|Promise<void>} config.mainFunction - Function executed on bot startup
*/
constructor({ id, mainFunction }: BasePartConfig) {
super({ id, mainFunction });
this.init = mainFunction;
}
}