Skip to content

Commit 4bdf3c9

Browse files
committed
feat: plugin events wip
1 parent 76a6b13 commit 4bdf3c9

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

packages/commandkit/src/plugins/runtime/CommandKitPluginRuntime.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1-
import { Collection, Interaction } from 'discord.js';
1+
import { Collection, Events } from 'discord.js';
22
import { CommandKit } from '../../CommandKit';
33
import { RuntimePlugin } from '../RuntimePlugin';
44
import { AsyncFunction } from '../../cache';
55
import {
66
CommandKitErrorCodes,
77
createCommandKitError,
8-
isCommandKitError,
98
isErrorType,
109
} from '../../utils/error-codes';
1110
import { 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;

packages/commandkit/src/plugins/runtime/CompilerPluginRuntime.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
OnResolveResult,
88
Setup,
99
} from './types';
10-
import { getConfig } from '../../config';
10+
import { findCommandKitConfig } from '../../cli/common';
1111

1212
const pattern = /\.(c|m)?(t|j)sx?$/;
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}`,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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>;

0 commit comments

Comments
 (0)