feat(handler) : refactored ready event and finished loading command

plugins
This commit is contained in:
Jacob Nguyen
2022-04-15 22:45:01 -05:00
parent f213e88a5d
commit 4e1a8066d2
3 changed files with 31 additions and 9 deletions

View File

@@ -1,12 +1,13 @@
import { from, fromEvent, map, take,concat, concatAll, mergeMap, skip} from 'rxjs'; import { basename } from 'path';
import { from, fromEvent, map, take,concat, concatAll, mergeMap, skip, Observable, defer} 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';
import { CommandType } from '../sern';
import type { CommandPlugin, SernPlugin } from '../plugins/plugin';
import { partition } from './observableHandling';
import { Err, Ok } from 'ts-results';
import { Err, Ok, Result } from 'ts-results';
import type { PluggedModule } from '../structures/modules/module';
import type { Awaitable } from 'discord.js';
export const onReady = ( wrapper : Wrapper ) => {
const { client, commands } = wrapper;
@@ -31,16 +32,24 @@ export const onReady = ( wrapper : Wrapper ) => {
})
}),
);
concat(ready$.pipe(skip(1)),processCommandFiles$)
.subscribe( _ => {
if(a.ok) {
(concat(ready$.pipe(skip(1)),processCommandFiles$) as Observable<{
res : Awaitable<Result<void, void>>, plugged : PluggedModule
}>).pipe(
mergeMap(async( {res, plugged})=> ({ res:await res, plugged }) )
)
.subscribe( ({ res, plugged: { mod, plugins }}) => {
if(res.ok) {
registerModule(mod.name!, mod, plugins)
} else {
// TODO: add event emitter for command failures
console.log('a plugin failed to load');
console.log(`Did not register command ${mod.name!}`)
console.log(mod);
}
})
}
// Refactor : ? Possibly repetitive and verbose.
const handler = ( name : string ) =>
({

View File

@@ -1,5 +1,5 @@
import type { Client } from "discord.js";
import type { Module } from "../..";
import { Module, Sern } from "../..";
import { CommandPlugin, Controller, PluginType } from "./plugin";
export function reload(
@@ -13,6 +13,7 @@ export function reload(
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,
})

View File

@@ -2,12 +2,14 @@ import type {
DiscordEvent,
} from '../types/handler';
import type {
import {
ApplicationCommandType,
Client,
MessageType,
} from 'discord.js';
import type Wrapper from './structures/wrapper';
import { fromEvent } from 'rxjs';
import { fromEvent, throwError } from 'rxjs';
import { SernError } from './structures/errors';
import { onReady } from './events/readyEvent';
import { onMessageCreate } from './events/messageEvent';
@@ -45,3 +47,13 @@ export enum CommandType {
Auto = 0b1000000
}
export function cmdTypeToDjs(ty: CommandType) {
switch (ty) {
case CommandType.Slash : case CommandType.Both : return ApplicationCommandType.ChatInput;
case CommandType.MenuUser : return ApplicationCommandType.User;
case CommandType.MenuMsg : return ApplicationCommandType.Message;
default : throw new Error(`Cannot turn this CommandType to ApplicationCommandType`)
}
}