mirror of
https://github.com/sern-handler/handler
synced 2026-06-22 15:52:16 +00:00
refactor: Adding more top level imports and move partition
This commit is contained in:
@@ -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) => {
|
||||
|
||||
@@ -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];
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ import type {
|
||||
ApplicationCommandOptionData,
|
||||
Awaitable,
|
||||
ButtonInteraction,
|
||||
ContextMenuCommandInteraction,
|
||||
MessageContextMenuCommandInteraction,
|
||||
SelectMenuInteraction,
|
||||
} from 'discord.js';
|
||||
|
||||
8
src/handler/utilities/partition.ts
Normal file
8
src/handler/utilities/partition.ts
Normal 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];
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
export * as Sern from './handler/sern';
|
||||
export * from './types/handler';
|
||||
export * from './handler/structures/structxports';
|
||||
export * from './handler/plugins/plugin';
|
||||
Reference in New Issue
Block a user