feat : strict commandType checking transition for ergonomics

This commit is contained in:
Jacob Nguyen
2022-04-29 15:52:14 -05:00
parent 2c8fdce7e1
commit 4448d5a1be
4 changed files with 12 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
import type { Message } from 'discord.js';
import { fromEvent, Observable, of, concatMap, map, from, every, concatAll, tap } from 'rxjs';
import { fromEvent, Observable, of, concatMap, map, from, filter, concatAll, tap } from 'rxjs';
import { Err, Ok } from 'ts-results';
import type { Args } from '../..';
import { CommandType } from '../sern';
@@ -7,7 +7,7 @@ import Context from '../structures/context';
import type Wrapper from '../structures/wrapper';
import { fmt } from '../utilities/messageHelpers';
import * as Files from '../utilities/readFile';
import { filterCorrectModule, ignoreNonBot } from './observableHandling';
import { filterCorrectModule, ignoreNonBot, match } from './observableHandling';
import { isEventPlugin } from './readyEvent';
export const onMessageCreate = (wrapper : Wrapper) => {
@@ -23,10 +23,11 @@ export const onMessageCreate = (wrapper : Wrapper) => {
return {
ctx : Context.wrap(message),
args : <Args>['text', rest],
mod : Files.ApplicationCommandStore[1].get(prefix) ?? Files.Alias.get(prefix)
mod : Files.ApplicationCommandStore[1].get(prefix)
?? Files.BothCommand.get(prefix)
?? Files.TextCommandStore.aliases.get(prefix)
}
}));
const ensureModuleType$ = processMessage$.pipe(
concatMap(payload => of(payload.mod)
.pipe(

View File

@@ -10,7 +10,7 @@ import type { EventPlugin, SernPlugin } from '../plugins/plugin';
export function match<T extends keyof ModuleDefs>(
plug: PluggedModule | undefined, type : T
) : plug is { mod: ModuleDefs[T], plugins : SernPlugin[] } {
return plug !== undefined && (plug.mod.type & type) != 0;
return plug !== undefined && plug.mod.type === type;
}
export function filterCorrectModule<T extends keyof ModuleDefs>(cmdType : T) {

View File

@@ -75,15 +75,15 @@ export const onReady = ( wrapper : Wrapper ) => {
function handler( name : string ) : ModuleHandlers {
return {
[CommandType.Text] : (mod, plugins) => {
mod.alias.forEach ( a => Files.Alias.set(a,{ mod, plugins}));
mod.alias.forEach ( a => Files.TextCommandStore.aliases.set(a,{ mod, plugins}));
Files.ApplicationCommandStore[1].set( name, { mod, plugins } );
},
[CommandType.Slash]: (mod, plugins) => {
Files.ApplicationCommandStore[1].set( name , { mod, plugins });
},
[CommandType.Both] :( mod, plugins )=> {
Files.ApplicationCommandStore[1].set ( name,{ mod, plugins});
mod.alias.forEach (a => Files.Alias.set(a, {mod,plugins}));
Files.BothCommand.set ( name,{ mod, plugins});
mod.alias.forEach (a => Files.TextCommandStore.aliases.set(a, {mod,plugins}));
},
[CommandType.MenuUser] : (mod, plugins) => {
Files.ApplicationCommandStore[2].set ( name, {mod, plugins} );

View File

@@ -6,7 +6,7 @@ import { CommandType } from '../sern';
import type { PluggedModule } from '../structures/modules/module';
// A little ambigious, but ChatInput map stores text commands also.
export const BothCommand = new Map<string, PluggedModule>();
export const ApplicationCommandStore = {
[ApplicationCommandType.User] : new Map<string, PluggedModule>(),
[ApplicationCommandType.Message] : new Map<string, PluggedModule>(),
@@ -18,9 +18,9 @@ export const MessageCompCommandStore = {
[ComponentType.SelectMenu] : new Map<string, PluggedModule>()
}
export const TextCommandStore = {
[CommandType.Text] : new Map<string, PluggedModule>() // Aliases
text : new Map<string, PluggedModule>(),
aliases : new Map<string, PluggedModule>()
}
export const Alias = new Map<string, PluggedModule>();
// Courtesy @Townsy45
function readPath(dir: string, arrayOfFiles: string[] = []): string[] {