File tree Expand file tree Collapse file tree 3 files changed +42
-7
lines changed
packages/commandkit/src/plugins/runtime Expand file tree Collapse file tree 3 files changed +42
-7
lines changed Original file line number Diff line number Diff line change 1- import { Collection , Interaction } from 'discord.js' ;
1+ import { Collection , Events } from 'discord.js' ;
22import { CommandKit } from '../../CommandKit' ;
33import { RuntimePlugin } from '../RuntimePlugin' ;
44import { AsyncFunction } from '../../cache' ;
55import {
66 CommandKitErrorCodes ,
77 createCommandKitError ,
8- isCommandKitError ,
98 isErrorType ,
109} from '../../utils/error-codes' ;
1110import { Logger } from '../../logger/Logger' ;
11+ import { PluginEvents } from './runtime-events' ;
1212
13- export class CommandKitPluginRuntime {
13+ export class CommandKitPluginRuntime extends EventTarget {
1414 private plugins = new Collection < string , RuntimePlugin > ( ) ;
15+ private onClientLogin = ( ) => {
16+ this . dispatchEvent ( new PluginEvents . ClientLogin ( ) ) ;
17+ } ;
1518
16- public constructor ( public readonly commandkit : CommandKit ) { }
19+ public constructor ( public readonly commandkit : CommandKit ) {
20+ super ( ) ;
21+
22+ this . commandkit . client . on ( Events . ClientReady , this . onClientLogin ) ;
23+ }
1724
1825 public getPlugin ( pluginName : string ) : RuntimePlugin | null {
1926 return this . plugins . get ( pluginName ) ?? null ;
Original file line number Diff line number Diff line change 77 OnResolveResult ,
88 Setup ,
99} from './types' ;
10- import { getConfig } from '../../config ' ;
10+ import { findCommandKitConfig } from '../../cli/common ' ;
1111
1212const pattern = / \. ( c | m ) ? ( t | j ) s x ? $ / ;
1313
@@ -16,6 +16,10 @@ export class CompilerPluginRuntime {
1616
1717 public constructor ( private readonly plugins : CompilerPlugin [ ] ) { }
1818
19+ public getConfig ( ) {
20+ return findCommandKitConfig ( process . cwd ( ) ) ;
21+ }
22+
1923 private async onLoad ( args : OnLoadArgs ) : Promise < OnLoadResult > {
2024 const source = await readFile ( args . path , 'utf8' ) ;
2125
@@ -116,7 +120,7 @@ export class CompilerPluginRuntime {
116120 private async onDispose ( ) {
117121 for ( const plugin of this . plugins ) {
118122 try {
119- await plugin . deactivate ?.( getConfig ( ) ) ;
123+ await plugin . deactivate ?.( this ) ;
120124 } catch ( e : any ) {
121125 console . error (
122126 `Plugin ${ plugin . name } failed to deactivate with ${ e ?. stack || e } ` ,
@@ -128,7 +132,7 @@ export class CompilerPluginRuntime {
128132 private async onInit ( ) {
129133 for ( const plugin of this . plugins ) {
130134 try {
131- await plugin . activate ?.( getConfig ( ) ) ;
135+ await plugin . activate ?.( this ) ;
132136 } catch ( e : any ) {
133137 console . error (
134138 `Plugin ${ plugin . name } failed to activate with ${ e ?. stack || e } ` ,
Original file line number Diff line number Diff line change 1+ import { CommandKitPluginRuntime } from '../..' ;
2+
3+ const CommandKitPluginEvents = {
4+ 'client-login' : {
5+ name : 'ClientLogin' ,
6+ cancelable : false ,
7+ } ,
8+ } as const ;
9+
10+ type EventName =
11+ ( typeof CommandKitPluginEvents ) [ keyof typeof CommandKitPluginEvents ] [ 'name' ] ;
12+
13+ export const PluginEvents = Object . fromEntries (
14+ Object . entries ( CommandKitPluginEvents ) . map ( ( [ key , value ] ) => [
15+ value . name ,
16+ class extends Event {
17+ public target ! : CommandKitPluginRuntime ;
18+
19+ public constructor ( ) {
20+ super ( key , { cancelable : value . cancelable } ) ;
21+ }
22+ } ,
23+ ] ) ,
24+ ) as unknown as Record < EventName , new ( ) => Event > ;
You can’t perform that action at this time.
0 commit comments