feat(handler) add basic interaction handling and add error for not

detecting modules
This commit is contained in:
Jacob Nguyen
2022-03-21 19:52:24 -05:00
parent 5baff09c03
commit d34c1881bd
3 changed files with 8 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
import type { Awaitable, ChatInputCommandInteraction, Interaction } from "discord.js";
import { map, filter, fromEvent, Observable, of, mergeMap, tap} from "rxjs";
import { map, filter, fromEvent, Observable, of, mergeMap, tap, concatMap} from "rxjs";
import { None, Some } from "ts-results";
import { CommandType } from "../sern";
import Context from "../structures/context";
@@ -13,7 +13,7 @@ export const onInteractionCreate = ( wrapper : Wrapper ) => {
(fromEvent(client, 'interactionCreate') as Observable<Interaction>)
.pipe(
mergeMap ( interaction => {
concatMap ( interaction => {
if (interaction.isChatInputCommand()) {
return of(interaction.commandName).pipe(
map ( Files.Commands.get ),

View File

@@ -1,5 +1,6 @@
export enum SernError {
RESERVED_EVENT = 'Cannot register the reserved ready event. Please use the init property.',
NO_ALIAS = 'You cannot provide an array with elements to a slash command.',
NOT_VALID_MOD_TYPE = 'Detected an unknown module type'
NOT_VALID_MOD_TYPE = 'Detected an unknown module type',
UNDEFINED_MODULE = `A module could not be detected at`
}

View File

@@ -3,6 +3,7 @@ import type { ApplicationCommandOptionData } from 'discord.js';
import { readdirSync, statSync } from 'fs';
import { join } from 'path';
import type { Module } from '../structures/commands/module';
import { SernError } from '../structures/errors';
export const Commands = new Map<string, Module>();
@@ -39,7 +40,9 @@ export async function buildData(commandDir: string ): Promise<
> {
return Promise.all(
getCommands(commandDir).map( async (absPath) => {
return { mod: (await import(absPath)).module as Module, absPath };
const mod = (await import(absPath)).module as Module;
if (mod === undefined) throw Error(`${SernError.UNDEFINED_MODULE} ${absPath}`)
return { mod, absPath };
}),
);
}