feat(handler): command plugins work?!

This commit is contained in:
Jacob Nguyen
2022-04-16 17:27:24 -05:00
parent 4e1a8066d2
commit 70bd12dd61
2 changed files with 13 additions and 28 deletions

View File

@@ -1,4 +1,5 @@
import { from, fromEvent, map, take,concat, concatAll, mergeMap, skip, Observable, defer} from 'rxjs'; import { basename } from 'path';
import { from, fromEvent, map, take,concat, concatAll, mergeMap, skip, Observable} from 'rxjs';
import { basename } from 'path';
import * as Files from '../utilities/readFile';
import type Wrapper from '../structures/wrapper';
import type { HandlerCallback, ModuleHandlers, ModuleStates, ModuleType } from '../structures/modules/commands/moduleHandler';
@@ -11,12 +12,12 @@ import type { Awaitable } from 'discord.js';
export const onReady = ( wrapper : Wrapper ) => {
const { client, commands } = wrapper;
const ready$ = fromEvent(client, 'ready').pipe(take(1));
const ready$ = fromEvent(client, 'ready').pipe(take(1),skip(1));
const processCommandFiles$ = from(Files.buildData(commands)).pipe(
concatAll(),
map(({plugged, absPath}) => {
const name = plugged.mod.name ?? Files.fmtFileName(basename(absPath));
if (plugged.mod.name === undefined ) {
const name = plugged.mod?.name ?? Files.fmtFileName(basename(absPath));
if (plugged.mod?.name === undefined ) {
return { mod: { name, ...plugged.mod }, plugins : plugged.plugins };
}
return plugged;
@@ -32,12 +33,14 @@ export const onReady = ( wrapper : Wrapper ) => {
})
}),
);
(concat(ready$.pipe(skip(1)),processCommandFiles$) as Observable<{
(concat(ready$,processCommandFiles$) as Observable<{
res : Awaitable<Result<void, void>>, plugged : PluggedModule
}>).pipe(
mergeMap(async( {res, plugged})=> ({ res:await res, plugged }) )
)
.subscribe( ({ res, plugged: { mod, plugins }}) => {
mergeMap(async( {res, plugged} ) => ({ res:await res, plugged }) )
).subscribe(
({ res, plugged: { mod, plugins }}) => {
if(res.ok) {
registerModule(mod.name!, mod, plugins)
} else {
@@ -87,7 +90,7 @@ function registerModule <T extends ModuleType> (
}
function isCmdPlugin ( p : SernPlugin) : p is CommandPlugin {
return (p.type & 0) !== 0;
return (p.type & 0) === 0;
}

View File

@@ -2,23 +2,5 @@ import type { Client } from "discord.js";
import { Module, Sern } from "../..";
import { CommandPlugin, Controller, PluginType } from "./plugin";
export function reload(
data : { guildId: string, applicationId: string }
) : CommandPlugin {
return {
type : PluginType.Command,
name : 'Refresh',
description : 'Will reload the command this plugin is applied to',
async execute(client : Client, module: Module, controller : Controller ) {
const curGuild = await client.guilds.fetch(data.guildId);
await curGuild.commands.edit(data.applicationId, {
type : Sern.cmdTypeToDjs(module.type),
name : module.name!,
description: module.description,
})
return controller.next()
}
}
}