Skip to content

Commit

Permalink
fix: Standard for of does not resolve promises. Switched to for await
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoobes committed May 19, 2022
1 parent b8cda35 commit 66b9f51
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions src/handler/events/interactionCreate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import type { CommandInteraction, Interaction, MessageComponentInteraction, SelectMenuInteraction } from 'discord.js';
import type {
Awaitable,
CommandInteraction,
Interaction,
MessageComponentInteraction,
SelectMenuInteraction,
} from 'discord.js';
import { concatMap, fromEvent, map, Observable, of, throwError } from 'rxjs';
import type Wrapper from '../structures/wrapper';
import * as Files from '../utilities/readFile';
Expand All @@ -10,12 +16,13 @@ import type { Module } from '../structures/module';
import {
isButton,
isChatInputCommand,
isMessageCtxMenuCmd,
isMessageCtxMenuCmd, isPromise,
isSelectMenu,
isUserContextMenuCmd,
} from '../utilities/predicates';
import { filterCorrectModule } from './observableHandling';
import { CommandType } from '../structures/enums';
import type { Result } from 'ts-results';

function applicationCommandHandler(mod: Module | undefined, interaction: CommandInteraction) {
const mod$ = <T extends CommandType>(cmdTy : T) => of(mod).pipe(
Expand All @@ -31,7 +38,7 @@ function applicationCommandHandler(mod: Module | undefined, interaction: Command
return of(m.onEvent?.map(e => e.execute(
[ctx, ['slash', i.options]],
controller
)) ?? []).pipe(map(res => ({ m, res, execute() { return m.execute(ctx, ['slash', i.options]); } }) ));
)) ?? []).pipe(map(res => ({ mod, res, execute() { return m.execute(ctx, ['slash', i.options]); } }) ));
}),
);
},
Expand All @@ -44,7 +51,7 @@ function applicationCommandHandler(mod: Module | undefined, interaction: Command
return of(m.onEvent?.map(e => e.execute(
[ctx],
controller
)) ?? []).pipe(map(res => ({ m, res, execute() { return m.execute(ctx); } }) ));
)) ?? []).pipe(map(res => ({ mod, res, execute() { return m.execute(ctx); } }) ));
}),
);
},
Expand All @@ -55,7 +62,7 @@ function applicationCommandHandler(mod: Module | undefined, interaction: Command
return of(m.onEvent?.map(e => e.execute(
[ctx],
controller
)) ?? []).pipe(map(res => ({ m, res, execute() { return m.execute(ctx); } }) ));
)) ?? []).pipe(map(res => ({ mod, res, execute() { return m.execute(ctx); } }) ));
}),
);
})
Expand All @@ -77,7 +84,7 @@ function messageComponentInteractionHandler(
return of(m.onEvent?.map(e => e.execute(
[ctx],
controller
)) ?? []).pipe(map(res => ({ m, res, execute() { return m.execute(ctx); } }) ));
)) ?? []).pipe(map(res => ({ mod, res, execute() { return m.execute(ctx); } }) ));
}),
);
})
Expand All @@ -87,7 +94,7 @@ function messageComponentInteractionHandler(
return of(m.onEvent?.map(e => e.execute(
[ctx],
controller
)) ?? []).pipe(map(res => ({ m, res, execute() { return m.execute(ctx); } }) ));
)) ?? []).pipe(map(res => ({ mod, res, execute() { return m.execute(ctx); } }) ));
}),
);
})
Expand Down Expand Up @@ -117,13 +124,23 @@ export function onInteractionCreate (wrapper: Wrapper) {
} else return throwError(() => SernError.NotSupportedInteraction);
}),
).subscribe({
next({m, res, execute}) {

},
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>>);
}
if(ePlugArr.every(e => e.ok)) {
await execute();
} else {
console.log(ePlugArr);
console.log(mod, 'failed');
}
},
error(err) {
console.log(err);
}
});


}
}

0 comments on commit 66b9f51

Please sign in to comment.