@marshmallow-stoat/mally 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +212 -0
- package/dist/index.d.mts +549 -0
- package/dist/index.d.ts +549 -0
- package/dist/index.js +661 -0
- package/dist/index.mjs +610 -0
- package/package.json +42 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,610 @@
|
|
|
1
|
+
// src/types.ts
|
|
2
|
+
var BaseCommand = class {
|
|
3
|
+
/**
|
|
4
|
+
* Optional: Called when an error occurs during command execution.
|
|
5
|
+
* Override this method to provide custom error handling.
|
|
6
|
+
*/
|
|
7
|
+
async onError(ctx, error) {
|
|
8
|
+
await ctx.reply(`An error occurred: ${error.message}`);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// src/decorators/Stoat.ts
|
|
13
|
+
import "reflect-metadata";
|
|
14
|
+
|
|
15
|
+
// src/decorators/keys.ts
|
|
16
|
+
var METADATA_KEYS = {
|
|
17
|
+
COMMAND_OPTIONS: /* @__PURE__ */ Symbol("mally:command:options"),
|
|
18
|
+
IS_COMMAND: /* @__PURE__ */ Symbol("mally:command:isCommand"),
|
|
19
|
+
IS_STOAT_CLASS: /* @__PURE__ */ Symbol("mally:stoat:isClass"),
|
|
20
|
+
SIMPLE_COMMANDS: /* @__PURE__ */ Symbol("mally:stoat:simpleCommands"),
|
|
21
|
+
GUARDS: "mally:command:guards"
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// src/decorators/Stoat.ts
|
|
25
|
+
function Stoat() {
|
|
26
|
+
return (target) => {
|
|
27
|
+
Reflect.defineMetadata(METADATA_KEYS.IS_STOAT_CLASS, true, target);
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function isStoatClass(target) {
|
|
31
|
+
return Reflect.getMetadata(METADATA_KEYS.IS_STOAT_CLASS, target) === true;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// src/decorators/SimpleCommand.ts
|
|
35
|
+
import "reflect-metadata";
|
|
36
|
+
function SimpleCommand(options = {}) {
|
|
37
|
+
return (target, propertyKey, descriptor) => {
|
|
38
|
+
const constructor = target.constructor;
|
|
39
|
+
const existingCommands = Reflect.getMetadata(METADATA_KEYS.SIMPLE_COMMANDS, constructor) || [];
|
|
40
|
+
existingCommands.push({
|
|
41
|
+
methodName: String(propertyKey),
|
|
42
|
+
options
|
|
43
|
+
});
|
|
44
|
+
Reflect.defineMetadata(METADATA_KEYS.SIMPLE_COMMANDS, existingCommands, constructor);
|
|
45
|
+
return descriptor;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function getSimpleCommands(target) {
|
|
49
|
+
return Reflect.getMetadata(METADATA_KEYS.SIMPLE_COMMANDS, target) || [];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// src/decorators/Command.ts
|
|
53
|
+
import "reflect-metadata";
|
|
54
|
+
function Command(options = {}) {
|
|
55
|
+
return (target) => {
|
|
56
|
+
Reflect.defineMetadata(METADATA_KEYS.IS_COMMAND, true, target);
|
|
57
|
+
Reflect.defineMetadata(METADATA_KEYS.COMMAND_OPTIONS, options, target);
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function isCommand(target) {
|
|
61
|
+
return Reflect.getMetadata(METADATA_KEYS.IS_COMMAND, target) === true;
|
|
62
|
+
}
|
|
63
|
+
function getCommandOptions(target) {
|
|
64
|
+
return Reflect.getMetadata(METADATA_KEYS.COMMAND_OPTIONS, target);
|
|
65
|
+
}
|
|
66
|
+
function buildCommandMetadata(target, options, category) {
|
|
67
|
+
const className = target.name;
|
|
68
|
+
const derivedName = className.replace(/Command$/i, "").toLowerCase();
|
|
69
|
+
return {
|
|
70
|
+
name: options.name ?? derivedName,
|
|
71
|
+
description: options.description ?? "No description provided",
|
|
72
|
+
aliases: options.aliases ?? [],
|
|
73
|
+
permissions: options.permissions ?? [],
|
|
74
|
+
category: options.category ?? category ?? "uncategorized",
|
|
75
|
+
cooldown: options.cooldown ?? 0,
|
|
76
|
+
nsfw: options.nsfw ?? false,
|
|
77
|
+
ownerOnly: options.ownerOnly ?? false
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/decorators/Guard.ts
|
|
82
|
+
import "reflect-metadata";
|
|
83
|
+
function Guard(guardClass) {
|
|
84
|
+
return (target) => {
|
|
85
|
+
const existingGuards = Reflect.getMetadata(METADATA_KEYS.GUARDS, target) || [];
|
|
86
|
+
existingGuards.push(guardClass);
|
|
87
|
+
Reflect.defineMetadata(METADATA_KEYS.GUARDS, existingGuards, target);
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function getGuards(target) {
|
|
91
|
+
return Reflect.getMetadata(METADATA_KEYS.GUARDS, target) || [];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// src/decorators/utils.ts
|
|
95
|
+
function buildSimpleCommandMetadata(options, methodName, category) {
|
|
96
|
+
return {
|
|
97
|
+
name: options.name ?? methodName.toLowerCase(),
|
|
98
|
+
description: options.description ?? "No description provided",
|
|
99
|
+
aliases: options.aliases ?? [],
|
|
100
|
+
permissions: options.permissions ?? [],
|
|
101
|
+
category: options.category ?? category ?? "uncategorized",
|
|
102
|
+
cooldown: options.cooldown ?? 0,
|
|
103
|
+
nsfw: options.nsfw ?? false,
|
|
104
|
+
ownerOnly: options.ownerOnly ?? false
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/registry.ts
|
|
109
|
+
import * as path from "path";
|
|
110
|
+
import { pathToFileURL } from "url";
|
|
111
|
+
import { glob } from "tinyglobby";
|
|
112
|
+
var CommandRegistry = class {
|
|
113
|
+
constructor(extensions = [".js", ".ts"]) {
|
|
114
|
+
this.commands = /* @__PURE__ */ new Map();
|
|
115
|
+
this.aliases = /* @__PURE__ */ new Map();
|
|
116
|
+
this.extensions = extensions;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Get the number of registered commands
|
|
120
|
+
*/
|
|
121
|
+
get size() {
|
|
122
|
+
return this.commands.size;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Load commands from a directory using glob pattern matching
|
|
126
|
+
*/
|
|
127
|
+
async loadFromDirectory(directory) {
|
|
128
|
+
const patterns = this.extensions.map((ext) => path.join(directory, "**", `*${ext}`).replace(/\\/g, "/"));
|
|
129
|
+
for (const pattern of patterns) {
|
|
130
|
+
const files = await glob(pattern, {
|
|
131
|
+
ignore: ["**/*.d.ts", "**/*.test.ts", "**/*.spec.ts"],
|
|
132
|
+
absolute: true
|
|
133
|
+
});
|
|
134
|
+
for (const file of files) {
|
|
135
|
+
await this.loadFile(file, directory);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
console.log(`[Mally] Loaded ${this.commands.size} command(s)`);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Register a command instance
|
|
142
|
+
*/
|
|
143
|
+
register(instance, metadata, classConstructor, methodName) {
|
|
144
|
+
const name = metadata.name.toLowerCase();
|
|
145
|
+
if (this.commands.has(name)) {
|
|
146
|
+
console.warn(`[Mally] Duplicate command name: ${name}. Skipping...`);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
this.validateGuards(classConstructor, metadata.name);
|
|
150
|
+
if (!methodName) {
|
|
151
|
+
this.validateCooldown(instance, metadata);
|
|
152
|
+
}
|
|
153
|
+
this.commands.set(name, { instance, metadata, methodName, classConstructor });
|
|
154
|
+
for (const alias of metadata.aliases) {
|
|
155
|
+
const aliasLower = alias.toLowerCase();
|
|
156
|
+
if (this.aliases.has(aliasLower) || this.commands.has(aliasLower)) {
|
|
157
|
+
console.warn(`[Mally] Duplicate alias: ${aliasLower}. Skipping...`);
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
this.aliases.set(aliasLower, name);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Get a command by name or alias
|
|
165
|
+
*/
|
|
166
|
+
get(name) {
|
|
167
|
+
const lowerName = name.toLowerCase();
|
|
168
|
+
const resolvedName = this.aliases.get(lowerName) ?? lowerName;
|
|
169
|
+
return this.commands.get(resolvedName);
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Check if a command exists
|
|
173
|
+
*/
|
|
174
|
+
has(name) {
|
|
175
|
+
const lowerName = name.toLowerCase();
|
|
176
|
+
return this.commands.has(lowerName) || this.aliases.has(lowerName);
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Get all registered commands
|
|
180
|
+
*/
|
|
181
|
+
getAll() {
|
|
182
|
+
return Array.from(this.commands.values());
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Get all command metadata
|
|
186
|
+
*/
|
|
187
|
+
getAllMetadata() {
|
|
188
|
+
return this.getAll().map((c) => c.metadata);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Get commands grouped by category
|
|
192
|
+
*/
|
|
193
|
+
getByCategory() {
|
|
194
|
+
const categories = /* @__PURE__ */ new Map();
|
|
195
|
+
for (const cmd of this.commands.values()) {
|
|
196
|
+
const category = cmd.metadata.category;
|
|
197
|
+
const existing = categories.get(category) ?? [];
|
|
198
|
+
existing.push(cmd);
|
|
199
|
+
categories.set(category, existing);
|
|
200
|
+
}
|
|
201
|
+
return categories;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Clear all commands
|
|
205
|
+
*/
|
|
206
|
+
clear() {
|
|
207
|
+
this.commands.clear();
|
|
208
|
+
this.aliases.clear();
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Iterate over commands
|
|
212
|
+
*/
|
|
213
|
+
[Symbol.iterator]() {
|
|
214
|
+
return this.commands.entries();
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Iterate over command values
|
|
218
|
+
*/
|
|
219
|
+
values() {
|
|
220
|
+
return this.commands.values();
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Iterate over command names
|
|
224
|
+
*/
|
|
225
|
+
keys() {
|
|
226
|
+
return this.commands.keys();
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Validate that all guards on a command implement the required methods
|
|
230
|
+
* @param commandClass
|
|
231
|
+
* @param commandName
|
|
232
|
+
* @private
|
|
233
|
+
*/
|
|
234
|
+
validateGuards(commandClass, commandName) {
|
|
235
|
+
const guards = Reflect.getMetadata("mally:command:guards", commandClass) || [];
|
|
236
|
+
for (const GuardClass of guards) {
|
|
237
|
+
const guardInstance = new GuardClass();
|
|
238
|
+
if (typeof guardInstance.run !== "function") {
|
|
239
|
+
console.error(
|
|
240
|
+
`[Mally] FATAL: Guard "${GuardClass.name}" on command "${commandName}" does not have a run() method.`
|
|
241
|
+
);
|
|
242
|
+
process.exit(1);
|
|
243
|
+
}
|
|
244
|
+
if (typeof guardInstance.guardFail !== "function") {
|
|
245
|
+
console.error(
|
|
246
|
+
`[Mally] FATAL: Guard "${GuardClass.name}" on command "${commandName}" does not have a guardFail() method.`
|
|
247
|
+
);
|
|
248
|
+
console.error(`[Mally] All guards must implement guardFail() to handle failed checks.`);
|
|
249
|
+
process.exit(1);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Validate that commands with cooldowns implement the onCooldown method
|
|
255
|
+
* @param instance
|
|
256
|
+
* @param metadata
|
|
257
|
+
* @private
|
|
258
|
+
*/
|
|
259
|
+
validateCooldown(instance, metadata) {
|
|
260
|
+
if (metadata.cooldown > 0 && typeof instance.onCooldown !== "function") {
|
|
261
|
+
console.error(
|
|
262
|
+
`[Mally] FATAL: Command "${metadata.name}" has a cooldown of ${metadata.cooldown}ms but does not implement onCooldown() method.`
|
|
263
|
+
);
|
|
264
|
+
console.error(
|
|
265
|
+
`[Mally] Commands with cooldowns must implement onCooldown(ctx, remaining) to handle cooldown messages.`
|
|
266
|
+
);
|
|
267
|
+
process.exit(1);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Load commands from a single file
|
|
272
|
+
*/
|
|
273
|
+
async loadFile(filePath, baseDir) {
|
|
274
|
+
try {
|
|
275
|
+
const fileUrl = pathToFileURL(filePath).href;
|
|
276
|
+
const module = await import(fileUrl);
|
|
277
|
+
for (const exportKey of Object.keys(module)) {
|
|
278
|
+
const exported = module[exportKey];
|
|
279
|
+
if (typeof exported !== "function") {
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
if (isStoatClass(exported)) {
|
|
283
|
+
const instance2 = new exported();
|
|
284
|
+
const simpleCommands = getSimpleCommands(exported);
|
|
285
|
+
const category2 = this.getCategoryFromPath(filePath, baseDir);
|
|
286
|
+
if (simpleCommands.length === 0) {
|
|
287
|
+
console.warn(
|
|
288
|
+
`[Mally] Class ${exported.name} is decorated with @Stoat but has no @SimpleCommand methods. Skipping...`
|
|
289
|
+
);
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
for (const cmdDef of simpleCommands) {
|
|
293
|
+
const method = instance2[cmdDef.methodName];
|
|
294
|
+
if (typeof method !== "function") {
|
|
295
|
+
console.warn(`[Mally] Method ${cmdDef.methodName} not found on ${exported.name}. Skipping...`);
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
const metadata2 = buildSimpleCommandMetadata(cmdDef.options, cmdDef.methodName, category2);
|
|
299
|
+
this.register(instance2, metadata2, exported, cmdDef.methodName);
|
|
300
|
+
}
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
if (!isCommand(exported)) {
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
const options = getCommandOptions(exported);
|
|
307
|
+
if (!options) continue;
|
|
308
|
+
const instance = new exported();
|
|
309
|
+
if (typeof instance.run !== "function") {
|
|
310
|
+
console.warn(
|
|
311
|
+
`[Mally] Class ${exported.name} is decorated with @Command but does not implement run() method. Skipping...`
|
|
312
|
+
);
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
const category = this.getCategoryFromPath(filePath, baseDir);
|
|
316
|
+
const metadata = buildCommandMetadata(exported, options, category);
|
|
317
|
+
instance.metadata = metadata;
|
|
318
|
+
this.register(instance, metadata, exported);
|
|
319
|
+
}
|
|
320
|
+
} catch (error) {
|
|
321
|
+
console.error(`[Mally] Failed to load command file: ${filePath}`, error);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Derive category from file path relative to base directory
|
|
326
|
+
*/
|
|
327
|
+
getCategoryFromPath(filePath, baseDir) {
|
|
328
|
+
const relative2 = path.relative(baseDir, filePath);
|
|
329
|
+
const parts = relative2.split(path.sep);
|
|
330
|
+
if (parts.length > 1) {
|
|
331
|
+
return parts[0];
|
|
332
|
+
}
|
|
333
|
+
return void 0;
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
// src/handler.ts
|
|
338
|
+
import "reflect-metadata";
|
|
339
|
+
var MallyHandler = class {
|
|
340
|
+
constructor(options) {
|
|
341
|
+
this.cooldowns = /* @__PURE__ */ new Map();
|
|
342
|
+
this.client = options.client;
|
|
343
|
+
this.commandsDir = options.commandsDir;
|
|
344
|
+
this.prefixResolver = options.prefix;
|
|
345
|
+
this.owners = new Set(options.owners ?? []);
|
|
346
|
+
this.registry = new CommandRegistry(options.extensions);
|
|
347
|
+
this.disableMentionPrefix = options.disableMentionPrefix ?? false;
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Initialize the handler - load all commands
|
|
351
|
+
*/
|
|
352
|
+
async init() {
|
|
353
|
+
await this.registry.loadFromDirectory(this.commandsDir);
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Parse a raw message into command context
|
|
357
|
+
*/
|
|
358
|
+
async parseMessage(rawContent, message, meta) {
|
|
359
|
+
const prefix = await this.resolvePrefix(meta.serverId);
|
|
360
|
+
let usedPrefix = prefix;
|
|
361
|
+
let withoutPrefix = "";
|
|
362
|
+
if (rawContent.startsWith(prefix)) {
|
|
363
|
+
withoutPrefix = rawContent.slice(prefix.length).trim();
|
|
364
|
+
usedPrefix = prefix;
|
|
365
|
+
} else if (!this.disableMentionPrefix && rawContent.match(/^<@!?[\w]+>/)) {
|
|
366
|
+
const mentionMatch = rawContent.match(/^<@!?([\w]+)>\s*/);
|
|
367
|
+
if (mentionMatch) {
|
|
368
|
+
const mentionedId = mentionMatch[1];
|
|
369
|
+
const botId = this.client.user?.id;
|
|
370
|
+
if (botId && mentionedId === botId) {
|
|
371
|
+
usedPrefix = mentionMatch[0];
|
|
372
|
+
withoutPrefix = rawContent.slice(mentionMatch[0].length).trim();
|
|
373
|
+
} else {
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
if (!withoutPrefix) {
|
|
378
|
+
return null;
|
|
379
|
+
}
|
|
380
|
+
const [commandName, ...args] = withoutPrefix.split(/\s+/);
|
|
381
|
+
if (!commandName) {
|
|
382
|
+
return null;
|
|
383
|
+
}
|
|
384
|
+
return {
|
|
385
|
+
client: this.client,
|
|
386
|
+
content: rawContent,
|
|
387
|
+
authorId: meta.authorId,
|
|
388
|
+
channelId: meta.channelId,
|
|
389
|
+
serverId: meta.serverId,
|
|
390
|
+
args,
|
|
391
|
+
prefix: usedPrefix,
|
|
392
|
+
commandName: commandName.toLowerCase(),
|
|
393
|
+
reply: meta.reply,
|
|
394
|
+
message
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Handle a message object using the configured message adapter
|
|
399
|
+
*
|
|
400
|
+
* @example
|
|
401
|
+
* ```ts
|
|
402
|
+
* // With message adapter configured
|
|
403
|
+
* client.on('messageCreate', (message) => {
|
|
404
|
+
* handler.handle(message);
|
|
405
|
+
* });
|
|
406
|
+
* ```
|
|
407
|
+
*/
|
|
408
|
+
async handle(message) {
|
|
409
|
+
if (!message.channel || !message.author) {
|
|
410
|
+
return false;
|
|
411
|
+
}
|
|
412
|
+
if (message.author.bot) {
|
|
413
|
+
return false;
|
|
414
|
+
}
|
|
415
|
+
const rawContent = message.content;
|
|
416
|
+
const authorId = message.author.id;
|
|
417
|
+
const channelId = message.channel.id;
|
|
418
|
+
const serverId = message.server?.id;
|
|
419
|
+
const reply = async (content) => {
|
|
420
|
+
await message.channel.sendMessage(content);
|
|
421
|
+
};
|
|
422
|
+
return this.handleMessage(rawContent, message, {
|
|
423
|
+
authorId,
|
|
424
|
+
channelId,
|
|
425
|
+
serverId,
|
|
426
|
+
reply
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Handle a raw message string with metadata
|
|
431
|
+
*
|
|
432
|
+
* @example
|
|
433
|
+
* ```ts
|
|
434
|
+
* // Manual usage without message adapter
|
|
435
|
+
* client.on('messageCreate', (message) => {
|
|
436
|
+
* handler.handleMessage(message.content, message, {
|
|
437
|
+
* authorId: message.author.id,
|
|
438
|
+
* channelId: message.channel.id,
|
|
439
|
+
* serverId: message.server?.id,
|
|
440
|
+
* reply: (content) => message.channel.sendMessage(content),
|
|
441
|
+
* });
|
|
442
|
+
* });
|
|
443
|
+
* ```
|
|
444
|
+
*/
|
|
445
|
+
async handleMessage(rawContent, message, meta) {
|
|
446
|
+
const ctx = await this.parseMessage(rawContent, message, meta);
|
|
447
|
+
if (!ctx) {
|
|
448
|
+
return false;
|
|
449
|
+
}
|
|
450
|
+
return this.execute(ctx);
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Execute a command with the given context
|
|
454
|
+
*/
|
|
455
|
+
async execute(ctx) {
|
|
456
|
+
const registered = this.registry.get(ctx.commandName);
|
|
457
|
+
if (!registered) {
|
|
458
|
+
return false;
|
|
459
|
+
}
|
|
460
|
+
const { instance, metadata, methodName, classConstructor } = registered;
|
|
461
|
+
if (metadata.ownerOnly && !this.owners.has(ctx.authorId)) {
|
|
462
|
+
await ctx.reply("This command is owner-only.");
|
|
463
|
+
return false;
|
|
464
|
+
}
|
|
465
|
+
const guards = Reflect.getMetadata("mally:command:guards", classConstructor) || [];
|
|
466
|
+
for (const guardClass of guards) {
|
|
467
|
+
const guardInstance = new guardClass();
|
|
468
|
+
if (typeof guardInstance.run === "function") {
|
|
469
|
+
const guardResult = await guardInstance.run(ctx);
|
|
470
|
+
if (!guardResult) {
|
|
471
|
+
if (typeof guardInstance.guardFail === "function") {
|
|
472
|
+
await guardInstance.guardFail(ctx);
|
|
473
|
+
} else {
|
|
474
|
+
console.error("[Mally] Guard check failed but no guardFail method defined on", guardClass.name);
|
|
475
|
+
}
|
|
476
|
+
return false;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
if (!this.checkCooldown(ctx.authorId, metadata)) {
|
|
481
|
+
const remaining = this.getRemainingCooldown(ctx.authorId, metadata);
|
|
482
|
+
if (typeof instance.onCooldown === "function") {
|
|
483
|
+
await instance.onCooldown(ctx, remaining);
|
|
484
|
+
} else {
|
|
485
|
+
await ctx.reply(`Please wait ${(remaining / 1e3).toFixed(1)} seconds before using this command again.`);
|
|
486
|
+
}
|
|
487
|
+
return false;
|
|
488
|
+
}
|
|
489
|
+
try {
|
|
490
|
+
if (methodName) {
|
|
491
|
+
await instance[methodName](ctx);
|
|
492
|
+
} else {
|
|
493
|
+
await instance.run(ctx);
|
|
494
|
+
}
|
|
495
|
+
if (metadata.cooldown > 0) {
|
|
496
|
+
this.setCooldown(ctx.authorId, metadata);
|
|
497
|
+
}
|
|
498
|
+
return true;
|
|
499
|
+
} catch (error) {
|
|
500
|
+
if (typeof instance.onError === "function") {
|
|
501
|
+
await instance.onError(ctx, error);
|
|
502
|
+
} else {
|
|
503
|
+
console.error(`[Mally] Error in command ${metadata.name}:`, error);
|
|
504
|
+
await ctx.reply(`An error occurred: ${error.message}`);
|
|
505
|
+
}
|
|
506
|
+
return false;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* Get the command registry
|
|
511
|
+
*/
|
|
512
|
+
getRegistry() {
|
|
513
|
+
return this.registry;
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Get a command by name or alias
|
|
517
|
+
*/
|
|
518
|
+
getCommand(name) {
|
|
519
|
+
return this.registry.get(name);
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Get all commands
|
|
523
|
+
*/
|
|
524
|
+
getCommands() {
|
|
525
|
+
return this.registry.getAll();
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* Reload all commands
|
|
529
|
+
*/
|
|
530
|
+
async reload() {
|
|
531
|
+
this.registry.clear();
|
|
532
|
+
this.cooldowns.clear();
|
|
533
|
+
await this.registry.loadFromDirectory(this.commandsDir);
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* Check if a user is an owner
|
|
537
|
+
*/
|
|
538
|
+
isOwner(userId) {
|
|
539
|
+
return this.owners.has(userId);
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Add an owner
|
|
543
|
+
*/
|
|
544
|
+
addOwner(userId) {
|
|
545
|
+
this.owners.add(userId);
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Remove an owner
|
|
549
|
+
*/
|
|
550
|
+
removeOwner(userId) {
|
|
551
|
+
this.owners.delete(userId);
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Resolve the prefix for a context
|
|
555
|
+
*/
|
|
556
|
+
async resolvePrefix(serverId) {
|
|
557
|
+
if (typeof this.prefixResolver === "function") {
|
|
558
|
+
return this.prefixResolver({ serverId });
|
|
559
|
+
}
|
|
560
|
+
return this.prefixResolver;
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Check if user is on cooldown
|
|
564
|
+
*/
|
|
565
|
+
checkCooldown(userId, metadata) {
|
|
566
|
+
if (metadata.cooldown <= 0) return true;
|
|
567
|
+
const commandCooldowns = this.cooldowns.get(metadata.name);
|
|
568
|
+
if (!commandCooldowns) return true;
|
|
569
|
+
const userCooldown = commandCooldowns.get(userId);
|
|
570
|
+
if (!userCooldown) return true;
|
|
571
|
+
return Date.now() >= userCooldown;
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Get remaining cooldown time in ms
|
|
575
|
+
*/
|
|
576
|
+
getRemainingCooldown(userId, metadata) {
|
|
577
|
+
const commandCooldowns = this.cooldowns.get(metadata.name);
|
|
578
|
+
if (!commandCooldowns) return 0;
|
|
579
|
+
const userCooldown = commandCooldowns.get(userId);
|
|
580
|
+
if (!userCooldown) return 0;
|
|
581
|
+
return Math.max(0, userCooldown - Date.now());
|
|
582
|
+
}
|
|
583
|
+
/**
|
|
584
|
+
* Set cooldown for a user
|
|
585
|
+
*/
|
|
586
|
+
setCooldown(userId, metadata) {
|
|
587
|
+
if (!this.cooldowns.has(metadata.name)) {
|
|
588
|
+
this.cooldowns.set(metadata.name, /* @__PURE__ */ new Map());
|
|
589
|
+
}
|
|
590
|
+
const commandCooldowns = this.cooldowns.get(metadata.name);
|
|
591
|
+
commandCooldowns.set(userId, Date.now() + metadata.cooldown);
|
|
592
|
+
}
|
|
593
|
+
};
|
|
594
|
+
export {
|
|
595
|
+
BaseCommand,
|
|
596
|
+
Command,
|
|
597
|
+
CommandRegistry,
|
|
598
|
+
Guard,
|
|
599
|
+
METADATA_KEYS,
|
|
600
|
+
MallyHandler,
|
|
601
|
+
SimpleCommand,
|
|
602
|
+
Stoat,
|
|
603
|
+
buildCommandMetadata,
|
|
604
|
+
buildSimpleCommandMetadata,
|
|
605
|
+
getCommandOptions,
|
|
606
|
+
getGuards,
|
|
607
|
+
getSimpleCommands,
|
|
608
|
+
isCommand,
|
|
609
|
+
isStoatClass
|
|
610
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@marshmallow-stoat/mally",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A high-performance, decorator-based command handler for the Stoat ecosystem.",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"license": "AGPL-3.0-or-later",
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
24
|
+
"dev": "tsup src/index.ts --format cjs,esm --watch --dts",
|
|
25
|
+
"lint": "eslint src/**/*.ts",
|
|
26
|
+
"format": "prettier --write src/**/*.ts"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"reflect-metadata": "^0.2.2",
|
|
30
|
+
"stoat.js": "^7.3.6",
|
|
31
|
+
"tinyglobby": "^0.2.10"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@types/node": "^20.0.0",
|
|
35
|
+
"tsup": "^8.0.0",
|
|
36
|
+
"typescript": "^5.0.0",
|
|
37
|
+
"prettier": "^3.8.1"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"reflect-metadata": "^0.2.0"
|
|
41
|
+
}
|
|
42
|
+
}
|