refactor: a lot

This commit is contained in:
Jacob Nguyen
2023-05-15 17:35:38 -05:00
parent 423b0d2aa7
commit 8f39fc0896
5 changed files with 26 additions and 13 deletions

View File

@@ -27,7 +27,6 @@ export async function defaultModuleLoader<T extends Module>(absPath: string): Mo
module = module.getInstance();
}
//todo readd class modules
assert.ok(module.type > 0 && module.type < 1<<10, 'Found a module that does not have a valid type');
return Ok({ module, absPath });
}

View File

@@ -1,10 +1,12 @@
import { CoreModuleStore } from '../contracts';
import { Module, CommandMeta } from '../types/modules';
/*
* @internal
* Version 4.0.0 will internalize this api. Please refrain from using ModuleStore!
* For interacting with ModuleStore, use CoreModuleStore contract.
* For interacting with modules, use the ModuleManager instead.
*/
export class ModuleStore implements CoreModuleStore {
metadata = new WeakMap<Module, CommandMeta>();
commands = new Map<string, string>();
}

View File

@@ -1,6 +1,6 @@
import { ClientEvents } from 'discord.js';
import { CommandType, EventType, PluginType } from '../core/structures';
import { AnyCommandPlugin, AnyEventPlugin, CommandArgs, EventArgs } from '../core/types/plugins';
import { AnyCommandPlugin, AnyEventPlugin, CommandArgs, ControlPlugin, EventArgs } from '../core/types/plugins';
import { CommandModule, EventModule, InputCommand, InputEvent } from '../core/types/modules';
import { partitionPlugins } from '../core/functions';
import { Awaitable } from '../shared';
@@ -62,13 +62,20 @@ export function discordEvent<T extends keyof ClientEvents>(mod: {
export abstract class CommandExecutable<const Type extends CommandType> {
abstract type: Type;
plugins?: AnyCommandPlugin[];
onEvent?: ControlPlugin[]
private static _instance : CommandModule;
static readonly [clazz] = true;
constructor() {
const [onEvent, plugins] = partitionPlugins(this.plugins);
this.plugins = plugins as AnyCommandPlugin[];
Reflect.set(this, 'onEvent', onEvent);
if(this.onEvent) {
console.warn('Put control plugins in `onEvent` into the `plugins` field, as it\'s automatically handled in v3.');
} else {
const [onEvent, plugins] = partitionPlugins(this.plugins);
this.plugins = plugins as AnyCommandPlugin[];
Reflect.set(this, 'onEvent', onEvent);
}
}
static getInstance() {
if (!CommandExecutable._instance) {
//@ts-ignore
@@ -76,6 +83,7 @@ export abstract class CommandExecutable<const Type extends CommandType> {
}
return CommandExecutable._instance;
}
abstract execute(...args: CommandArgs<Type, PluginType.Control>) : Awaitable<unknown>
}
@@ -88,11 +96,16 @@ export abstract class EventExecutable<Type extends EventType> {
abstract type: Type;
plugins?: AnyEventPlugin[];
static readonly [clazz] = true;
onEvent?: ControlPlugin[]
private static _instance : EventModule;
constructor() {
const [onEvent, plugins] = partitionPlugins(this.plugins);
this.plugins = plugins as AnyEventPlugin[];
Reflect.set(this, 'onEvent', onEvent);
if(this.onEvent) {
console.warn('Put control plugins in `onEvent` into the `plugins` field, as it\'s automatically handled in v3.');
} else {
const [onEvent, plugins] = partitionPlugins(this.plugins);
this.plugins = plugins as AnyEventPlugin[];
Reflect.set(this, 'onEvent', onEvent);
}
}
static getInstance() {
if (!EventExecutable._instance) {

View File

@@ -7,6 +7,7 @@ import { SernEmitter } from '../../core';
import { Processed, DependencyList } from '../types';
import { buildModules, callInitPlugins } from './generic';
import { AnyModule } from '../../core/types/modules';
import * as assert from 'node:assert';
export function startReadyEvent(
[sEmitter,,, moduleManager, client]: DependencyList,
@@ -41,6 +42,7 @@ function registerModule<T extends Processed<AnyModule>>(
const { id, fullPath } = manager.getMetadata(module);
assert.ok(module.type > 0 && module.type < 1<<10, `Found ${module}, which does not have a valid type`);
if (module.type === CommandType.Both
|| module.type === CommandType.Text
) {

View File

@@ -18,9 +18,6 @@ import { Wrapper } from '../shared';
* Sern.init({
* commands: 'dist/commands',
* events: 'dist/events',
* containerConfig : {
* get: useContainer
* }
* })
* ```
*/
@@ -32,7 +29,7 @@ export function init(wrapper: Wrapper) {
const dependencies = useDependencies();
const logger = dependencies[2];
const errorHandler = dependencies[1];
const mode = debugModuleLoading(process.env.MODE);
const mode = debugModuleLoading(wrapper.mode ?? process.env.MODE);
if (wrapper.events !== undefined) {
makeEventsHandler(