mirror of
https://github.com/sern-handler/handler
synced 2026-06-22 07:42:14 +00:00
feat : can register plugin modules now
This commit is contained in:
@@ -5,6 +5,7 @@ import type Wrapper from '../structures/wrapper';
|
||||
import type { Module } from '../structures/structxports';
|
||||
import type { HandlerCallback, ModuleHandlers, ModuleStates, ModuleType } from '../structures/modules/commands/moduleHandler';
|
||||
import { CommandType } from '../sern';
|
||||
import type { PluggedModule } from '../structures/modules/module';
|
||||
|
||||
export const onReady = ( wrapper : Wrapper ) => {
|
||||
const { client, init, commands } = wrapper;
|
||||
@@ -48,19 +49,22 @@ const handler = ( name : string ) =>
|
||||
const registerModules = <T extends ModuleType >(name : string, mod : ModuleStates[T]) =>
|
||||
(<HandlerCallback<T>> handler(name)[mod.type])(mod);
|
||||
|
||||
function setCommands ( { mod, absPath } : { mod : Module, absPath : string } ) {
|
||||
const name = mod.name ?? Files.fmtFileName(basename(absPath));
|
||||
|
||||
function setCommands ( { plugged, absPath } : { plugged: PluggedModule, absPath : string } ) {
|
||||
const name = plugged.mod.name ?? Files.fmtFileName(basename(absPath));
|
||||
// making all modules have name property
|
||||
if (mod.name === undefined ) {
|
||||
registerModules(name, { name, ...mod });
|
||||
if (plugged.mod.name === undefined ) {
|
||||
registerModules(name, { name, ...plugged.mod });
|
||||
} else {
|
||||
registerModules(name, mod);
|
||||
registerModules(name, plugged.mod);
|
||||
}
|
||||
}
|
||||
|
||||
function createCommandCache(
|
||||
arr: {mod: Module, absPath: string}[]
|
||||
arr: {plugged: PluggedModule, absPath: string}[]
|
||||
) {
|
||||
// possible mem leak?
|
||||
from(arr).subscribe ( setCommands );
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
import type { Err, Ok, Result } from "ts-results";
|
||||
import type { Override, Wrapper } from "../..";
|
||||
import type { BaseModule } from "../structures/modules/module";
|
||||
import { apply, BaseModule, sernModule } from "../structures/modules/module";
|
||||
|
||||
export enum PluginType {
|
||||
Command = 0b00,
|
||||
@@ -27,15 +27,17 @@ interface Controller {
|
||||
|
||||
}
|
||||
|
||||
type executePlugin = { execute : ( controller : Controller ) => Result<void, void> }
|
||||
type executeCmdPlugin = { execute : ( wrapper : Wrapper, controller : Controller ) => Result<void, void> }
|
||||
|
||||
interface BasePlugin extends Override<BaseModule, executePlugin>{
|
||||
interface BasePlugin extends Override<BaseModule, executeCmdPlugin>{
|
||||
type : PluginType
|
||||
}
|
||||
|
||||
export type CommandPlugin = {
|
||||
type : PluginType.Command
|
||||
} & Override<BasePlugin, { execute : ( wrapper : Wrapper ) => Result<void,void>}>;
|
||||
} & Override<BasePlugin, {
|
||||
execute : (wrapper:Wrapper, controller:Controller) => Result<void,void>
|
||||
}>;
|
||||
|
||||
export type EventPlugin = {
|
||||
type : PluginType.Event
|
||||
@@ -46,5 +48,11 @@ export type SernPlugin =
|
||||
| EventPlugin;
|
||||
|
||||
|
||||
sernModule(
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { Awaitable, ChatInputCommandInteraction, Interaction } from "discord.js";
|
||||
import type { Awaitable, ChatInputCommandInteraction } from "discord.js";
|
||||
import type { Args, Module } from "../../..";
|
||||
import type { CommandPlugin, EventPlugin, SernPlugin } from "../../plugins/plugin";
|
||||
import type Context from "../context";
|
||||
@@ -9,8 +9,8 @@ export interface BaseModule {
|
||||
execute: (ctx: Context, args: Args) => Awaitable<void>;
|
||||
}
|
||||
|
||||
export interface PluggedModule<T extends Module> {
|
||||
mod : T;
|
||||
export interface PluggedModule {
|
||||
mod : Module;
|
||||
plugins : SernPlugin[];
|
||||
}
|
||||
|
||||
@@ -26,10 +26,10 @@ export function apply(...plugins: SernPlugin[]) {
|
||||
return plugins;
|
||||
}
|
||||
|
||||
export function sernModule<T extends Module>
|
||||
(plugins : () => SernPlugin[], mod : T, ) : PluggedModule<T> {
|
||||
export function sernModule
|
||||
(plugins : SernPlugin[], mod : Module, ) : PluggedModule {
|
||||
return {
|
||||
mod,
|
||||
plugins : plugins()
|
||||
plugins
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { readdirSync, statSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import type { Module } from '../structures/modules/commands/module';
|
||||
import { SernError } from '../structures/errors';
|
||||
import type { PluggedModule } from '../structures/modules/module';
|
||||
|
||||
//We can look into lazily loading modules once everything is set
|
||||
export const ContextMenuUser = new Map<string, Module>();
|
||||
@@ -32,20 +33,20 @@ export const fmtFileName = (n: string) => n.substring(0, n.length - 3);
|
||||
/**
|
||||
*
|
||||
* @param {commandsDir} Relative path to commands directory
|
||||
* @returns {Promise<{ mod: Command; absPath: string; }[]>} data from command files
|
||||
* @returns {Promise<{ mod: PluggedModule; absPath: string; }[]>} data from command files
|
||||
*/
|
||||
|
||||
export async function buildData(commandDir: string ): Promise<
|
||||
{
|
||||
mod: Module;
|
||||
plugged: PluggedModule;
|
||||
absPath: string;
|
||||
}[]
|
||||
> {
|
||||
return Promise.all(
|
||||
getCommands(commandDir).map( async (absPath) => {
|
||||
const mod = <Module> (await import(absPath)).module;
|
||||
if (mod === undefined) throw Error(`${SernError.UndefinedModule} ${absPath}`);
|
||||
return { mod, absPath };
|
||||
const plugged = <PluggedModule> (await import(absPath)).module;
|
||||
if (plugged === undefined) throw Error(`${SernError.UndefinedModule} ${absPath}`);
|
||||
return { plugged , absPath };
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user