@pikokr/command.ts 5.0.0-dev.a0bc517 → 5.0.0-dev.a935c89
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/dist/index.d.ts +71 -29
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +6 -2
- package/src/applicationCommand/ApplicationCommand.ts +4 -17
- package/src/applicationCommand/ApplicationCommandExtension.ts +162 -0
- package/src/core/components/BaseComponent.ts +8 -13
- package/src/core/components/decoratorCreator.ts +3 -3
- package/src/core/converter/index.ts +7 -0
- package/src/core/extensions/CTSExtension.ts +9 -0
- package/src/core/extensions/Extension.ts +54 -0
- package/src/core/extensions/index.ts +1 -0
- package/src/core/index.ts +1 -0
- package/src/core/listener/index.ts +12 -1
- package/src/core/structures/CommandClient.ts +24 -2
- package/src/core/structures/Registry.ts +19 -4
- package/src/core/symbols.ts +1 -1
- package/test/index.ts +30 -24
- package/tsconfig.prod.json +1 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as discord_js from 'discord.js';
|
|
2
|
+
import { Collection, Snowflake, Interaction, ChatInputCommandInteraction, MessageContextMenuCommandInteraction, UserContextMenuCommandInteraction, Client, UserApplicationCommandData, MessageApplicationCommandData, ChatInputApplicationCommandData, ApplicationCommandType, APIApplicationCommandOption } from 'discord.js';
|
|
2
3
|
import EventEmitter from 'events';
|
|
4
|
+
import * as tslog from 'tslog';
|
|
5
|
+
import { Logger } from 'tslog';
|
|
3
6
|
|
|
4
7
|
declare class ComponentArgumentDecorator<Options = unknown> {
|
|
5
8
|
options: Options;
|
|
@@ -13,12 +16,12 @@ declare class ComponentArgument {
|
|
|
13
16
|
constructor(type: unknown);
|
|
14
17
|
}
|
|
15
18
|
|
|
16
|
-
declare class BaseComponent<Options = unknown,
|
|
17
|
-
options: Options
|
|
19
|
+
declare class BaseComponent<Options = unknown, OptionsArg = Options> {
|
|
20
|
+
options: Options;
|
|
18
21
|
method: Function;
|
|
19
22
|
argTypes: Collection<number, ComponentArgument>;
|
|
20
|
-
constructor(options:
|
|
21
|
-
|
|
23
|
+
constructor(options: OptionsArg, method: Function, argTypes: unknown[]);
|
|
24
|
+
convertOptions(options: OptionsArg): Options;
|
|
22
25
|
execute(target: object, args: unknown[]): any;
|
|
23
26
|
}
|
|
24
27
|
|
|
@@ -26,19 +29,59 @@ declare type ComponentStore = Collection<string | symbol, BaseComponent>;
|
|
|
26
29
|
declare type ComponentArgumentStore = Collection<number, ComponentArgumentDecorator>;
|
|
27
30
|
declare const getComponentStore: (target: object) => ComponentStore;
|
|
28
31
|
declare const getComponent: (target: object, key: string | symbol) => BaseComponent<unknown, unknown> | undefined;
|
|
29
|
-
declare const createComponentDecorator: <Options,
|
|
30
|
-
new (options:
|
|
31
|
-
}) => (options:
|
|
32
|
+
declare const createComponentDecorator: <Options, OptionArgs>(type: {
|
|
33
|
+
new (options: OptionArgs, method: Function, argTypes: unknown[]): BaseComponent<Options, OptionArgs>;
|
|
34
|
+
}) => (options: OptionArgs) => MethodDecorator;
|
|
32
35
|
declare const getComponentArgumentStore: (target: object, key: string | symbol) => ComponentArgumentStore;
|
|
33
36
|
declare const createArgumentDecorator: <Options>(type: {
|
|
34
37
|
new (options: Partial<Options>): ComponentArgumentDecorator<Options>;
|
|
35
38
|
}) => (options: Options) => ParameterDecorator;
|
|
36
39
|
|
|
40
|
+
declare class Extension {
|
|
41
|
+
protected get commandClient(): CommandClient;
|
|
42
|
+
protected get client(): discord_js.Client<boolean>;
|
|
43
|
+
protected _logger?: Logger;
|
|
44
|
+
protected get logger(): Logger;
|
|
45
|
+
protected convertArguments(component: typeof BaseComponent<unknown>, argList: unknown[], args: Collection<number, ComponentArgument>, getConverterArgs: (arg: ComponentArgument, index: number) => unknown[] | Promise<unknown[]>): Promise<void>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
declare class CTSExtension extends Extension {
|
|
49
|
+
protected get logger(): tslog.Logger;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
declare type ApplicationCommandExtensionConfig = {
|
|
53
|
+
guilds?: Snowflake[];
|
|
54
|
+
};
|
|
55
|
+
declare class ApplicationCommandExtension extends CTSExtension {
|
|
56
|
+
config: ApplicationCommandExtensionConfig;
|
|
57
|
+
constructor(config: ApplicationCommandExtensionConfig);
|
|
58
|
+
interactionCreate(i: Interaction): Promise<void>;
|
|
59
|
+
load(): Promise<void>;
|
|
60
|
+
sync(): Promise<void>;
|
|
61
|
+
chatInteraction(i: ChatInputCommandInteraction): Promise<ChatInputCommandInteraction<discord_js.CacheType>>;
|
|
62
|
+
messageInteraction(i: MessageContextMenuCommandInteraction): Promise<MessageContextMenuCommandInteraction<discord_js.CacheType>>;
|
|
63
|
+
userInteraction(i: UserContextMenuCommandInteraction): Promise<UserContextMenuCommandInteraction<discord_js.CacheType>>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
declare class CommandClient extends EventEmitter {
|
|
67
|
+
discord: Client;
|
|
68
|
+
logger: Logger;
|
|
69
|
+
ctsLogger: Logger;
|
|
70
|
+
registry: Registry;
|
|
71
|
+
constructor(discord: Client, logger?: Logger);
|
|
72
|
+
enableApplicationCommandsExtension(config: ApplicationCommandExtensionConfig): Promise<void>;
|
|
73
|
+
getApplicationCommandsExtension(): ApplicationCommandExtension | undefined;
|
|
74
|
+
static getFromModule(ext: object): CommandClient;
|
|
75
|
+
}
|
|
76
|
+
|
|
37
77
|
declare class Registry {
|
|
78
|
+
client: CommandClient;
|
|
38
79
|
extensions: object[];
|
|
39
80
|
emitters: Collection<string, EventEmitter>;
|
|
40
|
-
|
|
41
|
-
|
|
81
|
+
logger: Logger;
|
|
82
|
+
constructor(logger: Logger, client: CommandClient);
|
|
83
|
+
getComponentsWithTypeGlobal<T extends typeof BaseComponent<Config>, Config>(type: T): InstanceType<T>[];
|
|
84
|
+
getComponentsWithType<T extends typeof BaseComponent<Config>, Config>(ext: object, type: T): InstanceType<T>[];
|
|
42
85
|
registerEventListeners(ext: object): void;
|
|
43
86
|
unregisterEventListeners(ext: object): void;
|
|
44
87
|
registerModule(ext: object): Promise<void>;
|
|
@@ -47,32 +90,31 @@ declare class Registry {
|
|
|
47
90
|
registerEventEmitter(name: string, emitter: EventEmitter): void;
|
|
48
91
|
}
|
|
49
92
|
|
|
50
|
-
declare class CommandClient extends EventEmitter {
|
|
51
|
-
discord: Client;
|
|
52
|
-
registry: Registry;
|
|
53
|
-
constructor(discord: Client);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
93
|
declare type ModuleHookStore = Collection<string | symbol, Function[]>;
|
|
57
94
|
declare const getModuleHookStore: (target: object) => ModuleHookStore;
|
|
58
95
|
declare const moduleHook: (name: string) => MethodDecorator;
|
|
59
96
|
|
|
60
|
-
declare type
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
name: string;
|
|
65
|
-
description: string;
|
|
97
|
+
declare type Options = {
|
|
98
|
+
component: typeof BaseComponent<unknown>;
|
|
99
|
+
type: Function;
|
|
100
|
+
parameterless: boolean;
|
|
66
101
|
};
|
|
67
|
-
declare class
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
102
|
+
declare class ConverterComponent extends BaseComponent<Options, Options & {
|
|
103
|
+
parameterless?: boolean;
|
|
104
|
+
}> {
|
|
105
|
+
}
|
|
106
|
+
declare const argConverter: (options: Options & {
|
|
107
|
+
parameterless?: boolean | undefined;
|
|
108
|
+
}) => MethodDecorator;
|
|
109
|
+
|
|
110
|
+
declare class ApplicationCommandComponent extends BaseComponent<(UserApplicationCommandData | MessageApplicationCommandData | Omit<ChatInputApplicationCommandData, 'options'>) & {
|
|
111
|
+
type: ApplicationCommandType;
|
|
112
|
+
}> {
|
|
71
113
|
}
|
|
72
|
-
declare const applicationCommand: (options:
|
|
114
|
+
declare const applicationCommand: (options: (UserApplicationCommandData | MessageApplicationCommandData | Omit<ChatInputApplicationCommandData, "options">) & {
|
|
73
115
|
type: ApplicationCommandType;
|
|
74
|
-
}
|
|
116
|
+
}) => MethodDecorator;
|
|
75
117
|
|
|
76
118
|
declare const option: (options: APIApplicationCommandOption) => ParameterDecorator;
|
|
77
119
|
|
|
78
|
-
export { ApplicationCommandComponent, BaseComponent, CommandClient, Registry, applicationCommand, createArgumentDecorator, createComponentDecorator, getComponent, getComponentArgumentStore, getComponentStore, getModuleHookStore, moduleHook, option };
|
|
120
|
+
export { ApplicationCommandComponent, BaseComponent, CommandClient, ConverterComponent, Registry, applicationCommand, argConverter, createArgumentDecorator, createComponentDecorator, getComponent, getComponentArgumentStore, getComponentStore, getModuleHookStore, moduleHook, option };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var z=Object.create;var l=Object.defineProperty;var F=Object.getOwnPropertyDescriptor;var J=Object.getOwnPropertyNames;var K=Object.getPrototypeOf,N=Object.prototype.hasOwnProperty;var T=e=>l(e,"__esModule",{value:!0}),n=(e,t)=>l(e,"name",{value:t,configurable:!0});var P=(e,t)=>{for(var o in t)l(e,o,{get:t[o],enumerable:!0})},L=(e,t,o,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of J(t))!N.call(e,s)&&(o||s!=="default")&&l(e,s,{get:()=>t[s],enumerable:!(r=F(t,s))||r.enumerable});return e},h=(e,t)=>L(T(l(e!=null?z(K(e)):{},"default",!t&&e&&e.__esModule?{get:()=>e.default,enumerable:!0}:{value:e,enumerable:!0})),e),Q=(e=>(t,o)=>e&&e.get(t)||(o=L(T({}),t,1),e&&e.set(t,o),o))(typeof WeakMap!="undefined"?new WeakMap:0);var Y={};P(Y,{ApplicationCommandComponent:()=>y,BaseComponent:()=>m,CommandClient:()=>w,Registry:()=>d,applicationCommand:()=>X,createArgumentDecorator:()=>E,createComponentDecorator:()=>u,getComponent:()=>U,getComponentArgumentStore:()=>v,getComponentStore:()=>a,getModuleHookStore:()=>x,moduleHook:()=>V,option:()=>G});var pt=require("reflect-metadata");var D=require("discord.js"),O=h(require("lodash"));var g=class{constructor(t){this.type=t,this.decorators=[]}};n(g,"ComponentArgument");var m=class{argTypes=new D.Collection;constructor(t,o,r){typeof t=="object"?this.options=O.default.merge(this.defaultOptions(),t):typeof t=="string"?this.options=t:this.options=null,this.method=o;for(let s=0;s<r.length;s++){let p=r[s];this.argTypes.set(s,new g(p))}}defaultOptions(){return{}}execute(t,o){return this.method.apply(t,o)}};n(m,"BaseComponent");var M=require("discord.js");var f=Symbol(),ot=Symbol(),C=Symbol(),rt=Symbol();var a=n(e=>{let t=Reflect.getMetadata(f,e);return t||(t=new M.Collection,Reflect.defineMetadata(f,t,e)),t},"getComponentStore"),U=n((e,t)=>a(e).get(t),"getComponent"),u=n(e=>t=>(o,r)=>{var s=new e(t,Reflect.get(o,r),Reflect.getMetadata("design:paramtypes",o,r));let p=a(o);v(o,r).forEach((I,q)=>{var H;(H=s.argTypes.get(q))==null||H.decorators.push(I)}),p.set(r,s)},"createComponentDecorator"),v=n((e,t)=>{let o=Reflect.getMetadata(f,e,t);return o||(o=new M.Collection,Reflect.defineMetadata(f,o,e,t)),o},"getComponentArgumentStore"),E=n(e=>t=>(o,r,s)=>{var p=new e(t);v(o,r).set(s,p)},"createArgumentDecorator");var k=require("discord.js"),A=h(require("lodash"));var W=require("discord.js");var x=n(e=>{let t=Reflect.getMetadata(C,e);return t||(t=new W.Collection,Reflect.defineMetadata(C,t,e)),t},"getModuleHookStore"),V=n(e=>(t,o)=>{let r=x(t),s=r.get(o);s||(s=[],r.set(o,s)),s.push(Reflect.get(t,o))},"moduleHook");var c=class extends m{defaultOptions(){return{emitter:"discord"}}};n(c,"ListenerComponent");var gt=u(c);var d=class{extensions=[];emitters=new k.Collection;getComponentsWithTypeGlobal(t){let o=[];for(let r of this.extensions)o.push(...this.getComponentsWithType(r,t));return o}getComponentsWithType(t,o){let r=a(t);return Array.from(r.filter(s=>s.constructor===o).values())}registerEventListeners(t){let o=this.getComponentsWithType(t,c);for(let r of o){let s=this.emitters.get(r.options.emitter);if(s){let p=r.method.bind(t);Reflect.defineMetadata("bound",p,r),s.addListener(r.options.event,p)}}}unregisterEventListeners(t){let o=this.getComponentsWithType(t,c);for(let r of o){let s=this.emitters.get(r.options.emitter),p=Reflect.getMetadata("bound",r);s&&p&&s.removeListener(r.options.event,p)}}async registerModule(t){this.registerEventListeners(t),await this.runModuleHook(t,"load"),this.extensions.push(t)}async unregisterModule(t){this.unregisterEventListeners(t),await this.runModuleHook(t,"unload"),A.default.remove(this.extensions,o=>o===t)}runModuleHook(t,o,...r){let p=x(t).get(o);if(p)for(let b of p)b.apply(t,r)}registerEventEmitter(t,o){this.emitters.set(t,o)}};n(d,"Registry");var _=h(require("events"));var w=class extends _.default{constructor(t){super();this.discord=t,this.registry=new d,this.registry.registerEventEmitter("cts",this),this.registry.registerEventEmitter("discord",this.discord)}};n(w,"CommandClient");var j=require("discord.js");var y=class extends m{defaultOptions(){return{type:j.ApplicationCommandType.ChatInput}}};n(y,"ApplicationCommandComponent");var X=u(y);var B=h(require("lodash")),S=class{constructor(t){typeof t=="object"?this.options=B.default.merge(this.defaultOptions(),t):this.options=t}defaultOptions(){return{}}};n(S,"ComponentArgumentDecorator");var R=class extends S{};n(R,"ApplicationCommandOption");var G=E(R);module.exports=Q(Y);0&&(module.exports={ApplicationCommandComponent,BaseComponent,CommandClient,Registry,applicationCommand,createArgumentDecorator,createComponentDecorator,getComponent,getComponentArgumentStore,getComponentStore,getModuleHookStore,moduleHook,option});
|
|
1
|
+
var mt=Object.create;var v=Object.defineProperty;var at=Object.getOwnPropertyDescriptor;var pt=Object.getOwnPropertyNames,G=Object.getOwnPropertySymbols,lt=Object.getPrototypeOf,z=Object.prototype.hasOwnProperty,ft=Object.prototype.propertyIsEnumerable;var U=(o,t,e)=>t in o?v(o,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):o[t]=e,Z=(o,t)=>{for(var e in t||(t={}))z.call(t,e)&&U(o,e,t[e]);if(G)for(var e of G(t))ft.call(t,e)&&U(o,e,t[e]);return o};var q=o=>v(o,"__esModule",{value:!0}),s=(o,t)=>v(o,"name",{value:t,configurable:!0});var gt=(o,t)=>{for(var e in t)v(o,e,{get:t[e],enumerable:!0})},J=(o,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of pt(t))!z.call(o,r)&&(e||r!=="default")&&v(o,r,{get:()=>t[r],enumerable:!(n=at(t,r))||n.enumerable});return o},y=(o,t)=>J(q(v(o!=null?mt(lt(o)):{},"default",!t&&o&&o.__esModule?{get:()=>o.default,enumerable:!0}:{value:o,enumerable:!0})),o),dt=(o=>(t,e)=>o&&o.get(t)||(e=J(q({}),t,1),o&&o.set(t,e),e))(typeof WeakMap!="undefined"?new WeakMap:0);var yt={};gt(yt,{ApplicationCommandComponent:()=>l,BaseComponent:()=>u,CommandClient:()=>b,ConverterComponent:()=>w,Registry:()=>E,applicationCommand:()=>ht,argConverter:()=>_,createArgumentDecorator:()=>N,createComponentDecorator:()=>C,getComponent:()=>ut,getComponentArgumentStore:()=>W,getComponentStore:()=>T,getModuleHookStore:()=>A,moduleHook:()=>P,option:()=>et});var Tt=require("reflect-metadata");var K=require("discord.js");var I=class{constructor(t){this.type=t,this.decorators=[]}};s(I,"ComponentArgument");var u=class{argTypes=new K.Collection;constructor(t,e,n){this.options=this.convertOptions(t),this.method=e;for(let r=0;r<n.length;r++){let i=n[r];this.argTypes.set(r,new I(i))}}convertOptions(t){return t}execute(t,e){return this.method.call(t,...e)}};s(u,"BaseComponent");var D=require("discord.js");var R=Symbol(),Mt=Symbol(),k=Symbol(),L=Symbol();var T=s(o=>{let t=Reflect.getMetadata(R,o);return t||(t=new D.Collection,Reflect.defineMetadata(R,t,o)),t},"getComponentStore"),ut=s((o,t)=>T(o).get(t),"getComponent"),C=s(o=>t=>(e,n)=>{var r=new o(t,Reflect.get(e,n),Reflect.getMetadata("design:paramtypes",e,n));let i=T(e);W(e,n).forEach((m,f)=>{var h;(h=r.argTypes.get(f))==null||h.decorators.push(m)}),i.set(n,r)},"createComponentDecorator"),W=s((o,t)=>{let e=Reflect.getMetadata(R,o,t);return e||(e=new D.Collection,Reflect.defineMetadata(R,e,o,t)),e},"getComponentArgumentStore"),N=s(o=>t=>(e,n,r)=>{var i=new o(t);W(e,n).set(r,i)},"createArgumentDecorator");var O=y(require("chalk")),X=require("discord.js"),Y=y(require("lodash"));var Q=require("discord.js");var A=s(o=>{let t=Reflect.getMetadata(k,o);return t||(t=new Q.Collection,Reflect.defineMetadata(k,t,o)),t},"getModuleHookStore"),P=s(o=>(t,e)=>{let n=A(t),r=n.get(e);r||(r=[],n.set(e,r)),r.push(Reflect.get(t,e))},"moduleHook");var M=class extends u{defaultOptions(){return{emitter:"discord"}}constructor(t,e,n){super({event:t.event,emitter:(r=t.emitter)!=null?r:"discord"},e,n);var r}};s(M,"ListenerComponent");var V=C(M);var E=class{constructor(t,e){this.client=e,this.extensions=[],this.emitters=new X.Collection,this.logger=t.getChildLogger({prefix:[O.default.green("[Registry]")]})}getComponentsWithTypeGlobal(t){let e=[];for(let n of this.extensions)e.push(...this.getComponentsWithType(n,t));return e}getComponentsWithType(t,e){let n=T(t);return Array.from(n.filter(r=>r.constructor===e).values())}registerEventListeners(t){let e=this.getComponentsWithType(t,M);for(let n of e){let r=this.emitters.get(n.options.emitter);if(r){let i=n.method.bind(t);Reflect.defineMetadata("bound",i,n),r.addListener(n.options.event,i)}}}unregisterEventListeners(t){let e=this.getComponentsWithType(t,M);for(let n of e){let r=this.emitters.get(n.options.emitter),i=Reflect.getMetadata("bound",n);r&&i&&r.removeListener(n.options.event,i)}}async registerModule(t){Reflect.defineMetadata(L,this.client,t),this.registerEventListeners(t),await this.runModuleHook(t,"load"),this.extensions.push(t),this.logger.info(`Module registered: ${O.default.green(t.constructor.name)}`)}async unregisterModule(t){this.unregisterEventListeners(t),await this.runModuleHook(t,"unload"),Y.default.remove(this.extensions,e=>e===t),this.logger.info(`Module unregistered: ${O.default.green(t.constructor.name)}`)}runModuleHook(t,e,...n){let i=A(t).get(e);if(i)for(let c of i)c.call(t,...n)}registerEventEmitter(t,e){this.emitters.set(t,e)}};s(E,"Registry");var st=y(require("chalk")),it=y(require("events")),ct=require("tslog");var x=y(require("chalk")),p=require("discord.js");var l=class extends u{};s(l,"ApplicationCommandComponent");var ht=C(l);var tt=y(require("lodash")),$=class{constructor(t){typeof t=="object"?this.options=tt.default.merge(this.defaultOptions(),t):this.options=t}defaultOptions(){return{}}};s($,"ComponentArgumentDecorator");var S=class extends ${};s(S,"ApplicationCommandOption");var et=N(S);var w=class extends u{};s(w,"ConverterComponent");var _=C(w);var nt=y(require("chalk"));var ot=y(require("chalk")),rt=require("discord.js");var j=class{get commandClient(){return b.getFromModule(this)}get client(){return this.commandClient.discord}get logger(){return this._logger||(this._logger=this.commandClient.logger.getChildLogger({prefix:[ot.default.green(`[${this.constructor.name}]`)],displayFunctionName:!1})),this._logger}async convertArguments(t,e,n,r){let i=new rt.Collection;for(let c of this.commandClient.registry.extensions)for(let m of this.commandClient.registry.getComponentsWithType(c,w))m.options.component==t&&i.set(m.options.type,{component:m,ext:c});for(let[c,m]of n){let f=i.get(m.type);if(!f){e[c]=void 0;continue}let h=await r(m,c);e[c]=await f.component.execute(f.ext,h)}}};s(j,"Extension");var H=class extends j{get logger(){return this._logger||(this._logger=this.commandClient.ctsLogger.getChildLogger({prefix:[nt.default.green(`[${this.constructor.name}]`)],displayFunctionName:!1})),this._logger}};s(H,"CTSExtension");var F=function(o,t,e,n){var r=arguments.length,i=r<3?t:n===null?n=Object.getOwnPropertyDescriptor(t,e):n,c;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")i=Reflect.decorate(o,t,e,n);else for(var m=o.length-1;m>=0;m--)(c=o[m])&&(i=(r<3?c(i):r>3?c(t,e,i):c(t,e))||i);return r>3&&i&&Object.defineProperty(t,e,i),i},g=function(o,t){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(o,t)},d=class extends H{constructor(t){super();this.config=t}async interactionCreate(t){var i;if(t.type!==p.InteractionType.ApplicationCommand)return;let e=null,n=null,r=this.commandClient.registry.extensions;for(let c of r){let m=this.commandClient.registry.getComponentsWithType(c,l);for(let f of m)f.options.name===t.commandName&&(n=c,e=f)}if(e&&n){let c=[];await this.convertArguments(l,c,e.argTypes,()=>[t]);for(let[m,f]of e.argTypes){let h=null;for(let B of f.decorators)if(B instanceof S){h=(i=t.options.get(B.options.name,!1))==null?void 0:i.value;break}h&&(c[m]=h)}try{await e.execute(n,c)}catch(m){this.logger.error(m),this.commandClient.emit("applicationCommandInvokeError",m,t)}}}async load(){}async sync(){let t=b.getFromModule(this);this.logger.info("Trying to sync commands...");let e=[];for(let n of t.registry.getComponentsWithTypeGlobal(l)){let r=Z({},n.options);if(r.type===p.ApplicationCommandType.ChatInput){r.options=[];for(let[,i]of n.argTypes){let c=i.decorators.find(m=>m.constructor===S);c&&r.options.push(c.options)}}e.push(r)}if(this.logger.info(`Processing ${x.default.green(e.length)} commands(${e.map(n=>x.default.blue(n.name)).join(", ")})`),this.config.guilds)for(let n of this.config.guilds)try{let r=await this.client.guilds.fetch(n);await r.fetch(),this.logger.info(`Registering commands for guild ${x.default.green(r.name)}(${x.default.blue(r.id)})`),await r.commands.set(e),this.logger.info(`Successfully registered commands for guild ${x.default.green(r.name)}(${x.default.blue(r.id)})`)}catch(r){this.logger.error(`Failed to register commands to guild ${x.default.green(n)}: ${r.message}`)}else try{this.logger.info("Registering commands globally..."),await this.client.application.commands.set(e),this.logger.info("Successfully registered commands.")}catch(n){this.logger.error(`Failed to register commands to global: ${n.message}`)}}async chatInteraction(t){return t}async messageInteraction(t){return t}async userInteraction(t){return t}};s(d,"ApplicationCommandExtension");F([V({event:"interactionCreate"}),g("design:type",Function),g("design:paramtypes",[typeof p.Interaction=="undefined"?Object:p.Interaction])],d.prototype,"interactionCreate",null);F([P("load"),g("design:type",Function),g("design:paramtypes",[])],d.prototype,"load",null);F([_({component:l,parameterless:!0,type:p.ChatInputCommandInteraction}),g("design:type",Function),g("design:paramtypes",[typeof p.ChatInputCommandInteraction=="undefined"?Object:p.ChatInputCommandInteraction])],d.prototype,"chatInteraction",null);F([_({component:l,parameterless:!0,type:p.MessageContextMenuCommandInteraction}),g("design:type",Function),g("design:paramtypes",[typeof p.MessageContextMenuCommandInteraction=="undefined"?Object:p.MessageContextMenuCommandInteraction])],d.prototype,"messageInteraction",null);F([_({component:l,parameterless:!0,type:p.UserContextMenuCommandInteraction}),g("design:type",Function),g("design:paramtypes",[typeof p.UserContextMenuCommandInteraction=="undefined"?Object:p.UserContextMenuCommandInteraction])],d.prototype,"userInteraction",null);var b=class extends it.default{constructor(t,e=new ct.Logger({dateTimeTimezone:Intl.DateTimeFormat().resolvedOptions().timeZone})){super();this.discord=t,this.logger=e,this.ctsLogger=e.getChildLogger({prefix:[st.default.blue("[command.ts]")],displayFilePath:"hidden",displayFunctionName:!1}),this.registry=new E(this.ctsLogger,this),this.registry.registerEventEmitter("cts",this),this.registry.registerEventEmitter("discord",this.discord)}async enableApplicationCommandsExtension(t){await this.registry.registerModule(new d(t)),this.ctsLogger.info("Application command extension enabled.")}getApplicationCommandsExtension(){return this.registry.extensions.find(t=>t.constructor===d)}static getFromModule(t){return Reflect.getMetadata(L,t)}};s(b,"CommandClient");module.exports=dt(yt);0&&(module.exports={ApplicationCommandComponent,BaseComponent,CommandClient,ConverterComponent,Registry,applicationCommand,argConverter,createArgumentDecorator,createComponentDecorator,getComponent,getComponentArgumentStore,getComponentStore,getModuleHookStore,moduleHook,option});
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/home/runner/work/command.ts/command.ts/src/index.ts","../src/core/components/home/runner/work/command.ts/command.ts/src/core/components/index.ts","../src/core/components/home/runner/work/command.ts/command.ts/src/core/components/BaseComponent.ts","../src/core/components/home/runner/work/command.ts/command.ts/src/core/components/ComponentArgument.ts","../src/core/components/home/runner/work/command.ts/command.ts/src/core/components/decoratorCreator.ts","../src/core/home/runner/work/command.ts/command.ts/src/core/symbols.ts","../src/core/structures/home/runner/work/command.ts/command.ts/src/core/structures/Registry.ts","../src/core/hooks/home/runner/work/command.ts/command.ts/src/core/hooks/moduleHook.ts","../src/core/listener/home/runner/work/command.ts/command.ts/src/core/listener/index.ts","../src/core/structures/home/runner/work/command.ts/command.ts/src/core/structures/CommandClient.ts","../src/applicationCommand/home/runner/work/command.ts/command.ts/src/applicationCommand/ApplicationCommand.ts","../src/core/components/home/runner/work/command.ts/command.ts/src/core/components/ComponentArgumentDecorator.ts","../src/applicationCommand/home/runner/work/command.ts/command.ts/src/applicationCommand/ApplicationCommandOption.ts"],"sourcesContent":["/*\n * File: index.ts\n *\n * Copyright (c) 2022-2022 pikokr\n *\n * Licensed under MIT License. Please see more defails in LICENSE file.\n */\n\nexport * from './core'\nexport * from './applicationCommand'\n","import 'reflect-metadata'\nexport * from './BaseComponent'\nexport * from './decoratorCreator'\n","import { Collection } from 'discord.js'\nimport _ from 'lodash'\nimport { ComponentArgument } from './ComponentArgument'\n\nexport class BaseComponent<Options = unknown, RequiredOptions = unknown> {\n options: Options & RequiredOptions\n\n method: Function\n\n argTypes: Collection<number, ComponentArgument> = new Collection()\n\n constructor(options: Partial<Options> & RequiredOptions, method: Function, argTypes: unknown[]) {\n if (typeof options === 'object') {\n this.options = _.merge(this.defaultOptions(), options)\n } else if (typeof options === 'string') {\n this.options = options as this['options']\n } else {\n this.options = null as unknown as this['options']\n }\n this.method = method\n for (let i = 0; i < argTypes.length; i++) {\n const element = argTypes[i]\n this.argTypes.set(i, new ComponentArgument(element))\n }\n }\n\n defaultOptions(): Options & Partial<RequiredOptions> {\n return {} as unknown as ReturnType<this['defaultOptions']>\n }\n\n execute(target: object, args: unknown[]) {\n return this.method.apply(target, args)\n }\n}\n","import { ComponentArgumentDecorator } from './ComponentArgumentDecorator'\n\nexport class ComponentArgument {\n decorators: ComponentArgumentDecorator[] = []\n\n constructor(public type: unknown) {}\n}\n","import { Collection } from 'discord.js'\nimport { ComponentStoreSymbol } from '../symbols'\nimport { BaseComponent } from './BaseComponent'\nimport { ComponentArgumentDecorator } from './ComponentArgumentDecorator'\n\ntype ComponentStore = Collection<string | symbol, BaseComponent>\ntype ComponentArgumentStore = Collection<number, ComponentArgumentDecorator>\n\nexport const getComponentStore = (target: object): ComponentStore => {\n let result: ComponentStore | null = Reflect.getMetadata(ComponentStoreSymbol, target)\n\n if (!result) {\n result = new Collection()\n\n Reflect.defineMetadata(ComponentStoreSymbol, result, target)\n }\n\n return result\n}\n\nexport const getComponent = (target: object, key: string | symbol) => {\n const store = getComponentStore(target)\n\n return store.get(key)\n}\n\nexport const createComponentDecorator = <Options, RequiredOptions>(type: typeof BaseComponent<Options, RequiredOptions>) => {\n return (options: Partial<Options> & RequiredOptions): MethodDecorator => {\n return (target, key) => {\n var component: BaseComponent<Options> = new type(options, Reflect.get(target, key), Reflect.getMetadata('design:paramtypes', target, key))\n\n const store = getComponentStore(target)\n\n const decorators = getComponentArgumentStore(target, key)\n\n decorators.forEach((x, i) => {\n component.argTypes.get(i)?.decorators.push(x)\n })\n\n store.set(key, component)\n }\n }\n}\n\nexport const getComponentArgumentStore = (target: object, key: string | symbol): ComponentArgumentStore => {\n let result: ComponentArgumentStore | null = Reflect.getMetadata(ComponentStoreSymbol, target, key)\n\n if (!result) {\n result = new Collection()\n\n Reflect.defineMetadata(ComponentStoreSymbol, result, target, key)\n }\n\n return result\n}\n\nexport const createArgumentDecorator = <Options>(type: typeof ComponentArgumentDecorator<Options>) => {\n return (options: Options): ParameterDecorator => {\n return (target, key, idx) => {\n var arg: ComponentArgumentDecorator<Options> = new type(options)\n\n const store = getComponentArgumentStore(target, key)\n\n store.set(idx, arg)\n }\n }\n}\n","export const ComponentStoreSymbol = Symbol()\nexport const ComponentArgStoreSymbol = Symbol()\nexport const ModuleHookStoreSymbol = Symbol()\nexport const ListenersSymbol = Symbol()\n","import { Collection } from 'discord.js'\nimport EventEmitter from 'events'\nimport _ from 'lodash'\nimport { BaseComponent, getComponentStore } from '../components'\nimport { getModuleHookStore } from '../hooks'\nimport { ListenerComponent } from '../listener'\nimport { ListenersSymbol } from '../symbols'\n\nexport class Registry {\n extensions: object[] = []\n\n emitters: Collection<string, EventEmitter> = new Collection()\n\n getComponentsWithTypeGlobal<T extends typeof BaseComponent<Config, RequiredConfig>, Config, RequiredConfig>(type: T): InstanceType<T>[] {\n const result: InstanceType<T>[] = []\n\n for (const ext of this.extensions) {\n result.push(...this.getComponentsWithType(ext, type))\n }\n\n return result\n }\n\n getComponentsWithType<T extends typeof BaseComponent<Config, RequiredConfig>, Config, RequiredConfig>(ext: object, type: T): InstanceType<T>[] {\n const componentStore = getComponentStore(ext)\n\n return Array.from(componentStore.filter((x) => (x.constructor as unknown) === type).values() as Iterable<InstanceType<T>>)\n }\n\n registerEventListeners(ext: object) {\n const listeners = this.getComponentsWithType(ext, ListenerComponent)\n\n for (const listener of listeners) {\n const emitter = this.emitters.get(listener.options.emitter)\n\n if (emitter) {\n const bound = listener.method.bind(ext)\n\n Reflect.defineMetadata('bound', bound, listener)\n\n emitter.addListener(listener.options.event, bound)\n }\n }\n }\n\n unregisterEventListeners(ext: object) {\n const listeners = this.getComponentsWithType(ext, ListenerComponent)\n\n for (const listener of listeners) {\n const emitter = this.emitters.get(listener.options.emitter)\n const bound = Reflect.getMetadata('bound', listener)\n\n if (emitter && bound) {\n emitter.removeListener(listener.options.event, bound)\n }\n }\n }\n\n async registerModule(ext: object) {\n this.registerEventListeners(ext)\n await this.runModuleHook(ext, 'load')\n this.extensions.push(ext)\n }\n\n async unregisterModule(ext: object) {\n this.unregisterEventListeners(ext)\n await this.runModuleHook(ext, 'unload')\n _.remove(this.extensions, (x) => x === ext)\n }\n\n runModuleHook(ext: object, hookName: string, ...args: unknown[]) {\n const hooks = getModuleHookStore(ext)\n\n const functions = hooks.get(hookName)\n\n if (functions) {\n for (const fn of functions) {\n fn.apply(ext, args)\n }\n }\n }\n\n registerEventEmitter(name: string, emitter: EventEmitter) {\n this.emitters.set(name, emitter)\n }\n}\n","import { Collection } from 'discord.js'\nimport { ModuleHookStoreSymbol } from '../symbols'\n\ntype ModuleHookStore = Collection<string | symbol, Function[]>\n\nexport const getModuleHookStore = (target: object) => {\n let result: ModuleHookStore | null = Reflect.getMetadata(ModuleHookStoreSymbol, target)\n\n if (!result) {\n result = new Collection()\n\n Reflect.defineMetadata(ModuleHookStoreSymbol, result, target)\n }\n\n return result\n}\n\nexport const moduleHook = (name: string): MethodDecorator => {\n return (target, key) => {\n const store = getModuleHookStore(target)\n\n let v = store.get(key)\n\n if (!v) {\n v = []\n store.set(key, v)\n }\n\n v.push(Reflect.get(target, key))\n }\n}\n","import { BaseComponent, createComponentDecorator } from '../components'\n\nexport class ListenerComponent extends BaseComponent<{ emitter: string }, { event: string }> {\n defaultOptions() {\n return { emitter: 'discord' }\n }\n}\n\nexport const listener = createComponentDecorator(ListenerComponent)\n","import { Client } from 'discord.js'\nimport EventEmitter from 'events'\nimport { Registry } from './Registry'\n\nexport class CommandClient extends EventEmitter {\n registry = new Registry()\n\n constructor(public discord: Client) {\n super()\n\n this.registry.registerEventEmitter('cts', this)\n this.registry.registerEventEmitter('discord', this.discord)\n }\n}\n","import { ApplicationCommandType } from 'discord.js'\nimport { BaseComponent, createComponentDecorator } from '../core'\n\ntype ApplicationCommandOptions = {\n type: ApplicationCommandType\n}\n\ntype ApplicationCommandRequiredOptions = {\n name: string\n description: string\n}\n\nexport class ApplicationCommandComponent extends BaseComponent<ApplicationCommandOptions, ApplicationCommandRequiredOptions> {\n defaultOptions() {\n return {\n type: ApplicationCommandType.ChatInput,\n }\n }\n}\n\nexport const applicationCommand = createComponentDecorator(ApplicationCommandComponent)\n","import _ from 'lodash'\n\nexport class ComponentArgumentDecorator<Options = unknown> {\n options: Options\n\n constructor(options: Partial<Options>) {\n if (typeof options === 'object') {\n this.options = _.merge(this.defaultOptions(), options)\n } else {\n this.options = options\n }\n }\n\n defaultOptions(): Options {\n return {} as unknown as Options\n }\n}\n","import { APIApplicationCommandOption } from 'discord.js'\nimport { createArgumentDecorator } from '../core'\nimport { ComponentArgumentDecorator } from '../core/components/ComponentArgumentDecorator'\n\ntype Options = APIApplicationCommandOption\n\nexport class ApplicationCommandOption extends ComponentArgumentDecorator<Options> {}\n\nexport const option = createArgumentDecorator(ApplicationCommandOption)\n"],"mappings":"ouBAAA,iUCAA,OAAO,4BCAP,MAA2B,sBAC3B,EAAc,qBCCP,WAAuB,CAG5B,YAAmB,EAAe,MAAf,KAAA,OAFnB,WAA2C,KADtC,yBDEA,WAAmB,CAKxB,SAAkD,GAAI,cAEtD,YAAY,EAA6C,EAAkB,EAAqB,CAC9F,AAAI,MAAO,IAAY,SACrB,KAAK,QAAU,UAAE,MAAM,KAAK,iBAAkB,GACzC,AAAI,MAAO,IAAY,SAC5B,KAAK,QAAU,EAEf,KAAK,QAAU,KAEjB,KAAK,OAAS,EACd,OAAS,GAAI,EAAG,EAAI,EAAS,OAAQ,IAAK,CACxC,GAAM,GAAU,EAAS,GACzB,KAAK,SAAS,IAAI,EAAG,GAAI,GAAkB,KAI/C,gBAAqD,CACnD,MAAO,GAGT,QAAQ,EAAgB,EAAiB,CACvC,MAAO,MAAK,OAAO,MAAM,EAAQ,KA3B9B,qBEJP,MAA2B,sBCApB,GAAM,GAAuB,SACvB,GAA0B,SAC1B,EAAwB,SACxB,GAAkB,SDKxB,GAAM,GAAoB,EAAC,GAAmC,CACnE,GAAI,GAAgC,QAAQ,YAAY,EAAsB,GAE9E,MAAK,IACH,GAAS,GAAI,cAEb,QAAQ,eAAe,EAAsB,EAAQ,IAGhD,GATwB,qBAYpB,EAAe,GAAC,EAAgB,IAGpC,AAFO,EAAkB,GAEnB,IAAI,GAHS,gBAMf,EAA2B,EAA2B,GAC1D,AAAC,GACC,CAAC,EAAQ,IAAQ,CACtB,GAAI,GAAoC,GAAI,GAAK,EAAS,QAAQ,IAAI,EAAQ,GAAM,Q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,aAH5B,mCAQA,GAAM,GAAqB,EAAyB,GCpB3D,MAAc,qBAEP,OAAgC,CAGrC,YAAY,EAA2B,CACrC,AAAI,MAAO,IAAY,SACrB,KAAK,QAAU,UAAE,MAAM,KAAK,iBAAkB,GAE9C,KAAK,QAAU,EAInB,gBAA0B,CACxB,MAAO,KAZJ,kCCIA,mBAAuC,EAA0B,GAAjE,gCAEA,GAAM,GAAS,EAAwB","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/home/runner/work/command.ts/command.ts/src/index.ts","../src/core/components/home/runner/work/command.ts/command.ts/src/core/components/index.ts","../src/core/components/home/runner/work/command.ts/command.ts/src/core/components/BaseComponent.ts","../src/core/components/home/runner/work/command.ts/command.ts/src/core/components/ComponentArgument.ts","../src/core/components/home/runner/work/command.ts/command.ts/src/core/components/decoratorCreator.ts","../src/core/home/runner/work/command.ts/command.ts/src/core/symbols.ts","../src/core/structures/home/runner/work/command.ts/command.ts/src/core/structures/Registry.ts","../src/core/hooks/home/runner/work/command.ts/command.ts/src/core/hooks/moduleHook.ts","../src/core/listener/home/runner/work/command.ts/command.ts/src/core/listener/index.ts","../src/core/structures/home/runner/work/command.ts/command.ts/src/core/structures/CommandClient.ts","../src/applicationCommand/home/runner/work/command.ts/command.ts/src/applicationCommand/ApplicationCommandExtension.ts","../src/applicationCommand/home/runner/work/command.ts/command.ts/src/applicationCommand/ApplicationCommand.ts","../src/core/components/home/runner/work/command.ts/command.ts/src/core/components/ComponentArgumentDecorator.ts","../src/applicationCommand/home/runner/work/command.ts/command.ts/src/applicationCommand/ApplicationCommandOption.ts","../src/core/converter/home/runner/work/command.ts/command.ts/src/core/converter/index.ts","../src/core/extensions/home/runner/work/command.ts/command.ts/src/core/extensions/CTSExtension.ts","../src/core/extensions/home/runner/work/command.ts/command.ts/src/core/extensions/Extension.ts"],"sourcesContent":["/*\n * File: index.ts\n *\n * Copyright (c) 2022-2022 pikokr\n *\n * Licensed under MIT License. Please see more defails in LICENSE file.\n */\n\nexport * from './core'\nexport * from './applicationCommand'\n","import 'reflect-metadata'\nexport * from './BaseComponent'\nexport * from './decoratorCreator'\n","import { Collection } from 'discord.js'\nimport _ from 'lodash'\nimport { ComponentArgument } from './ComponentArgument'\n\nexport class BaseComponent<Options = unknown, OptionsArg = Options> {\n options: Options\n\n method: Function\n\n argTypes: Collection<number, ComponentArgument> = new Collection()\n\n constructor(options: OptionsArg, method: Function, argTypes: unknown[]) {\n this.options = this.convertOptions(options)\n\n this.method = method\n for (let i = 0; i < argTypes.length; i++) {\n const element = argTypes[i]\n this.argTypes.set(i, new ComponentArgument(element))\n }\n }\n\n convertOptions(options: OptionsArg): Options {\n return options as unknown as Options\n }\n\n execute(target: object, args: unknown[]) {\n return this.method.call(target, ...args)\n }\n}\n","import { ComponentArgumentDecorator } from './ComponentArgumentDecorator'\n\nexport class ComponentArgument {\n decorators: ComponentArgumentDecorator[] = []\n\n constructor(public type: unknown) {}\n}\n","import { Collection } from 'discord.js'\nimport { ComponentStoreSymbol } from '../symbols'\nimport { BaseComponent } from './BaseComponent'\nimport { ComponentArgumentDecorator } from './ComponentArgumentDecorator'\n\ntype ComponentStore = Collection<string | symbol, BaseComponent>\ntype ComponentArgumentStore = Collection<number, ComponentArgumentDecorator>\n\nexport const getComponentStore = (target: object): ComponentStore => {\n let result: ComponentStore | null = Reflect.getMetadata(ComponentStoreSymbol, target)\n\n if (!result) {\n result = new Collection()\n\n Reflect.defineMetadata(ComponentStoreSymbol, result, target)\n }\n\n return result\n}\n\nexport const getComponent = (target: object, key: string | symbol) => {\n const store = getComponentStore(target)\n\n return store.get(key)\n}\n\nexport const createComponentDecorator = <Options, OptionArgs>(type: typeof BaseComponent<Options, OptionArgs>) => {\n return (options: OptionArgs): MethodDecorator => {\n return (target, key) => {\n var component: BaseComponent<Options, OptionArgs> = new type(options, Reflect.get(target, key), Reflect.getMetadata('design:paramtypes', target, key))\n\n const store = getComponentStore(target)\n\n const decorators = getComponentArgumentStore(target, key)\n\n decorators.forEach((x, i) => {\n component.argTypes.get(i)?.decorators.push(x)\n })\n\n store.set(key, component)\n }\n }\n}\n\nexport const getComponentArgumentStore = (target: object, key: string | symbol): ComponentArgumentStore => {\n let result: ComponentArgumentStore | null = Reflect.getMetadata(ComponentStoreSymbol, target, key)\n\n if (!result) {\n result = new Collection()\n\n Reflect.defineMetadata(ComponentStoreSymbol, result, target, key)\n }\n\n return result\n}\n\nexport const createArgumentDecorator = <Options>(type: typeof ComponentArgumentDecorator<Options>) => {\n return (options: Options): ParameterDecorator => {\n return (target, key, idx) => {\n var arg: ComponentArgumentDecorator<Options> = new type(options)\n\n const store = getComponentArgumentStore(target, key)\n\n store.set(idx, arg)\n }\n }\n}\n","export const ComponentStoreSymbol = Symbol()\nexport const ComponentArgStoreSymbol = Symbol()\nexport const ModuleHookStoreSymbol = Symbol()\nexport const CommandClientSymbol = Symbol()\n","import chalk from 'chalk'\nimport { Collection } from 'discord.js'\nimport EventEmitter from 'events'\nimport _ from 'lodash'\nimport { Logger } from 'tslog'\nimport { BaseComponent, getComponentStore } from '../components'\nimport { getModuleHookStore } from '../hooks'\nimport { ListenerComponent } from '../listener'\nimport { CommandClientSymbol } from '../symbols'\nimport { CommandClient } from './CommandClient'\n\nexport class Registry {\n extensions: object[] = []\n\n emitters: Collection<string, EventEmitter> = new Collection()\n\n logger: Logger\n\n constructor(logger: Logger, public client: CommandClient) {\n this.logger = logger.getChildLogger({\n prefix: [chalk.green('[Registry]')],\n })\n }\n\n getComponentsWithTypeGlobal<T extends typeof BaseComponent<Config>, Config>(type: T): InstanceType<T>[] {\n const result: InstanceType<T>[] = []\n\n for (const ext of this.extensions) {\n result.push(...this.getComponentsWithType(ext, type))\n }\n\n return result\n }\n\n getComponentsWithType<T extends typeof BaseComponent<Config>, Config>(ext: object, type: T): InstanceType<T>[] {\n const componentStore = getComponentStore(ext)\n\n return Array.from(componentStore.filter((x) => (x.constructor as unknown) === type).values() as Iterable<InstanceType<T>>)\n }\n\n registerEventListeners(ext: object) {\n const listeners = this.getComponentsWithType(ext, ListenerComponent)\n\n for (const listener of listeners) {\n const emitter = this.emitters.get(listener.options.emitter)\n\n if (emitter) {\n const bound = listener.method.bind(ext)\n\n Reflect.defineMetadata('bound', bound, listener)\n\n emitter.addListener(listener.options.event, bound)\n }\n }\n }\n\n unregisterEventListeners(ext: object) {\n const listeners = this.getComponentsWithType(ext, ListenerComponent)\n\n for (const listener of listeners) {\n const emitter = this.emitters.get(listener.options.emitter)\n const bound = Reflect.getMetadata('bound', listener)\n\n if (emitter && bound) {\n emitter.removeListener(listener.options.event, bound)\n }\n }\n }\n\n async registerModule(ext: object) {\n Reflect.defineMetadata(CommandClientSymbol, this.client, ext)\n\n this.registerEventListeners(ext)\n await this.runModuleHook(ext, 'load')\n this.extensions.push(ext)\n this.logger.info(`Module registered: ${chalk.green(ext.constructor.name)}`)\n }\n\n async unregisterModule(ext: object) {\n this.unregisterEventListeners(ext)\n await this.runModuleHook(ext, 'unload')\n _.remove(this.extensions, (x) => x === ext)\n this.logger.info(`Module unregistered: ${chalk.green(ext.constructor.name)}`)\n }\n\n runModuleHook(ext: object, hookName: string, ...args: unknown[]) {\n const hooks = getModuleHookStore(ext)\n\n const functions = hooks.get(hookName)\n\n if (functions) {\n for (const fn of functions) {\n fn.call(ext, ...args)\n }\n }\n }\n\n registerEventEmitter(name: string, emitter: EventEmitter) {\n this.emitters.set(name, emitter)\n }\n}\n","import { Collection } from 'discord.js'\nimport { ModuleHookStoreSymbol } from '../symbols'\n\ntype ModuleHookStore = Collection<string | symbol, Function[]>\n\nexport const getModuleHookStore = (target: object) => {\n let result: ModuleHookStore | null = Reflect.getMetadata(ModuleHookStoreSymbol, target)\n\n if (!result) {\n result = new Collection()\n\n Reflect.defineMetadata(ModuleHookStoreSymbol, result, target)\n }\n\n return result\n}\n\nexport const moduleHook = (name: string): MethodDecorator => {\n return (target, key) => {\n const store = getModuleHookStore(target)\n\n let v = store.get(key)\n\n if (!v) {\n v = []\n store.set(key, v)\n }\n\n v.push(Reflect.get(target, key))\n }\n}\n","import { BaseComponent, createComponentDecorator } from '../components'\n\nexport class ListenerComponent extends BaseComponent<{ emitter: string; event: string }, { emitter?: string; event: string }> {\n defaultOptions() {\n return { emitter: 'discord' }\n }\n\n constructor(options: ListenerComponent['options'], method: Function, argTypes: unknown[]) {\n super(\n {\n event: options.event,\n emitter: options.emitter ?? 'discord',\n },\n method,\n argTypes,\n )\n }\n}\n\nexport const listener = createComponentDecorator(ListenerComponent)\n","import chalk from 'chalk'\nimport { Client } from 'discord.js'\nimport EventEmitter from 'events'\nimport { Logger } from 'tslog'\nimport { ApplicationCommandExtension, ApplicationCommandExtensionConfig } from '../../applicationCommand/ApplicationCommandExtension'\nimport { CommandClientSymbol } from '../symbols'\nimport { Registry } from './Registry'\n\nexport class CommandClient extends EventEmitter {\n ctsLogger: Logger\n registry: Registry\n\n constructor(public discord: Client, public logger: Logger = new Logger({ dateTimeTimezone: Intl.DateTimeFormat().resolvedOptions().timeZone })) {\n super()\n\n this.ctsLogger = logger.getChildLogger({ prefix: [chalk.blue('[command.ts]')], displayFilePath: 'hidden', displayFunctionName: false })\n\n this.registry = new Registry(this.ctsLogger, this)\n\n this.registry.registerEventEmitter('cts', this)\n this.registry.registerEventEmitter('discord', this.discord)\n }\n\n async enableApplicationCommandsExtension(config: ApplicationCommandExtensionConfig) {\n await this.registry.registerModule(new ApplicationCommandExtension(config))\n this.ctsLogger.info('Application command extension enabled.')\n }\n\n getApplicationCommandsExtension() {\n return this.registry.extensions.find((x) => x.constructor === ApplicationCommandExtension) as ApplicationCommandExtension | undefined\n }\n\n static getFromModule(ext: object): CommandClient {\n return Reflect.getMetadata(CommandClientSymbol, ext)\n }\n}\n","import chalk from 'chalk'\nimport {\n ApplicationCommandData,\n ApplicationCommandType,\n ChatInputCommandInteraction,\n GuildAuditLogs,\n Interaction,\n InteractionType,\n MessageContextMenuCommandInteraction,\n Snowflake,\n UserContextMenuCommandInteraction,\n} from 'discord.js'\nimport { ApplicationCommandComponent } from '.'\nimport { ApplicationCommandOption } from './ApplicationCommandOption'\nimport { moduleHook } from '../core/hooks'\nimport { listener } from '../core/listener'\nimport { CommandClient } from '../core/structures'\nimport { argConverter } from '../core/converter'\nimport { CTSExtension } from '../core/extensions/CTSExtension'\n\nexport type ApplicationCommandExtensionConfig = {\n guilds?: Snowflake[]\n}\n\nexport class ApplicationCommandExtension extends CTSExtension {\n constructor(public config: ApplicationCommandExtensionConfig) {\n super()\n }\n\n @listener({ event: 'interactionCreate' })\n async interactionCreate(i: Interaction) {\n if (i.type !== InteractionType.ApplicationCommand) return\n\n let cmd: ApplicationCommandComponent | null = null\n let ext: object | null = null\n\n const extensions = this.commandClient.registry.extensions\n\n for (const extension of extensions) {\n const components = this.commandClient.registry.getComponentsWithType(extension, ApplicationCommandComponent)\n\n for (const command of components) {\n if (command.options.name === i.commandName) {\n ext = extension\n cmd = command\n }\n }\n }\n\n if (cmd && ext) {\n const argList: unknown[] = []\n\n await this.convertArguments(ApplicationCommandComponent, argList, cmd.argTypes, () => [i])\n\n for (const [idx, arg] of cmd.argTypes) {\n let value: unknown = null\n\n for (const decorator of arg.decorators) {\n if (decorator instanceof ApplicationCommandOption) {\n value = i.options.get(decorator.options.name, false)?.value\n break\n }\n }\n\n if (value) {\n argList[idx] = value\n }\n }\n\n try {\n await cmd.execute(ext, argList)\n } catch (e) {\n this.logger.error(e)\n this.commandClient.emit('applicationCommandInvokeError', e, i)\n }\n }\n }\n\n @moduleHook('load')\n async load() {}\n\n async sync() {\n const client = CommandClient.getFromModule(this)\n\n this.logger.info('Trying to sync commands...')\n\n const commands: ApplicationCommandData[] = []\n\n for (const command of client.registry.getComponentsWithTypeGlobal(ApplicationCommandComponent)) {\n const cmd: ApplicationCommandData = { ...command.options }\n\n if (cmd.type === ApplicationCommandType.ChatInput) {\n cmd.options = []\n\n for (const [, arg] of command.argTypes) {\n const option = arg.decorators.find((x) => x.constructor === ApplicationCommandOption) as ApplicationCommandOption\n\n if (option) {\n cmd.options.push(option.options)\n }\n }\n }\n\n commands.push(cmd)\n }\n\n this.logger.info(`Processing ${chalk.green(commands.length)} commands(${commands.map((x) => chalk.blue(x.name)).join(', ')})`)\n\n if (this.config.guilds) {\n for (const guild of this.config.guilds) {\n try {\n const g = await this.client.guilds.fetch(guild)\n await g.fetch()\n this.logger.info(`Registering commands for guild ${chalk.green(g.name)}(${chalk.blue(g.id)})`)\n\n await g.commands.set(commands)\n\n this.logger.info(`Successfully registered commands for guild ${chalk.green(g.name)}(${chalk.blue(g.id)})`)\n } catch (e) {\n this.logger.error(`Failed to register commands to guild ${chalk.green(guild)}: ${(e as Error).message}`)\n }\n }\n } else {\n try {\n this.logger.info(`Registering commands globally...`)\n\n await this.client.application!.commands.set(commands)\n\n this.logger.info('Successfully registered commands.')\n } catch (e) {\n this.logger.error(`Failed to register commands to global: ${(e as Error).message}`)\n }\n }\n }\n\n @argConverter({\n component: ApplicationCommandComponent,\n parameterless: true,\n type: ChatInputCommandInteraction,\n })\n async chatInteraction(i: ChatInputCommandInteraction) {\n return i\n }\n\n @argConverter({\n component: ApplicationCommandComponent,\n parameterless: true,\n type: MessageContextMenuCommandInteraction,\n })\n async messageInteraction(i: MessageContextMenuCommandInteraction) {\n return i\n }\n\n @argConverter({\n component: ApplicationCommandComponent,\n parameterless: true,\n type: UserContextMenuCommandInteraction,\n })\n async userInteraction(i: UserContextMenuCommandInteraction) {\n return i\n }\n}\n","import { ApplicationCommandType, ChatInputApplicationCommandData, MessageApplicationCommandData, UserApplicationCommandData } from 'discord.js'\nimport { BaseComponent, createComponentDecorator } from '../core'\n\nexport class ApplicationCommandComponent extends BaseComponent<\n (UserApplicationCommandData | MessageApplicationCommandData | Omit<ChatInputApplicationCommandData, 'options'>) & { type: ApplicationCommandType }\n> {}\n\nexport const applicationCommand = createComponentDecorator(ApplicationCommandComponent)\n","import _ from 'lodash'\n\nexport class ComponentArgumentDecorator<Options = unknown> {\n options: Options\n\n constructor(options: Partial<Options>) {\n if (typeof options === 'object') {\n this.options = _.merge(this.defaultOptions(), options)\n } else {\n this.options = options\n }\n }\n\n defaultOptions(): Options {\n return {} as unknown as Options\n }\n}\n","import { APIApplicationCommandOption } from 'discord.js'\nimport { createArgumentDecorator } from '../core'\nimport { ComponentArgumentDecorator } from '../core/components/ComponentArgumentDecorator'\n\ntype Options = APIApplicationCommandOption\n\nexport class ApplicationCommandOption extends ComponentArgumentDecorator<Options> {}\n\nexport const option = createArgumentDecorator(ApplicationCommandOption)\n","import { BaseComponent, createComponentDecorator } from '../components'\n\ntype Options = { component: typeof BaseComponent<unknown>; type: Function; parameterless: boolean }\n\nexport class ConverterComponent extends BaseComponent<Options, Options & { parameterless?: boolean }> {}\n\nexport const argConverter = createComponentDecorator(ConverterComponent)\n","import chalk from 'chalk'\nimport { Extension } from './Extension'\n\nexport class CTSExtension extends Extension {\n protected get logger() {\n if (!this._logger) this._logger = this.commandClient.ctsLogger.getChildLogger({ prefix: [chalk.green(`[${this.constructor.name}]`)], displayFunctionName: false })\n return this._logger\n }\n}\n","import chalk from 'chalk'\nimport { Collection } from 'discord.js'\nimport { Logger } from 'tslog'\nimport { BaseComponent } from '../components'\nimport { ComponentArgument } from '../components/ComponentArgument'\nimport { ConverterComponent } from '../converter'\nimport { CommandClient } from '../structures'\n\nexport class Extension {\n protected get commandClient() {\n return CommandClient.getFromModule(this)\n }\n\n protected get client() {\n return this.commandClient.discord\n }\n\n protected _logger?: Logger\n\n protected get logger() {\n if (!this._logger) this._logger = this.commandClient.logger.getChildLogger({ prefix: [chalk.green(`[${this.constructor.name}]`)], displayFunctionName: false })\n return this._logger\n }\n\n protected async convertArguments(\n component: typeof BaseComponent<unknown>,\n argList: unknown[],\n args: Collection<number, ComponentArgument>,\n getConverterArgs: (arg: ComponentArgument, index: number) => unknown[] | Promise<unknown[]>,\n ) {\n const items = new Collection<unknown, { ext: object; component: ConverterComponent }>()\n\n for (const extension of this.commandClient.registry.extensions) {\n for (const converter of this.commandClient.registry.getComponentsWithType(extension, ConverterComponent)) {\n if (converter.options.component != component) continue\n\n items.set(converter.options.type, { component: converter, ext: extension })\n }\n }\n\n for (const [index, arg] of args) {\n const converter = items.get(arg.type)\n\n if (!converter) {\n argList[index] = undefined\n continue\n }\n\n const converterArgs = await getConverterArgs(arg, index)\n\n argList[index] = await converter.component.execute(converter.ext, converterArgs)\n }\n }\n}\n"],"mappings":"+/BAAA,mXCAA,OAAO,4BCAP,MAA2B,sBCEpB,WAAuB,CAG5B,YAAmB,EAAe,MAAf,KAAA,OAFnB,WAA2C,KADtC,yBDEA,WAAmB,CAKxB,SAAkD,GAAI,cAEtD,YAAY,EAAqB,EAAkB,EAAqB,CACtE,KAAK,QAAU,KAAK,eAAe,GAEnC,KAAK,OAAS,EACd,OAAS,GAAI,EAAG,EAAI,EAAS,OAAQ,IAAK,CACxC,GAAM,GAAU,EAAS,GACzB,KAAK,SAAS,IAAI,EAAG,GAAI,GAAkB,KAI/C,eAAe,EAA8B,CAC3C,MAAO,GAGT,QAAQ,EAAgB,EAAiB,CACvC,MAAO,MAAK,OAAO,KAAK,EAAM,GAAK,KAtBhC,qBEJP,MAA2B,sBCApB,GAAM,GAAuB,SACvB,GAA0B,SAC1B,EAAwB,SACxB,EAAsB,SDK5B,GAAM,GAAoB,EAAC,GAAmC,CACnE,GAAI,GAAgC,QAAQ,YAAY,EAAsB,GAE9E,MAAK,IACH,GAAS,GAAI,cAEb,QAAQ,eAAe,EAAsB,EAAQ,IAGhD,GATwB,qBAYpB,GAAe,GAAC,EAAgB,IAGpC,AAFO,EAAkB,GAEnB,IAAI,GAHS,gBAMf,EAA2B,EAAsB,GACrD,AAAC,GACC,CAAC,EAAQ,IAAQ,CACtB,GAAI,GAAgD,GAAI,GAAK,EAAS,QAAQ,IAAI,EAAQ,GAAM,QAAQ,YAAY,oBAAqB,EAAQ,IAEjJ,GAAM,GAAQ,EAAkB,GAIhC,AAFmB,EAA0B,EAAQ,GAE1C,QAAQ,CAAC,EAAG,IAAM,CAnCnC,MA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hC,qBAAkB,SAIvB,EAAa,CACZ,UAAW,EACX,cAAe,GACf,KAAM,6FAEiB,sCAAiC,YAAA,OAAjC,uCAtId,EAA2B,UAsIhC,kBAAe,MDtJhB,mBAA4B,WAAY,CAI7C,YAAmB,EAAwB,EAAiB,GAAI,WAAO,CAAE,iBAAkB,KAAK,iBAAiB,kBAAkB,WAAa,CAC9I,aADiB,QAAA,OAAwB,OAAA,EAGzC,KAAK,UAAY,EAAO,eAAe,CAAE,OAAQ,CAAC,WAAM,KAAK,iBAAkB,gBAAiB,SAAU,oBAAqB,KAE/H,KAAK,SAAW,GAAI,GAAS,KAAK,UAAW,MAE7C,KAAK,SAAS,qBAAqB,MAAO,MAC1C,KAAK,SAAS,qBAAqB,UAAW,KAAK,cAG/C,oCAAmC,EAA2C,CAClF,KAAM,MAAK,SAAS,eAAe,GAAI,GAA4B,IACnE,KAAK,UAAU,KAAK,0CAGtB,iCAAkC,CAChC,MAAO,MAAK,SAAS,WAAW,KAAK,AAAC,GAAM,EAAE,cAAgB,SAGzD,eAAc,EAA4B,CAC/C,MAAO,SAAQ,YAAY,EAAqB,KAzB7C","names":[]}
|
package/package.json
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pikokr/command.ts",
|
|
3
3
|
"description": "Discord.js command framework for typescript.",
|
|
4
|
-
"version": "5.0.0-dev.
|
|
4
|
+
"version": "5.0.0-dev.a935c89",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"devDependencies": {
|
|
9
9
|
"@discordjs/ts-docgen": "^0.4.1",
|
|
10
10
|
"@swc/core": "^1.2.218",
|
|
11
|
+
"@types/chalk": "^2.2.0",
|
|
11
12
|
"@types/lodash": "^4.14.172",
|
|
12
13
|
"all-contributors-cli": "^6.20.0",
|
|
13
14
|
"discord.js": "^14.0.1",
|
|
15
|
+
"dotenv": "^16.0.1",
|
|
14
16
|
"prettier": "^2.7.1",
|
|
15
17
|
"ts-node": "^10.9.1",
|
|
16
18
|
"tsup": "^5.11.12",
|
|
@@ -19,6 +21,7 @@
|
|
|
19
21
|
},
|
|
20
22
|
"dependencies": {
|
|
21
23
|
"@types/node": "^14.14.37",
|
|
24
|
+
"chalk": "4.1.2",
|
|
22
25
|
"discord-api-types": "^0.23.1",
|
|
23
26
|
"lodash": "^4.17.21",
|
|
24
27
|
"reflect-metadata": "^0.1.13",
|
|
@@ -34,7 +37,8 @@
|
|
|
34
37
|
"build": "tsup-node",
|
|
35
38
|
"docs:dev": "typedoc",
|
|
36
39
|
"docs:build": "typedoc",
|
|
37
|
-
"docs": "typedoc --json docs/typedoc-out.json --tsconfig tsconfig.prod.json src/index.ts && ts-node scripts/docs"
|
|
40
|
+
"docs": "typedoc --json docs/typedoc-out.json --tsconfig tsconfig.prod.json src/index.ts && ts-node scripts/docs",
|
|
41
|
+
"test": "yarn ts-node --swc test"
|
|
38
42
|
},
|
|
39
43
|
"peerDependencies": {
|
|
40
44
|
"discord.js": "^14.0.1"
|
|
@@ -1,21 +1,8 @@
|
|
|
1
|
-
import { ApplicationCommandType } from 'discord.js'
|
|
1
|
+
import { ApplicationCommandType, ChatInputApplicationCommandData, MessageApplicationCommandData, UserApplicationCommandData } from 'discord.js'
|
|
2
2
|
import { BaseComponent, createComponentDecorator } from '../core'
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
type: ApplicationCommandType
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
type ApplicationCommandRequiredOptions = {
|
|
9
|
-
name: string
|
|
10
|
-
description: string
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export class ApplicationCommandComponent extends BaseComponent<ApplicationCommandOptions, ApplicationCommandRequiredOptions> {
|
|
14
|
-
defaultOptions() {
|
|
15
|
-
return {
|
|
16
|
-
type: ApplicationCommandType.ChatInput,
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
4
|
+
export class ApplicationCommandComponent extends BaseComponent<
|
|
5
|
+
(UserApplicationCommandData | MessageApplicationCommandData | Omit<ChatInputApplicationCommandData, 'options'>) & { type: ApplicationCommandType }
|
|
6
|
+
> {}
|
|
20
7
|
|
|
21
8
|
export const applicationCommand = createComponentDecorator(ApplicationCommandComponent)
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import chalk from 'chalk'
|
|
2
|
+
import {
|
|
3
|
+
ApplicationCommandData,
|
|
4
|
+
ApplicationCommandType,
|
|
5
|
+
ChatInputCommandInteraction,
|
|
6
|
+
GuildAuditLogs,
|
|
7
|
+
Interaction,
|
|
8
|
+
InteractionType,
|
|
9
|
+
MessageContextMenuCommandInteraction,
|
|
10
|
+
Snowflake,
|
|
11
|
+
UserContextMenuCommandInteraction,
|
|
12
|
+
} from 'discord.js'
|
|
13
|
+
import { ApplicationCommandComponent } from '.'
|
|
14
|
+
import { ApplicationCommandOption } from './ApplicationCommandOption'
|
|
15
|
+
import { moduleHook } from '../core/hooks'
|
|
16
|
+
import { listener } from '../core/listener'
|
|
17
|
+
import { CommandClient } from '../core/structures'
|
|
18
|
+
import { argConverter } from '../core/converter'
|
|
19
|
+
import { CTSExtension } from '../core/extensions/CTSExtension'
|
|
20
|
+
|
|
21
|
+
export type ApplicationCommandExtensionConfig = {
|
|
22
|
+
guilds?: Snowflake[]
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class ApplicationCommandExtension extends CTSExtension {
|
|
26
|
+
constructor(public config: ApplicationCommandExtensionConfig) {
|
|
27
|
+
super()
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
@listener({ event: 'interactionCreate' })
|
|
31
|
+
async interactionCreate(i: Interaction) {
|
|
32
|
+
if (i.type !== InteractionType.ApplicationCommand) return
|
|
33
|
+
|
|
34
|
+
let cmd: ApplicationCommandComponent | null = null
|
|
35
|
+
let ext: object | null = null
|
|
36
|
+
|
|
37
|
+
const extensions = this.commandClient.registry.extensions
|
|
38
|
+
|
|
39
|
+
for (const extension of extensions) {
|
|
40
|
+
const components = this.commandClient.registry.getComponentsWithType(extension, ApplicationCommandComponent)
|
|
41
|
+
|
|
42
|
+
for (const command of components) {
|
|
43
|
+
if (command.options.name === i.commandName) {
|
|
44
|
+
ext = extension
|
|
45
|
+
cmd = command
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (cmd && ext) {
|
|
51
|
+
const argList: unknown[] = []
|
|
52
|
+
|
|
53
|
+
await this.convertArguments(ApplicationCommandComponent, argList, cmd.argTypes, () => [i])
|
|
54
|
+
|
|
55
|
+
for (const [idx, arg] of cmd.argTypes) {
|
|
56
|
+
let value: unknown = null
|
|
57
|
+
|
|
58
|
+
for (const decorator of arg.decorators) {
|
|
59
|
+
if (decorator instanceof ApplicationCommandOption) {
|
|
60
|
+
value = i.options.get(decorator.options.name, false)?.value
|
|
61
|
+
break
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (value) {
|
|
66
|
+
argList[idx] = value
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
await cmd.execute(ext, argList)
|
|
72
|
+
} catch (e) {
|
|
73
|
+
this.logger.error(e)
|
|
74
|
+
this.commandClient.emit('applicationCommandInvokeError', e, i)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
@moduleHook('load')
|
|
80
|
+
async load() {}
|
|
81
|
+
|
|
82
|
+
async sync() {
|
|
83
|
+
const client = CommandClient.getFromModule(this)
|
|
84
|
+
|
|
85
|
+
this.logger.info('Trying to sync commands...')
|
|
86
|
+
|
|
87
|
+
const commands: ApplicationCommandData[] = []
|
|
88
|
+
|
|
89
|
+
for (const command of client.registry.getComponentsWithTypeGlobal(ApplicationCommandComponent)) {
|
|
90
|
+
const cmd: ApplicationCommandData = { ...command.options }
|
|
91
|
+
|
|
92
|
+
if (cmd.type === ApplicationCommandType.ChatInput) {
|
|
93
|
+
cmd.options = []
|
|
94
|
+
|
|
95
|
+
for (const [, arg] of command.argTypes) {
|
|
96
|
+
const option = arg.decorators.find((x) => x.constructor === ApplicationCommandOption) as ApplicationCommandOption
|
|
97
|
+
|
|
98
|
+
if (option) {
|
|
99
|
+
cmd.options.push(option.options)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
commands.push(cmd)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
this.logger.info(`Processing ${chalk.green(commands.length)} commands(${commands.map((x) => chalk.blue(x.name)).join(', ')})`)
|
|
108
|
+
|
|
109
|
+
if (this.config.guilds) {
|
|
110
|
+
for (const guild of this.config.guilds) {
|
|
111
|
+
try {
|
|
112
|
+
const g = await this.client.guilds.fetch(guild)
|
|
113
|
+
await g.fetch()
|
|
114
|
+
this.logger.info(`Registering commands for guild ${chalk.green(g.name)}(${chalk.blue(g.id)})`)
|
|
115
|
+
|
|
116
|
+
await g.commands.set(commands)
|
|
117
|
+
|
|
118
|
+
this.logger.info(`Successfully registered commands for guild ${chalk.green(g.name)}(${chalk.blue(g.id)})`)
|
|
119
|
+
} catch (e) {
|
|
120
|
+
this.logger.error(`Failed to register commands to guild ${chalk.green(guild)}: ${(e as Error).message}`)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
} else {
|
|
124
|
+
try {
|
|
125
|
+
this.logger.info(`Registering commands globally...`)
|
|
126
|
+
|
|
127
|
+
await this.client.application!.commands.set(commands)
|
|
128
|
+
|
|
129
|
+
this.logger.info('Successfully registered commands.')
|
|
130
|
+
} catch (e) {
|
|
131
|
+
this.logger.error(`Failed to register commands to global: ${(e as Error).message}`)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
@argConverter({
|
|
137
|
+
component: ApplicationCommandComponent,
|
|
138
|
+
parameterless: true,
|
|
139
|
+
type: ChatInputCommandInteraction,
|
|
140
|
+
})
|
|
141
|
+
async chatInteraction(i: ChatInputCommandInteraction) {
|
|
142
|
+
return i
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
@argConverter({
|
|
146
|
+
component: ApplicationCommandComponent,
|
|
147
|
+
parameterless: true,
|
|
148
|
+
type: MessageContextMenuCommandInteraction,
|
|
149
|
+
})
|
|
150
|
+
async messageInteraction(i: MessageContextMenuCommandInteraction) {
|
|
151
|
+
return i
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
@argConverter({
|
|
155
|
+
component: ApplicationCommandComponent,
|
|
156
|
+
parameterless: true,
|
|
157
|
+
type: UserContextMenuCommandInteraction,
|
|
158
|
+
})
|
|
159
|
+
async userInteraction(i: UserContextMenuCommandInteraction) {
|
|
160
|
+
return i
|
|
161
|
+
}
|
|
162
|
+
}
|
|
@@ -2,21 +2,16 @@ import { Collection } from 'discord.js'
|
|
|
2
2
|
import _ from 'lodash'
|
|
3
3
|
import { ComponentArgument } from './ComponentArgument'
|
|
4
4
|
|
|
5
|
-
export class BaseComponent<Options = unknown,
|
|
6
|
-
options: Options
|
|
5
|
+
export class BaseComponent<Options = unknown, OptionsArg = Options> {
|
|
6
|
+
options: Options
|
|
7
7
|
|
|
8
8
|
method: Function
|
|
9
9
|
|
|
10
10
|
argTypes: Collection<number, ComponentArgument> = new Collection()
|
|
11
11
|
|
|
12
|
-
constructor(options:
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
} else if (typeof options === 'string') {
|
|
16
|
-
this.options = options as this['options']
|
|
17
|
-
} else {
|
|
18
|
-
this.options = null as unknown as this['options']
|
|
19
|
-
}
|
|
12
|
+
constructor(options: OptionsArg, method: Function, argTypes: unknown[]) {
|
|
13
|
+
this.options = this.convertOptions(options)
|
|
14
|
+
|
|
20
15
|
this.method = method
|
|
21
16
|
for (let i = 0; i < argTypes.length; i++) {
|
|
22
17
|
const element = argTypes[i]
|
|
@@ -24,11 +19,11 @@ export class BaseComponent<Options = unknown, RequiredOptions = unknown> {
|
|
|
24
19
|
}
|
|
25
20
|
}
|
|
26
21
|
|
|
27
|
-
|
|
28
|
-
return
|
|
22
|
+
convertOptions(options: OptionsArg): Options {
|
|
23
|
+
return options as unknown as Options
|
|
29
24
|
}
|
|
30
25
|
|
|
31
26
|
execute(target: object, args: unknown[]) {
|
|
32
|
-
return this.method.
|
|
27
|
+
return this.method.call(target, ...args)
|
|
33
28
|
}
|
|
34
29
|
}
|
|
@@ -24,10 +24,10 @@ export const getComponent = (target: object, key: string | symbol) => {
|
|
|
24
24
|
return store.get(key)
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
export const createComponentDecorator = <Options,
|
|
28
|
-
return (options:
|
|
27
|
+
export const createComponentDecorator = <Options, OptionArgs>(type: typeof BaseComponent<Options, OptionArgs>) => {
|
|
28
|
+
return (options: OptionArgs): MethodDecorator => {
|
|
29
29
|
return (target, key) => {
|
|
30
|
-
var component: BaseComponent<Options> = new type(options, Reflect.get(target, key), Reflect.getMetadata('design:paramtypes', target, key))
|
|
30
|
+
var component: BaseComponent<Options, OptionArgs> = new type(options, Reflect.get(target, key), Reflect.getMetadata('design:paramtypes', target, key))
|
|
31
31
|
|
|
32
32
|
const store = getComponentStore(target)
|
|
33
33
|
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { BaseComponent, createComponentDecorator } from '../components'
|
|
2
|
+
|
|
3
|
+
type Options = { component: typeof BaseComponent<unknown>; type: Function; parameterless: boolean }
|
|
4
|
+
|
|
5
|
+
export class ConverterComponent extends BaseComponent<Options, Options & { parameterless?: boolean }> {}
|
|
6
|
+
|
|
7
|
+
export const argConverter = createComponentDecorator(ConverterComponent)
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import chalk from 'chalk'
|
|
2
|
+
import { Extension } from './Extension'
|
|
3
|
+
|
|
4
|
+
export class CTSExtension extends Extension {
|
|
5
|
+
protected get logger() {
|
|
6
|
+
if (!this._logger) this._logger = this.commandClient.ctsLogger.getChildLogger({ prefix: [chalk.green(`[${this.constructor.name}]`)], displayFunctionName: false })
|
|
7
|
+
return this._logger
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import chalk from 'chalk'
|
|
2
|
+
import { Collection } from 'discord.js'
|
|
3
|
+
import { Logger } from 'tslog'
|
|
4
|
+
import { BaseComponent } from '../components'
|
|
5
|
+
import { ComponentArgument } from '../components/ComponentArgument'
|
|
6
|
+
import { ConverterComponent } from '../converter'
|
|
7
|
+
import { CommandClient } from '../structures'
|
|
8
|
+
|
|
9
|
+
export class Extension {
|
|
10
|
+
protected get commandClient() {
|
|
11
|
+
return CommandClient.getFromModule(this)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
protected get client() {
|
|
15
|
+
return this.commandClient.discord
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
protected _logger?: Logger
|
|
19
|
+
|
|
20
|
+
protected get logger() {
|
|
21
|
+
if (!this._logger) this._logger = this.commandClient.logger.getChildLogger({ prefix: [chalk.green(`[${this.constructor.name}]`)], displayFunctionName: false })
|
|
22
|
+
return this._logger
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
protected async convertArguments(
|
|
26
|
+
component: typeof BaseComponent<unknown>,
|
|
27
|
+
argList: unknown[],
|
|
28
|
+
args: Collection<number, ComponentArgument>,
|
|
29
|
+
getConverterArgs: (arg: ComponentArgument, index: number) => unknown[] | Promise<unknown[]>,
|
|
30
|
+
) {
|
|
31
|
+
const items = new Collection<unknown, { ext: object; component: ConverterComponent }>()
|
|
32
|
+
|
|
33
|
+
for (const extension of this.commandClient.registry.extensions) {
|
|
34
|
+
for (const converter of this.commandClient.registry.getComponentsWithType(extension, ConverterComponent)) {
|
|
35
|
+
if (converter.options.component != component) continue
|
|
36
|
+
|
|
37
|
+
items.set(converter.options.type, { component: converter, ext: extension })
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
for (const [index, arg] of args) {
|
|
42
|
+
const converter = items.get(arg.type)
|
|
43
|
+
|
|
44
|
+
if (!converter) {
|
|
45
|
+
argList[index] = undefined
|
|
46
|
+
continue
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const converterArgs = await getConverterArgs(arg, index)
|
|
50
|
+
|
|
51
|
+
argList[index] = await converter.component.execute(converter.ext, converterArgs)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Extension'
|
package/src/core/index.ts
CHANGED
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
import { BaseComponent, createComponentDecorator } from '../components'
|
|
2
2
|
|
|
3
|
-
export class ListenerComponent extends BaseComponent<{ emitter: string }, { event: string }> {
|
|
3
|
+
export class ListenerComponent extends BaseComponent<{ emitter: string; event: string }, { emitter?: string; event: string }> {
|
|
4
4
|
defaultOptions() {
|
|
5
5
|
return { emitter: 'discord' }
|
|
6
6
|
}
|
|
7
|
+
|
|
8
|
+
constructor(options: ListenerComponent['options'], method: Function, argTypes: unknown[]) {
|
|
9
|
+
super(
|
|
10
|
+
{
|
|
11
|
+
event: options.event,
|
|
12
|
+
emitter: options.emitter ?? 'discord',
|
|
13
|
+
},
|
|
14
|
+
method,
|
|
15
|
+
argTypes,
|
|
16
|
+
)
|
|
17
|
+
}
|
|
7
18
|
}
|
|
8
19
|
|
|
9
20
|
export const listener = createComponentDecorator(ListenerComponent)
|
|
@@ -1,14 +1,36 @@
|
|
|
1
|
+
import chalk from 'chalk'
|
|
1
2
|
import { Client } from 'discord.js'
|
|
2
3
|
import EventEmitter from 'events'
|
|
4
|
+
import { Logger } from 'tslog'
|
|
5
|
+
import { ApplicationCommandExtension, ApplicationCommandExtensionConfig } from '../../applicationCommand/ApplicationCommandExtension'
|
|
6
|
+
import { CommandClientSymbol } from '../symbols'
|
|
3
7
|
import { Registry } from './Registry'
|
|
4
8
|
|
|
5
9
|
export class CommandClient extends EventEmitter {
|
|
6
|
-
|
|
10
|
+
ctsLogger: Logger
|
|
11
|
+
registry: Registry
|
|
7
12
|
|
|
8
|
-
constructor(public discord: Client) {
|
|
13
|
+
constructor(public discord: Client, public logger: Logger = new Logger({ dateTimeTimezone: Intl.DateTimeFormat().resolvedOptions().timeZone })) {
|
|
9
14
|
super()
|
|
10
15
|
|
|
16
|
+
this.ctsLogger = logger.getChildLogger({ prefix: [chalk.blue('[command.ts]')], displayFilePath: 'hidden', displayFunctionName: false })
|
|
17
|
+
|
|
18
|
+
this.registry = new Registry(this.ctsLogger, this)
|
|
19
|
+
|
|
11
20
|
this.registry.registerEventEmitter('cts', this)
|
|
12
21
|
this.registry.registerEventEmitter('discord', this.discord)
|
|
13
22
|
}
|
|
23
|
+
|
|
24
|
+
async enableApplicationCommandsExtension(config: ApplicationCommandExtensionConfig) {
|
|
25
|
+
await this.registry.registerModule(new ApplicationCommandExtension(config))
|
|
26
|
+
this.ctsLogger.info('Application command extension enabled.')
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
getApplicationCommandsExtension() {
|
|
30
|
+
return this.registry.extensions.find((x) => x.constructor === ApplicationCommandExtension) as ApplicationCommandExtension | undefined
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static getFromModule(ext: object): CommandClient {
|
|
34
|
+
return Reflect.getMetadata(CommandClientSymbol, ext)
|
|
35
|
+
}
|
|
14
36
|
}
|
|
@@ -1,17 +1,28 @@
|
|
|
1
|
+
import chalk from 'chalk'
|
|
1
2
|
import { Collection } from 'discord.js'
|
|
2
3
|
import EventEmitter from 'events'
|
|
3
4
|
import _ from 'lodash'
|
|
5
|
+
import { Logger } from 'tslog'
|
|
4
6
|
import { BaseComponent, getComponentStore } from '../components'
|
|
5
7
|
import { getModuleHookStore } from '../hooks'
|
|
6
8
|
import { ListenerComponent } from '../listener'
|
|
7
|
-
import {
|
|
9
|
+
import { CommandClientSymbol } from '../symbols'
|
|
10
|
+
import { CommandClient } from './CommandClient'
|
|
8
11
|
|
|
9
12
|
export class Registry {
|
|
10
13
|
extensions: object[] = []
|
|
11
14
|
|
|
12
15
|
emitters: Collection<string, EventEmitter> = new Collection()
|
|
13
16
|
|
|
14
|
-
|
|
17
|
+
logger: Logger
|
|
18
|
+
|
|
19
|
+
constructor(logger: Logger, public client: CommandClient) {
|
|
20
|
+
this.logger = logger.getChildLogger({
|
|
21
|
+
prefix: [chalk.green('[Registry]')],
|
|
22
|
+
})
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
getComponentsWithTypeGlobal<T extends typeof BaseComponent<Config>, Config>(type: T): InstanceType<T>[] {
|
|
15
26
|
const result: InstanceType<T>[] = []
|
|
16
27
|
|
|
17
28
|
for (const ext of this.extensions) {
|
|
@@ -21,7 +32,7 @@ export class Registry {
|
|
|
21
32
|
return result
|
|
22
33
|
}
|
|
23
34
|
|
|
24
|
-
getComponentsWithType<T extends typeof BaseComponent<Config
|
|
35
|
+
getComponentsWithType<T extends typeof BaseComponent<Config>, Config>(ext: object, type: T): InstanceType<T>[] {
|
|
25
36
|
const componentStore = getComponentStore(ext)
|
|
26
37
|
|
|
27
38
|
return Array.from(componentStore.filter((x) => (x.constructor as unknown) === type).values() as Iterable<InstanceType<T>>)
|
|
@@ -57,15 +68,19 @@ export class Registry {
|
|
|
57
68
|
}
|
|
58
69
|
|
|
59
70
|
async registerModule(ext: object) {
|
|
71
|
+
Reflect.defineMetadata(CommandClientSymbol, this.client, ext)
|
|
72
|
+
|
|
60
73
|
this.registerEventListeners(ext)
|
|
61
74
|
await this.runModuleHook(ext, 'load')
|
|
62
75
|
this.extensions.push(ext)
|
|
76
|
+
this.logger.info(`Module registered: ${chalk.green(ext.constructor.name)}`)
|
|
63
77
|
}
|
|
64
78
|
|
|
65
79
|
async unregisterModule(ext: object) {
|
|
66
80
|
this.unregisterEventListeners(ext)
|
|
67
81
|
await this.runModuleHook(ext, 'unload')
|
|
68
82
|
_.remove(this.extensions, (x) => x === ext)
|
|
83
|
+
this.logger.info(`Module unregistered: ${chalk.green(ext.constructor.name)}`)
|
|
69
84
|
}
|
|
70
85
|
|
|
71
86
|
runModuleHook(ext: object, hookName: string, ...args: unknown[]) {
|
|
@@ -75,7 +90,7 @@ export class Registry {
|
|
|
75
90
|
|
|
76
91
|
if (functions) {
|
|
77
92
|
for (const fn of functions) {
|
|
78
|
-
fn.
|
|
93
|
+
fn.call(ext, ...args)
|
|
79
94
|
}
|
|
80
95
|
}
|
|
81
96
|
}
|
package/src/core/symbols.ts
CHANGED
package/test/index.ts
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import { ApplicationCommandOptionType, Client } from 'discord.js'
|
|
2
|
-
import { applicationCommand,
|
|
1
|
+
import { ApplicationCommandOptionType, ApplicationCommandType, ChatInputCommandInteraction, Client } from 'discord.js'
|
|
2
|
+
import { applicationCommand, CommandClient, moduleHook, option, Registry } from '../src'
|
|
3
3
|
import { listener } from '../src/core/listener'
|
|
4
|
+
import 'dotenv/config'
|
|
5
|
+
import { Logger } from 'tslog'
|
|
6
|
+
import chalk from 'chalk'
|
|
7
|
+
import { Extension } from '../src/core/extensions'
|
|
4
8
|
|
|
5
|
-
class Test {
|
|
9
|
+
class Test extends Extension {
|
|
6
10
|
@applicationCommand({
|
|
11
|
+
type: ApplicationCommandType.ChatInput,
|
|
7
12
|
name: 'test',
|
|
8
13
|
description: 'wow this is test',
|
|
9
14
|
})
|
|
10
|
-
async testCommand(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
description: '와아',
|
|
14
|
-
type: ApplicationCommandOptionType.String,
|
|
15
|
-
})
|
|
16
|
-
hello: string,
|
|
17
|
-
world: string,
|
|
18
|
-
) {}
|
|
15
|
+
async testCommand(i: ChatInputCommandInteraction) {
|
|
16
|
+
i.reply('Wow')
|
|
17
|
+
}
|
|
19
18
|
|
|
20
19
|
@applicationCommand({
|
|
20
|
+
type: ApplicationCommandType.ChatInput,
|
|
21
21
|
name: 'test2',
|
|
22
22
|
description: 'wow this is test wow',
|
|
23
23
|
})
|
|
@@ -26,23 +26,27 @@ class Test {
|
|
|
26
26
|
name: 'sans',
|
|
27
27
|
description: '와',
|
|
28
28
|
type: ApplicationCommandOptionType.String,
|
|
29
|
+
required: true,
|
|
29
30
|
})
|
|
30
31
|
wa: string,
|
|
31
|
-
|
|
32
|
+
i: ChatInputCommandInteraction,
|
|
33
|
+
) {
|
|
34
|
+
i.reply(wa)
|
|
35
|
+
}
|
|
32
36
|
|
|
33
37
|
@moduleHook('load')
|
|
34
38
|
load() {
|
|
35
|
-
|
|
39
|
+
this.logger.info('Load')
|
|
36
40
|
}
|
|
37
41
|
|
|
38
42
|
@moduleHook('unload')
|
|
39
43
|
unload() {
|
|
40
|
-
|
|
44
|
+
this.logger.info('Unload')
|
|
41
45
|
}
|
|
42
46
|
|
|
43
|
-
@listener({ event: '
|
|
47
|
+
@listener({ event: 'ready' })
|
|
44
48
|
testEvent() {
|
|
45
|
-
|
|
49
|
+
this.logger.info(`Login: ${chalk.green(client.user!.tag)}`)
|
|
46
50
|
}
|
|
47
51
|
}
|
|
48
52
|
|
|
@@ -50,20 +54,22 @@ const ext = new Test()
|
|
|
50
54
|
|
|
51
55
|
const client = new Client({ intents: [] })
|
|
52
56
|
|
|
53
|
-
const
|
|
57
|
+
const logger = new Logger({ dateTimeTimezone: Intl.DateTimeFormat().resolvedOptions().timeZone })
|
|
58
|
+
|
|
59
|
+
const cc = new CommandClient(client, logger)
|
|
54
60
|
|
|
55
61
|
const registry = cc.registry
|
|
56
62
|
|
|
57
63
|
const run = async () => {
|
|
58
|
-
await
|
|
64
|
+
await cc.enableApplicationCommandsExtension({
|
|
65
|
+
guilds: ['832938554438844438'],
|
|
66
|
+
})
|
|
59
67
|
|
|
60
|
-
|
|
61
|
-
client.emit('test')
|
|
68
|
+
await registry.registerModule(ext)
|
|
62
69
|
|
|
63
|
-
await
|
|
70
|
+
await client.login(process.env.TOKEN)
|
|
64
71
|
|
|
65
|
-
|
|
66
|
-
client.emit('test')
|
|
72
|
+
await cc.getApplicationCommandsExtension()!.sync()
|
|
67
73
|
}
|
|
68
74
|
|
|
69
75
|
run()
|