refactor: Adding more top level imports and move partition

This commit is contained in:
Jacob Nguyen
2022-05-15 01:03:57 -05:00
parent 86457a78ff
commit 61fe8534b5
7 changed files with 16 additions and 16 deletions

View File

@@ -23,15 +23,15 @@ import type { UnionToTuple } from '../utilities/resolveParameters';
function isChatInputCommand(i : CommandInteraction) : i is ChatInputCommandInteraction {
return i.isChatInputCommand();
}
function applicationCommandHandler(plugged: PluggedModule | undefined, interaction: CommandInteraction) {
function applicationCommandHandler(plugged: PluggedModule| undefined, interaction: CommandInteraction) {
if (plugged === undefined) {
return throwError(() => SernError.UndefinedModule);
}
const eventPlugins = plugged.plugins.filter(isEventPlugin);
return match(interaction)
.when(isChatInputCommand, i => {
const ctx = Context.wrap(i);
const res = eventPlugins.map(e => {
const ctx = Context.wrap(i);
const res = eventPlugins.map(e => {
return e.execute(
[ctx, <Args>['slash', i.options]]
, controller);
@@ -73,7 +73,7 @@ function messageComponentInteractionHandler(
}) as Awaited<Result<void, void>>[];
return of({ type : plugged.mod.type, res, plugged, ctx });
})
.otherwise(_ => throwError( () => SernError.NotSupportedInteraction) );
.otherwise(() => throwError( () => SernError.NotSupportedInteraction) );
}
export const onInteractionCreate = (wrapper: Wrapper) => {

View File

@@ -81,11 +81,3 @@ export function ignoreNonBot(prefix: string) {
});
}
export function partition<T, U extends T>(condition: (el: T) => el is U, array: T[]): [U[], T[]] {
const uArr: U[] = [];
const vArr: T[] = [];
for (const el of array) {
(condition(el) ? uArr : vArr).push(el);
}
return [uArr, vArr];
}

View File

@@ -10,7 +10,7 @@ import type {
} from '../structures/modules/commands/moduleHandler';
import { CommandType } from '../sern';
import { CommandPlugin, EventPlugin, PluginType, SernPlugin } from '../plugins/plugin';
import { partition } from './observableHandling';
import { partition } from '../utilities/partition';
import { Err, Ok, Result } from 'ts-results';
import type { PluggedModule } from '../structures/modules/module';
import type { Awaitable } from 'discord.js';
@@ -106,7 +106,7 @@ function isCmdPlugin(p: SernPlugin): p is CommandPlugin {
return (p.type & PluginType.Command) !== 0;
}
export function isEventPlugin(p: SernPlugin): p is EventPlugin {
export function isEventPlugin<T extends CommandType>(p: SernPlugin): p is EventPlugin {
return (p.type & PluginType.Event) !== 0;
}

View File

@@ -57,7 +57,7 @@ export type EventPlugin<T extends CommandType = CommandType> = {
export type SernPlugin = CommandPlugin | EventPlugin;
export function plugins<T extends CommandType, V extends EventPlugin<T> | CommandPlugin>(...plug: V[]) {
export function plugins(...plug: SernPlugin[]) {
return plug;
}

View File

@@ -2,7 +2,6 @@ import type {
ApplicationCommandOptionData,
Awaitable,
ButtonInteraction,
ContextMenuCommandInteraction,
MessageContextMenuCommandInteraction,
SelectMenuInteraction,
} from 'discord.js';

View File

@@ -0,0 +1,8 @@
export function partition<T, U extends T>(condition: (el: T) => el is U, array: T[]): [U[], T[]] {
const uArr: U[] = [];
const vArr: T[] = [];
for (const el of array) {
(condition(el) ? uArr : vArr).push(el);
}
return [uArr, vArr];
}

View File

@@ -1,3 +1,4 @@
export * as Sern from './handler/sern';
export * from './types/handler';
export * from './handler/structures/structxports';
export * from './handler/plugins/plugin';