-
Notifications
You must be signed in to change notification settings - Fork 0
/
cog.ts
64 lines (56 loc) · 2.47 KB
/
cog.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
import { CommandInteraction } from "discord.js";
import { CogSlashCommand, SlashCommand, SlashCommandMetadata } from "./command";
import { ArgumentType, CommandArgument } from "./argument";
import { CogDecoratorException } from "./errors";
export abstract class Cog
{
[key: string]: any;
static command(name? : string, description? : string)
{
return function (target: Cog, propertyKey : string, descriptor: PropertyDescriptor)
{
if (typeof descriptor.value !== "function")
throw new CogDecoratorException("@Cog.command decorator can only be applied to functions");
if (!name || name === "")
name = propertyKey;
const metadata = new SlashCommandMetadata(name, description);
const args = descriptor.value.args;
if (args != undefined)
{
for (let i = args.length - 1; i >= 0; i--)
{
metadata.addArg(args[i]);
}
}
Reflect.defineMetadata(SlashCommandMetadata.COMMAND_METADATA_KEY, metadata, target, propertyKey);
}
}
static argument(name: string, type?: ArgumentType, description?: string, required?: boolean)
{
return function(target: Cog, propertyKey: string, descriptor: PropertyDescriptor)
{
if (typeof descriptor.value !== "function")
throw new CogDecoratorException("@Cog.argument decorator can only be applied to functions");
if(!name || name === "")
throw new CogDecoratorException("Argument name cannot be empty");
descriptor.value.args = descriptor.value.args? descriptor.value.args : [];
descriptor.value.args.push(new CommandArgument(name, type, description, required));
}
}
getCommands() : SlashCommand[]
{
const commands : SlashCommand[] = [];
const classPrototype = Object.getPrototypeOf(this);
const properties = Object.getOwnPropertyNames(classPrototype);
for (const name of properties)
{
if (Reflect.hasMetadata(SlashCommandMetadata.COMMAND_METADATA_KEY, classPrototype, name))
{
const metadata = Reflect.getMetadata(SlashCommandMetadata.COMMAND_METADATA_KEY, this, name) as SlashCommandMetadata;
const command = new CogSlashCommand(metadata, this, name, );
commands.push(command);
}
}
return commands;
}
}