feat : finish basic message event handler

This commit is contained in:
Jacob Nguyen
2022-03-17 11:33:46 -05:00
parent 2ea35588bd
commit 1ad429fb5f
2 changed files with 25 additions and 10 deletions

View File

@@ -1,32 +1,43 @@
import type { Message } from "discord.js";
import { map, filter, fromEvent, Observable, of, concatMap } from "rxjs";
import { map, filter, fromEvent, Observable, of, concatMap, tap } from "rxjs";
import { None, Some } from "ts-results";
import Context from "../structures/context";
import type Wrapper from "../structures/wrapper";
import { isNotFromDM, isNotFromBot, hasPrefix, fmt } from "../utilities/messageHelpers";
import * as Files from '../utilities/readFile';
export const onMessageCreate = ( wrapper : Wrapper) => {
export const onMessageCreate = (wrapper : Wrapper) => {
const { client, defaultPrefix } = wrapper;
(fromEvent( client, 'messageCreate') as Observable<Message>)
.pipe (
filter( isNotFromBot ),
filter( isNotFromDM ),
filter( m => hasPrefix(m, defaultPrefix)),
concatMap ( m => of(fmt(m, defaultPrefix))
concatMap ( m => of( fmt(m, defaultPrefix) )
.pipe (
map(([prefix, ...args ]) =>{
return [Files.Commands.get(prefix) ?? Files.Alias.get(prefix), new Context(Some(m), None), args ] as const;
}),
filter( ([mod]) => mod !== undefined),
map ( ([mod, ctx, args ]) => {
map ( async ([ mod, ctx, args ]) => {
const parsedArgs = mod!.parse?.(ctx, args);
return mod!.execute(ctx, parsedArgs);
})
const res = await mod!.execute(ctx, parsedArgs);
// some ducktape lol
return res !== undefined ? ctx.messageUnchecked.channel.send(res) : null;
}),
)
)
).subscribe()
).subscribe ({
error() {
//log things
console.log('Failed to finished message subscription!');
},
next(command) {
//log on each command emitted
command.then( res => console.log(`a command returned ${ res ?? `no value`}`));
},
})
}

View File

@@ -3,7 +3,7 @@ import { basename } from 'path';
import * as Files from '../utilities/readFile';
import type Wrapper from '../structures/wrapper';
import type { Modules } from "../structures/structxports";
import type { HandlerCallback, ModuleDefs, ModuleHandlers, ModuleStates, ModuleType } from "../structures/commands/moduleHandler";
import type { HandlerCallback, ModuleHandlers, ModuleStates, ModuleType } from "../structures/commands/moduleHandler";
import { CommandType } from "../sern";
export const onReady = ( wrapper : Wrapper ) => {
@@ -19,7 +19,12 @@ export const onReady = ( wrapper : Wrapper ) => {
)
),
)
.subscribe( () => console.log(Files.Commands));
.subscribe({
complete() {
// on ready event, complete!
// log stuff?
}
});
}
const handler = ( name : string ) =>
@@ -48,6 +53,5 @@ function setCommands ( { mod, absPath } : { mod : Modules.Module, absPath : stri
async function createCommandCache(
arr: Promise<{mod: Modules.Module, absPath: string}[]>
) {
console.log(await arr);
from(await arr).subscribe ( setCommands );
}