mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
refactor: add asyncResolveArray.ts to resolve Awaitables easier
This commit is contained in:
@@ -21,7 +21,6 @@ import {
|
||||
isMessageComponent,
|
||||
isMessageCtxMenuCmd,
|
||||
isModalSubmit,
|
||||
isPromise,
|
||||
isSelectMenu,
|
||||
isUserContextMenuCmd,
|
||||
} from '../utilities/predicates';
|
||||
@@ -29,6 +28,7 @@ import { filterCorrectModule } from './observableHandling';
|
||||
import { CommandType } from '../structures/enums';
|
||||
import type { Result } from 'ts-results';
|
||||
import type { AutocompleteInteraction } from 'discord.js';
|
||||
import { asyncResolveArray } from '../utilities/asyncResolveArray';
|
||||
|
||||
function applicationCommandHandler(mod: Module | undefined, interaction: CommandInteraction) {
|
||||
const mod$ = <T extends CommandType>(cmdTy: T) => of(mod).pipe(filterCorrectModule(cmdTy));
|
||||
@@ -211,13 +211,7 @@ export function onInteractionCreate(wrapper: Wrapper) {
|
||||
)
|
||||
.subscribe({
|
||||
async next({ mod, res: eventPluginRes, execute }) {
|
||||
const ePlugArr: Result<void, void>[] = [];
|
||||
for await (const res of eventPluginRes) {
|
||||
if (isPromise(res)) {
|
||||
ePlugArr.push(res);
|
||||
}
|
||||
ePlugArr.push(res as Awaited<Result<void, void>>);
|
||||
}
|
||||
const ePlugArr: Result<void, void>[] = await asyncResolveArray(eventPluginRes);
|
||||
if (ePlugArr.every(e => e.ok)) {
|
||||
await execute();
|
||||
wrapper.sernEmitter?.emit('module.activate', { type: 'success', module: mod! });
|
||||
|
||||
@@ -8,6 +8,7 @@ import * as Files from '../utilities/readFile';
|
||||
import { filterCorrectModule, ignoreNonBot } from './observableHandling';
|
||||
import { CommandType } from '../structures/enums';
|
||||
import { SernError } from '../structures/errors';
|
||||
import { asyncResolveArray } from '../utilities/asyncResolveArray';
|
||||
|
||||
export const onMessageCreate = (wrapper: Wrapper) => {
|
||||
const { client, defaultPrefix } = wrapper;
|
||||
@@ -40,7 +41,7 @@ export const onMessageCreate = (wrapper: Wrapper) => {
|
||||
|
||||
const processEventPlugins$ = ensureModuleType$.pipe(
|
||||
concatMap(({ ctx, args, mod }) => {
|
||||
const res = Promise.all(
|
||||
const res = asyncResolveArray(
|
||||
mod.onEvent.map(ePlug => {
|
||||
return ePlug.execute([ctx, args], controller);
|
||||
}),
|
||||
|
||||
9
src/handler/utilities/asyncResolveArray.ts
Normal file
9
src/handler/utilities/asyncResolveArray.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import type { Awaitable } from 'discord.js';
|
||||
|
||||
export async function asyncResolveArray<T>(promiseLike: Awaitable<T>[]): Promise<T[]> {
|
||||
const arr: T[] = [];
|
||||
for await (const el of promiseLike) {
|
||||
arr.push(el);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
Reference in New Issue
Block a user