@pikokr/command.ts 5.0.0-dev.9796a99 → 5.0.0-dev.a0bc517
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/.github/workflows/codeql-analysis.yml +29 -29
- package/.github/workflows/docs.yml +1 -1
- package/.vscode/settings.json +9 -9
- package/README.md +2 -0
- package/dist/index.d.ts +47 -12
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/docs/index.yml +1 -1
- package/package.json +6 -6
- package/publish-version.js +10 -10
- package/scripts/docs.ts +8 -8
- package/src/applicationCommand/ApplicationCommand.ts +9 -5
- package/src/applicationCommand/index.ts +1 -1
- package/src/core/components/BaseComponent.ts +8 -6
- package/src/core/components/ComponentArgumentDecorator.ts +1 -1
- package/src/core/components/decoratorCreator.ts +8 -9
- package/src/core/hooks/index.ts +1 -0
- package/src/core/hooks/moduleHook.ts +31 -0
- package/src/core/index.ts +2 -0
- package/src/core/listener/index.ts +9 -0
- package/src/core/structures/CommandClient.ts +14 -0
- package/src/core/structures/Registry.ts +86 -0
- package/src/core/structures/index.ts +2 -0
- package/src/core/symbols.ts +2 -0
- package/src/index.ts +10 -10
- package/test/index.ts +36 -4
- package/tsconfig.json +2 -2
- package/tsconfig.prod.json +5 -2
- package/tsup.config.ts +9 -8
|
@@ -9,14 +9,14 @@
|
|
|
9
9
|
# the `language` matrix defined below to confirm you have the correct set of
|
|
10
10
|
# supported CodeQL languages.
|
|
11
11
|
#
|
|
12
|
-
name:
|
|
12
|
+
name: 'CodeQL'
|
|
13
13
|
|
|
14
14
|
on:
|
|
15
15
|
push:
|
|
16
|
-
branches: [
|
|
16
|
+
branches: [dev]
|
|
17
17
|
pull_request:
|
|
18
18
|
# The branches below must be a subset of the branches above
|
|
19
|
-
branches: [
|
|
19
|
+
branches: [dev]
|
|
20
20
|
schedule:
|
|
21
21
|
- cron: '31 4 * * 2'
|
|
22
22
|
|
|
@@ -32,39 +32,39 @@ jobs:
|
|
|
32
32
|
strategy:
|
|
33
33
|
fail-fast: false
|
|
34
34
|
matrix:
|
|
35
|
-
language: [
|
|
35
|
+
language: ['javascript']
|
|
36
36
|
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
|
|
37
37
|
# Learn more about CodeQL language support at https://git.io/codeql-language-support
|
|
38
38
|
|
|
39
39
|
steps:
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
- name: Checkout repository
|
|
41
|
+
uses: actions/checkout@v2
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
43
|
+
# Initializes the CodeQL tools for scanning.
|
|
44
|
+
- name: Initialize CodeQL
|
|
45
|
+
uses: github/codeql-action/init@v1
|
|
46
|
+
with:
|
|
47
|
+
languages: ${{ matrix.language }}
|
|
48
|
+
# If you wish to specify custom queries, you can do so here or in a config file.
|
|
49
|
+
# By default, queries listed here will override any specified in a config file.
|
|
50
|
+
# Prefix the list here with "+" to use these queries and those in the config file.
|
|
51
|
+
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
|
52
52
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
|
54
|
+
# If this step fails, then you should remove it and run the build manually (see below)
|
|
55
|
+
- name: Autobuild
|
|
56
|
+
uses: github/codeql-action/autobuild@v1
|
|
57
57
|
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
# ℹ️ Command-line programs to run using the OS shell.
|
|
59
|
+
# 📚 https://git.io/JvXDl
|
|
60
60
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
|
62
|
+
# and modify them (or add more) to build your code if your project
|
|
63
|
+
# uses a compiled language
|
|
64
64
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
65
|
+
#- run: |
|
|
66
|
+
# make bootstrap
|
|
67
|
+
# make release
|
|
68
68
|
|
|
69
|
-
|
|
70
|
-
|
|
69
|
+
- name: Perform CodeQL Analysis
|
|
70
|
+
uses: github/codeql-action/analyze@v1
|
package/.vscode/settings.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
2
|
+
"license-header-manager.additionalCommentStyles": [
|
|
3
|
+
{
|
|
4
|
+
"extension": ".ts",
|
|
5
|
+
"commentStart": "/*",
|
|
6
|
+
"commentEnd": "*/",
|
|
7
|
+
"commentMiddle": "*"
|
|
8
|
+
}
|
|
9
|
+
]
|
|
10
|
+
}
|
package/README.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://github.com/pikokr/command.ts/actions/workflows/publish.yml) <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
|
4
4
|
[](#contributors-)
|
|
5
|
+
|
|
5
6
|
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
|
6
7
|
|
|
7
8
|

|
|
@@ -11,6 +12,7 @@ Command framework for discord.js
|
|
|
11
12
|
[Discord](https://discord.gg/EEhcPzsGHV) / [문서](https://v3.cts.pikokr.dev) / [V2 문서](https://command-ts-docs-ezojnktwv-pikokr.vercel.app/)
|
|
12
13
|
|
|
13
14
|
## Contributors
|
|
15
|
+
|
|
14
16
|
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
|
|
15
17
|
<!-- prettier-ignore-start -->
|
|
16
18
|
<!-- markdownlint-disable -->
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { Collection, APIApplicationCommandOption } from 'discord.js';
|
|
1
|
+
import { Collection, Client, ApplicationCommandType, APIApplicationCommandOption } from 'discord.js';
|
|
2
|
+
import EventEmitter from 'events';
|
|
2
3
|
|
|
3
4
|
declare class ComponentArgumentDecorator<Options = unknown> {
|
|
4
5
|
options: Options;
|
|
@@ -12,32 +13,66 @@ declare class ComponentArgument {
|
|
|
12
13
|
constructor(type: unknown);
|
|
13
14
|
}
|
|
14
15
|
|
|
15
|
-
declare class BaseComponent<Options = unknown> {
|
|
16
|
-
options: Options;
|
|
16
|
+
declare class BaseComponent<Options = unknown, RequiredOptions = unknown> {
|
|
17
|
+
options: Options & RequiredOptions;
|
|
17
18
|
method: Function;
|
|
18
19
|
argTypes: Collection<number, ComponentArgument>;
|
|
19
|
-
constructor(options: Partial<Options
|
|
20
|
-
defaultOptions(): Options
|
|
20
|
+
constructor(options: Partial<Options> & RequiredOptions, method: Function, argTypes: unknown[]);
|
|
21
|
+
defaultOptions(): Options & Partial<RequiredOptions>;
|
|
21
22
|
execute(target: object, args: unknown[]): any;
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
declare type ComponentStore = Collection<string | symbol, BaseComponent>;
|
|
25
26
|
declare type ComponentArgumentStore = Collection<number, ComponentArgumentDecorator>;
|
|
26
27
|
declare const getComponentStore: (target: object) => ComponentStore;
|
|
27
|
-
declare const getComponent: (target: object, key: string | symbol) => BaseComponent<unknown> | undefined;
|
|
28
|
-
declare const createComponentDecorator: <Options>(type: {
|
|
29
|
-
new (options: Partial<Options
|
|
30
|
-
}) => (options: Options) => MethodDecorator;
|
|
28
|
+
declare const getComponent: (target: object, key: string | symbol) => BaseComponent<unknown, unknown> | undefined;
|
|
29
|
+
declare const createComponentDecorator: <Options, RequiredOptions>(type: {
|
|
30
|
+
new (options: Partial<Options> & RequiredOptions, method: Function, argTypes: unknown[]): BaseComponent<Options, RequiredOptions>;
|
|
31
|
+
}) => (options: Partial<Options> & RequiredOptions) => MethodDecorator;
|
|
31
32
|
declare const getComponentArgumentStore: (target: object, key: string | symbol) => ComponentArgumentStore;
|
|
32
33
|
declare const createArgumentDecorator: <Options>(type: {
|
|
33
34
|
new (options: Partial<Options>): ComponentArgumentDecorator<Options>;
|
|
34
35
|
}) => (options: Options) => ParameterDecorator;
|
|
35
36
|
|
|
36
|
-
declare
|
|
37
|
+
declare class Registry {
|
|
38
|
+
extensions: object[];
|
|
39
|
+
emitters: Collection<string, EventEmitter>;
|
|
40
|
+
getComponentsWithTypeGlobal<T extends typeof BaseComponent<Config, RequiredConfig>, Config, RequiredConfig>(type: T): InstanceType<T>[];
|
|
41
|
+
getComponentsWithType<T extends typeof BaseComponent<Config, RequiredConfig>, Config, RequiredConfig>(ext: object, type: T): InstanceType<T>[];
|
|
42
|
+
registerEventListeners(ext: object): void;
|
|
43
|
+
unregisterEventListeners(ext: object): void;
|
|
44
|
+
registerModule(ext: object): Promise<void>;
|
|
45
|
+
unregisterModule(ext: object): Promise<void>;
|
|
46
|
+
runModuleHook(ext: object, hookName: string, ...args: unknown[]): void;
|
|
47
|
+
registerEventEmitter(name: string, emitter: EventEmitter): void;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
declare class CommandClient extends EventEmitter {
|
|
51
|
+
discord: Client;
|
|
52
|
+
registry: Registry;
|
|
53
|
+
constructor(discord: Client);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
declare type ModuleHookStore = Collection<string | symbol, Function[]>;
|
|
57
|
+
declare const getModuleHookStore: (target: object) => ModuleHookStore;
|
|
58
|
+
declare const moduleHook: (name: string) => MethodDecorator;
|
|
59
|
+
|
|
60
|
+
declare type ApplicationCommandOptions = {
|
|
61
|
+
type: ApplicationCommandType;
|
|
62
|
+
};
|
|
63
|
+
declare type ApplicationCommandRequiredOptions = {
|
|
37
64
|
name: string;
|
|
38
65
|
description: string;
|
|
39
|
-
}
|
|
66
|
+
};
|
|
67
|
+
declare class ApplicationCommandComponent extends BaseComponent<ApplicationCommandOptions, ApplicationCommandRequiredOptions> {
|
|
68
|
+
defaultOptions(): {
|
|
69
|
+
type: ApplicationCommandType;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
declare const applicationCommand: (options: Partial<{
|
|
73
|
+
type: ApplicationCommandType;
|
|
74
|
+
}> & Partial<ApplicationCommandOptions> & ApplicationCommandRequiredOptions) => MethodDecorator;
|
|
40
75
|
|
|
41
76
|
declare const option: (options: APIApplicationCommandOption) => ParameterDecorator;
|
|
42
77
|
|
|
43
|
-
export { BaseComponent, applicationCommand, createArgumentDecorator, createComponentDecorator, getComponent, getComponentArgumentStore, getComponentStore, option };
|
|
78
|
+
export { ApplicationCommandComponent, BaseComponent, CommandClient, Registry, applicationCommand, createArgumentDecorator, createComponentDecorator, getComponent, getComponentArgumentStore, getComponentStore, getModuleHookStore, moduleHook, option };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
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});
|
|
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/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":["/*\r\n * File: index.ts\r\n *\r\n * Copyright (c) 2022-2022 pikokr\r\n *\r\n * Licensed under MIT License. Please see more defails in LICENSE file.\r\n */\r\n\r\nexport * from './core'\r\nexport * from './applicationCommand'\r\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> {\n options: Options\n\n method: Function\n\n argTypes: Collection<number, ComponentArgument> = new Collection()\n\n constructor(options: Partial<Options>, method: Function, argTypes: unknown[]) {\n if (typeof options === 'object') {\n this.options = _.merge(this.defaultOptions(), options)\n } else {\n this.options = 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 {\n return ({} as unknown) as Options\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>(type: typeof BaseComponent<Options>) => {\n return (options: Options): 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\n","export const ComponentStoreSymbol = Symbol()\nexport const ComponentArgStoreSymbol = Symbol()\n","import { BaseComponent, createComponentDecorator } from '../core'\n\nexport type ApplicationCommandOptions = {\n name: string\n description: string\n}\n\nexport class SlashCommand extends BaseComponent<ApplicationCommandOptions> {\n defaultOptions() {\n return {\n name: '',\n description: '',\n }\n }\n}\n\nexport const applicationCommand = createComponentDecorator(SlashCommand)\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,kNCAA,MAAO,4BCAP,MAA2B,sBAC3B,EAAc,qBCCP,WAAuB,CAG5B,YAAmB,EAAe,MAAf,KAAA,OAFnB,WAA2C,KADtC,yBDEA,WAAmB,CAKxB,SAAkD,GAAI,cAEtD,YAAY,EAA2B,EAAkB,EAAqB,CAC5E,AAAI,MAAO,IAAY,SACrB,KAAK,QAAU,UAAE,MAAM,KAAK,iBAAkB,GAE9C,KAAK,QAAU,EAEjB,KAAK,OAAS,EACd,OAAS,GAAI,EAAG,EAAI,EAAS,OAAQ,IAAK,CACxC,GAAM,GAAU,EAAS,GACzB,KAAK,SAAS,IAAI,EAAG,GAAI,GAAkB,KAI/C,gBAA0B,CACxB,MAAQ,GAGV,QAAQ,EAAgB,EAAiB,CACvC,MAAO,MAAK,OAAO,MAAM,EAAQ,KAzB9B,qBEJP,MAA2B,sBCApB,GAAM,GAAuB,SACvB,EAA0B,SDOhC,GAAM,GAAoB,EAAC,GAAmC,CACnE,GAAI,GAA8B,QAAQ,YAAY,EAAsB,GAE5E,MAAK,IACH,GAAS,GAAI,cAEb,QAAQ,eAAe,EAAsB,EAAQ,IAGhD,GATwB,qBAYpB,EAAe,GAAC,EAAgB,IAGpC,AAFO,EAAkB,GAEnB,IAAI,GAHS,gBAMf,EAA2B,EAAU,GACzC,AAAC,GACC,CAAC,EAAQ,IAAQ,CACtB,GAAI,GAAoC,GAAI,GAAK,EAAS,QAAQ,IAAI,EAAQ,GAAM,QAAQ,YAAY,oBAAqB,EAAQ,IAErI,GAAM,GAAQ,EAAkB,GAIhC,AAFmB,EAA0B,EAAQ,GAE1C,QAAQ,CAAC,EAAG,IAAK,CAnClC,MAoCQ,KAAU,SAAS,IAAI,KAAvB,QAA2B,WAAW,KAAK,KAG7C,EAAM,IAAI,EAAK,IAbmB,4BAkB3B,EAA4B,GAAC,EAAgB,IAA+C,CACvG,GAAI,GAAsC,QAAQ,YAAY,EAAsB,EAAQ,GAE5F,MAAK,IACH,GAAS,GAAI,cAEb,QAAQ,eAAe,EAAsB,EAAQ,EAAQ,IAGxD,GATgC,6BAY5B,EAA0B,EAAU,GACxC,AAAC,GACC,CAAC,EAAQ,EAAK,IAAQ,CAC3B,GAAI,GAA2C,GAAI,GAAK,GAIxD,AAFc,EAA0B,EAAQ,GAE1C,IAAI,EAAK,IAPkB,2BEjDhC,mBAA2B,EAAa,CAC7C,gBAAiB,CACf,MAAO,CACL,KAAM,GACN,YAAa,MAJZ,oBASA,GAAM,GAAqB,EAAyB,GChB3D,MAAc,qBAEP,OAAgC,CAGrC,YAAY,EAA2B,CACrC,AAAI,MAAO,IAAY,SACrB,KAAK,QAAU,UAAE,MAAM,KAAK,iBAAkB,GAE9C,KAAK,QAAU,EAInB,gBAA0B,CACxB,MAAQ,KAZL,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/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\n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BAAuC,EAA0B,GAAjE,gCAEA,GAAM,GAAS,EAAwB","names":[]}
|
package/docs/index.yml
CHANGED
package/package.json
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
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.a0bc517",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"devDependencies": {
|
|
9
|
-
"@discordjs/ts-docgen": "^0.
|
|
9
|
+
"@discordjs/ts-docgen": "^0.4.1",
|
|
10
10
|
"@swc/core": "^1.2.218",
|
|
11
11
|
"@types/lodash": "^4.14.172",
|
|
12
12
|
"all-contributors-cli": "^6.20.0",
|
|
13
13
|
"discord.js": "^14.0.1",
|
|
14
|
-
"prettier": "^2.
|
|
14
|
+
"prettier": "^2.7.1",
|
|
15
15
|
"ts-node": "^10.9.1",
|
|
16
16
|
"tsup": "^5.11.12",
|
|
17
|
-
"typedoc": "^0.22.11"
|
|
17
|
+
"typedoc": "^0.22.11",
|
|
18
|
+
"typescript": "^4.7.4"
|
|
18
19
|
},
|
|
19
20
|
"dependencies": {
|
|
20
21
|
"@types/node": "^14.14.37",
|
|
@@ -22,7 +23,6 @@
|
|
|
22
23
|
"lodash": "^4.17.21",
|
|
23
24
|
"reflect-metadata": "^0.1.13",
|
|
24
25
|
"tslog": "^3.3.1",
|
|
25
|
-
"typescript": "^4.7.4",
|
|
26
26
|
"walk-sync": "^3.0.0"
|
|
27
27
|
},
|
|
28
28
|
"repository": {
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"build": "tsup-node",
|
|
35
35
|
"docs:dev": "typedoc",
|
|
36
36
|
"docs:build": "typedoc",
|
|
37
|
-
"docs": "typedoc --json docs/typedoc-out.json --tsconfig tsconfig.prod.json src/index.ts && ts-node
|
|
37
|
+
"docs": "typedoc --json docs/typedoc-out.json --tsconfig tsconfig.prod.json src/index.ts && ts-node scripts/docs"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
40
|
"discord.js": "^14.0.1"
|
package/publish-version.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* This is the default license template.
|
|
3
|
-
*
|
|
4
|
-
* File: publish-version.js
|
|
5
|
-
* Author: pikokr
|
|
6
|
-
* Copyright (c) 2022 pikokr
|
|
7
|
-
*
|
|
8
|
-
* To edit this license information: Press Ctrl+Shift+P and press 'Create new License Template...'.
|
|
9
|
-
*/
|
|
10
|
-
|
|
1
|
+
/*
|
|
2
|
+
* This is the default license template.
|
|
3
|
+
*
|
|
4
|
+
* File: publish-version.js
|
|
5
|
+
* Author: pikokr
|
|
6
|
+
* Copyright (c) 2022 pikokr
|
|
7
|
+
*
|
|
8
|
+
* To edit this license information: Press Ctrl+Shift+P and press 'Create new License Template...'.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
11
|
/*
|
|
12
12
|
* Copyright (c) 2022 pikokr. Licensed under the MIT license
|
|
13
13
|
*/
|
package/scripts/docs.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* File: docs.ts
|
|
3
|
-
*
|
|
4
|
-
* Copyright (c) 2022-2022 pikokr
|
|
5
|
-
*
|
|
6
|
-
* Licensed under MIT License. Please see more defails in LICENSE file.
|
|
7
|
-
*/
|
|
8
|
-
|
|
1
|
+
/*
|
|
2
|
+
* File: docs.ts
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2022-2022 pikokr
|
|
5
|
+
*
|
|
6
|
+
* Licensed under MIT License. Please see more defails in LICENSE file.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
9
|
import { runGenerator } from '@discordjs/ts-docgen'
|
|
10
10
|
|
|
11
11
|
runGenerator({
|
|
@@ -1,17 +1,21 @@
|
|
|
1
|
+
import { ApplicationCommandType } from 'discord.js'
|
|
1
2
|
import { BaseComponent, createComponentDecorator } from '../core'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
type ApplicationCommandOptions = {
|
|
5
|
+
type: ApplicationCommandType
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
type ApplicationCommandRequiredOptions = {
|
|
4
9
|
name: string
|
|
5
10
|
description: string
|
|
6
11
|
}
|
|
7
12
|
|
|
8
|
-
export class
|
|
13
|
+
export class ApplicationCommandComponent extends BaseComponent<ApplicationCommandOptions, ApplicationCommandRequiredOptions> {
|
|
9
14
|
defaultOptions() {
|
|
10
15
|
return {
|
|
11
|
-
|
|
12
|
-
description: '',
|
|
16
|
+
type: ApplicationCommandType.ChatInput,
|
|
13
17
|
}
|
|
14
18
|
}
|
|
15
19
|
}
|
|
16
20
|
|
|
17
|
-
export const applicationCommand = createComponentDecorator(
|
|
21
|
+
export const applicationCommand = createComponentDecorator(ApplicationCommandComponent)
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from './ApplicationCommand'
|
|
2
2
|
export { option } from './ApplicationCommandOption'
|
|
@@ -2,18 +2,20 @@ 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, RequiredOptions = unknown> {
|
|
6
|
+
options: Options & RequiredOptions
|
|
7
7
|
|
|
8
8
|
method: Function
|
|
9
9
|
|
|
10
10
|
argTypes: Collection<number, ComponentArgument> = new Collection()
|
|
11
11
|
|
|
12
|
-
constructor(options: Partial<Options
|
|
12
|
+
constructor(options: Partial<Options> & RequiredOptions, method: Function, argTypes: unknown[]) {
|
|
13
13
|
if (typeof options === 'object') {
|
|
14
14
|
this.options = _.merge(this.defaultOptions(), options)
|
|
15
|
+
} else if (typeof options === 'string') {
|
|
16
|
+
this.options = options as this['options']
|
|
15
17
|
} else {
|
|
16
|
-
this.options = options
|
|
18
|
+
this.options = null as unknown as this['options']
|
|
17
19
|
}
|
|
18
20
|
this.method = method
|
|
19
21
|
for (let i = 0; i < argTypes.length; i++) {
|
|
@@ -22,8 +24,8 @@ export class BaseComponent<Options = unknown> {
|
|
|
22
24
|
}
|
|
23
25
|
}
|
|
24
26
|
|
|
25
|
-
defaultOptions(): Options {
|
|
26
|
-
return
|
|
27
|
+
defaultOptions(): Options & Partial<RequiredOptions> {
|
|
28
|
+
return {} as unknown as ReturnType<this['defaultOptions']>
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
execute(target: object, args: unknown[]) {
|
|
@@ -3,11 +3,11 @@ import { ComponentStoreSymbol } from '../symbols'
|
|
|
3
3
|
import { BaseComponent } from './BaseComponent'
|
|
4
4
|
import { ComponentArgumentDecorator } from './ComponentArgumentDecorator'
|
|
5
5
|
|
|
6
|
-
type ComponentStore = Collection<string|symbol, BaseComponent>
|
|
6
|
+
type ComponentStore = Collection<string | symbol, BaseComponent>
|
|
7
7
|
type ComponentArgumentStore = Collection<number, ComponentArgumentDecorator>
|
|
8
8
|
|
|
9
9
|
export const getComponentStore = (target: object): ComponentStore => {
|
|
10
|
-
let result: ComponentStore|null = Reflect.getMetadata(ComponentStoreSymbol, target)
|
|
10
|
+
let result: ComponentStore | null = Reflect.getMetadata(ComponentStoreSymbol, target)
|
|
11
11
|
|
|
12
12
|
if (!result) {
|
|
13
13
|
result = new Collection()
|
|
@@ -18,14 +18,14 @@ export const getComponentStore = (target: object): ComponentStore => {
|
|
|
18
18
|
return result
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
export const getComponent = (target: object, key: string|symbol) => {
|
|
21
|
+
export const getComponent = (target: object, key: string | symbol) => {
|
|
22
22
|
const store = getComponentStore(target)
|
|
23
23
|
|
|
24
24
|
return store.get(key)
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
export const createComponentDecorator = <Options>(type: typeof BaseComponent<Options>) => {
|
|
28
|
-
return (options: Options): MethodDecorator => {
|
|
27
|
+
export const createComponentDecorator = <Options, RequiredOptions>(type: typeof BaseComponent<Options, RequiredOptions>) => {
|
|
28
|
+
return (options: Partial<Options> & RequiredOptions): MethodDecorator => {
|
|
29
29
|
return (target, key) => {
|
|
30
30
|
var component: BaseComponent<Options> = new type(options, Reflect.get(target, key), Reflect.getMetadata('design:paramtypes', target, key))
|
|
31
31
|
|
|
@@ -33,7 +33,7 @@ export const createComponentDecorator = <Options>(type: typeof BaseComponent<Opt
|
|
|
33
33
|
|
|
34
34
|
const decorators = getComponentArgumentStore(target, key)
|
|
35
35
|
|
|
36
|
-
decorators.forEach((x, i)=> {
|
|
36
|
+
decorators.forEach((x, i) => {
|
|
37
37
|
component.argTypes.get(i)?.decorators.push(x)
|
|
38
38
|
})
|
|
39
39
|
|
|
@@ -42,8 +42,8 @@ export const createComponentDecorator = <Options>(type: typeof BaseComponent<Opt
|
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
export const getComponentArgumentStore = (target: object, key: string|symbol): ComponentArgumentStore => {
|
|
46
|
-
let result: ComponentArgumentStore|null = Reflect.getMetadata(ComponentStoreSymbol, target, key)
|
|
45
|
+
export const getComponentArgumentStore = (target: object, key: string | symbol): ComponentArgumentStore => {
|
|
46
|
+
let result: ComponentArgumentStore | null = Reflect.getMetadata(ComponentStoreSymbol, target, key)
|
|
47
47
|
|
|
48
48
|
if (!result) {
|
|
49
49
|
result = new Collection()
|
|
@@ -65,4 +65,3 @@ export const createArgumentDecorator = <Options>(type: typeof ComponentArgumentD
|
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
|
-
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './moduleHook'
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Collection } from 'discord.js'
|
|
2
|
+
import { ModuleHookStoreSymbol } from '../symbols'
|
|
3
|
+
|
|
4
|
+
type ModuleHookStore = Collection<string | symbol, Function[]>
|
|
5
|
+
|
|
6
|
+
export const getModuleHookStore = (target: object) => {
|
|
7
|
+
let result: ModuleHookStore | null = Reflect.getMetadata(ModuleHookStoreSymbol, target)
|
|
8
|
+
|
|
9
|
+
if (!result) {
|
|
10
|
+
result = new Collection()
|
|
11
|
+
|
|
12
|
+
Reflect.defineMetadata(ModuleHookStoreSymbol, result, target)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return result
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const moduleHook = (name: string): MethodDecorator => {
|
|
19
|
+
return (target, key) => {
|
|
20
|
+
const store = getModuleHookStore(target)
|
|
21
|
+
|
|
22
|
+
let v = store.get(key)
|
|
23
|
+
|
|
24
|
+
if (!v) {
|
|
25
|
+
v = []
|
|
26
|
+
store.set(key, v)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
v.push(Reflect.get(target, key))
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/core/index.ts
CHANGED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { BaseComponent, createComponentDecorator } from '../components'
|
|
2
|
+
|
|
3
|
+
export class ListenerComponent extends BaseComponent<{ emitter: string }, { event: string }> {
|
|
4
|
+
defaultOptions() {
|
|
5
|
+
return { emitter: 'discord' }
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const listener = createComponentDecorator(ListenerComponent)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Client } from 'discord.js'
|
|
2
|
+
import EventEmitter from 'events'
|
|
3
|
+
import { Registry } from './Registry'
|
|
4
|
+
|
|
5
|
+
export class CommandClient extends EventEmitter {
|
|
6
|
+
registry = new Registry()
|
|
7
|
+
|
|
8
|
+
constructor(public discord: Client) {
|
|
9
|
+
super()
|
|
10
|
+
|
|
11
|
+
this.registry.registerEventEmitter('cts', this)
|
|
12
|
+
this.registry.registerEventEmitter('discord', this.discord)
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Collection } from 'discord.js'
|
|
2
|
+
import EventEmitter from 'events'
|
|
3
|
+
import _ from 'lodash'
|
|
4
|
+
import { BaseComponent, getComponentStore } from '../components'
|
|
5
|
+
import { getModuleHookStore } from '../hooks'
|
|
6
|
+
import { ListenerComponent } from '../listener'
|
|
7
|
+
import { ListenersSymbol } from '../symbols'
|
|
8
|
+
|
|
9
|
+
export class Registry {
|
|
10
|
+
extensions: object[] = []
|
|
11
|
+
|
|
12
|
+
emitters: Collection<string, EventEmitter> = new Collection()
|
|
13
|
+
|
|
14
|
+
getComponentsWithTypeGlobal<T extends typeof BaseComponent<Config, RequiredConfig>, Config, RequiredConfig>(type: T): InstanceType<T>[] {
|
|
15
|
+
const result: InstanceType<T>[] = []
|
|
16
|
+
|
|
17
|
+
for (const ext of this.extensions) {
|
|
18
|
+
result.push(...this.getComponentsWithType(ext, type))
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return result
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
getComponentsWithType<T extends typeof BaseComponent<Config, RequiredConfig>, Config, RequiredConfig>(ext: object, type: T): InstanceType<T>[] {
|
|
25
|
+
const componentStore = getComponentStore(ext)
|
|
26
|
+
|
|
27
|
+
return Array.from(componentStore.filter((x) => (x.constructor as unknown) === type).values() as Iterable<InstanceType<T>>)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
registerEventListeners(ext: object) {
|
|
31
|
+
const listeners = this.getComponentsWithType(ext, ListenerComponent)
|
|
32
|
+
|
|
33
|
+
for (const listener of listeners) {
|
|
34
|
+
const emitter = this.emitters.get(listener.options.emitter)
|
|
35
|
+
|
|
36
|
+
if (emitter) {
|
|
37
|
+
const bound = listener.method.bind(ext)
|
|
38
|
+
|
|
39
|
+
Reflect.defineMetadata('bound', bound, listener)
|
|
40
|
+
|
|
41
|
+
emitter.addListener(listener.options.event, bound)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
unregisterEventListeners(ext: object) {
|
|
47
|
+
const listeners = this.getComponentsWithType(ext, ListenerComponent)
|
|
48
|
+
|
|
49
|
+
for (const listener of listeners) {
|
|
50
|
+
const emitter = this.emitters.get(listener.options.emitter)
|
|
51
|
+
const bound = Reflect.getMetadata('bound', listener)
|
|
52
|
+
|
|
53
|
+
if (emitter && bound) {
|
|
54
|
+
emitter.removeListener(listener.options.event, bound)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async registerModule(ext: object) {
|
|
60
|
+
this.registerEventListeners(ext)
|
|
61
|
+
await this.runModuleHook(ext, 'load')
|
|
62
|
+
this.extensions.push(ext)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async unregisterModule(ext: object) {
|
|
66
|
+
this.unregisterEventListeners(ext)
|
|
67
|
+
await this.runModuleHook(ext, 'unload')
|
|
68
|
+
_.remove(this.extensions, (x) => x === ext)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
runModuleHook(ext: object, hookName: string, ...args: unknown[]) {
|
|
72
|
+
const hooks = getModuleHookStore(ext)
|
|
73
|
+
|
|
74
|
+
const functions = hooks.get(hookName)
|
|
75
|
+
|
|
76
|
+
if (functions) {
|
|
77
|
+
for (const fn of functions) {
|
|
78
|
+
fn.apply(ext, args)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
registerEventEmitter(name: string, emitter: EventEmitter) {
|
|
84
|
+
this.emitters.set(name, emitter)
|
|
85
|
+
}
|
|
86
|
+
}
|
package/src/core/symbols.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* File: index.ts
|
|
3
|
-
*
|
|
4
|
-
* Copyright (c) 2022-2022 pikokr
|
|
5
|
-
*
|
|
6
|
-
* Licensed under MIT License. Please see more defails in LICENSE file.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
export * from './core'
|
|
10
|
-
export * from './applicationCommand'
|
|
1
|
+
/*
|
|
2
|
+
* File: index.ts
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2022-2022 pikokr
|
|
5
|
+
*
|
|
6
|
+
* Licensed under MIT License. Please see more defails in LICENSE file.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export * from './core'
|
|
10
|
+
export * from './applicationCommand'
|
package/test/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { ApplicationCommandOptionType } from 'discord.js'
|
|
2
|
-
import { applicationCommand,
|
|
1
|
+
import { ApplicationCommandOptionType, Client } from 'discord.js'
|
|
2
|
+
import { applicationCommand, ApplicationCommandComponent, CommandClient, moduleHook, option, Registry } from '../src'
|
|
3
|
+
import { listener } from '../src/core/listener'
|
|
3
4
|
|
|
4
5
|
class Test {
|
|
5
6
|
@applicationCommand({
|
|
@@ -28,10 +29,41 @@ class Test {
|
|
|
28
29
|
})
|
|
29
30
|
wa: string,
|
|
30
31
|
) {}
|
|
32
|
+
|
|
33
|
+
@moduleHook('load')
|
|
34
|
+
load() {
|
|
35
|
+
console.log('load')
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@moduleHook('unload')
|
|
39
|
+
unload() {
|
|
40
|
+
console.log('unload')
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
@listener({ event: 'test', emitter: 'discord' })
|
|
44
|
+
testEvent() {
|
|
45
|
+
console.log('test')
|
|
46
|
+
}
|
|
31
47
|
}
|
|
32
48
|
|
|
33
49
|
const ext = new Test()
|
|
34
50
|
|
|
35
|
-
const
|
|
51
|
+
const client = new Client({ intents: [] })
|
|
52
|
+
|
|
53
|
+
const cc = new CommandClient(client)
|
|
54
|
+
|
|
55
|
+
const registry = cc.registry
|
|
56
|
+
|
|
57
|
+
const run = async () => {
|
|
58
|
+
await registry.registerModule(ext)
|
|
59
|
+
|
|
60
|
+
// listener test
|
|
61
|
+
client.emit('test')
|
|
62
|
+
|
|
63
|
+
await registry.unregisterModule(ext)
|
|
64
|
+
|
|
65
|
+
// shold not work
|
|
66
|
+
client.emit('test')
|
|
67
|
+
}
|
|
36
68
|
|
|
37
|
-
|
|
69
|
+
run()
|
package/tsconfig.json
CHANGED
|
@@ -15,11 +15,10 @@
|
|
|
15
15
|
"declaration": true,
|
|
16
16
|
/* Generates corresponding '.d.ts' file. */
|
|
17
17
|
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
|
|
18
|
-
|
|
18
|
+
"sourceMap": true /* Generates corresponding '.map' file. */,
|
|
19
19
|
// "outFile": "./", /* Concatenate and emit output to single file. */
|
|
20
20
|
"outDir": "./dist",
|
|
21
21
|
/* Redirect output structure to the directory. */
|
|
22
|
-
"rootDir": "./src",
|
|
23
22
|
/* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
|
|
24
23
|
// "composite": true, /* Enable project compilation */
|
|
25
24
|
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
|
|
@@ -31,6 +30,7 @@
|
|
|
31
30
|
|
|
32
31
|
/* Strict Type-Checking Options */
|
|
33
32
|
"strict": true,
|
|
33
|
+
|
|
34
34
|
/* Enable all strict type-checking options. */
|
|
35
35
|
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
|
|
36
36
|
// "strictNullChecks": true, /* Enable strict null checks. */
|
package/tsconfig.prod.json
CHANGED
package/tsup.config.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* File: tsup.config.ts
|
|
3
|
-
*
|
|
4
|
-
* Copyright (c) 2022-2022 pikokr
|
|
5
|
-
*
|
|
6
|
-
* Licensed under MIT License. Please see more defails in LICENSE file.
|
|
7
|
-
*/
|
|
8
|
-
|
|
1
|
+
/*
|
|
2
|
+
* File: tsup.config.ts
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2022-2022 pikokr
|
|
5
|
+
*
|
|
6
|
+
* Licensed under MIT License. Please see more defails in LICENSE file.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
9
|
import { defineConfig } from 'tsup'
|
|
10
10
|
|
|
11
11
|
export default defineConfig({
|
|
@@ -15,4 +15,5 @@ export default defineConfig({
|
|
|
15
15
|
clean: true,
|
|
16
16
|
dts: true,
|
|
17
17
|
minify: true,
|
|
18
|
+
tsconfig: 'tsconfig.prod.json',
|
|
18
19
|
})
|