refactor : modify module typings to override

This commit is contained in:
Jacob Nguyen
2022-03-22 23:19:15 -05:00
parent 44e6e58fee
commit 2191fda383
4 changed files with 22 additions and 17 deletions

View File

@@ -2,6 +2,7 @@ import type { Message } from "discord.js";
import { map, filter, fromEvent, Observable, of, concatMap, tap } from "rxjs";
import { None, Some } from "ts-results";
import { CommandType } from "../sern";
import type { TextCommand } from "../structures/commands/module";
import Context from "../structures/context";
import type Wrapper from "../structures/wrapper";
import { isNotFromDM, isNotFromBot, hasPrefix, fmt } from "../utilities/messageHelpers";
@@ -25,7 +26,7 @@ export const onMessageCreate = (wrapper : Wrapper) => {
),
filter( ([mod]) => mod !== undefined && (mod.type & CommandType.TEXT) != 0 ),
tap ( ([ mod, ctx, args ]) => {
mod!.execute(ctx, ['text', args])
(mod as TextCommand)!.execute(ctx, ['text', args])
}),
)
)

View File

@@ -1,30 +1,34 @@
import type { ApplicationCommandOptionData, Awaitable, Interaction } from "discord.js";
import type { Args } from "../../../types/handler";
import type { ApplicationCommandOptionData, Awaitable, ChatInputCommandInteraction, Interaction } from "discord.js";
import type { Args, Override } from "../../../types/handler";
import type { CommandType } from "../../sern";
import type Context from "../context";
type executeSlash = { execute : (ctx : Context<ChatInputCommandInteraction>, args: Args) => Awaitable<void> };
export interface BaseModule {
name? : string;
description : string;
execute: (ctx: Context<Interaction>, args: Args) => Awaitable<void>;
}
export type Text = {
export type TextCommand = {
type : CommandType.TEXT;
alias : string[] | [],
};
export type Slash = {
} & BaseModule;
export type SlashCommand = {
type : CommandType.SLASH;
options : ApplicationCommandOptionData[] | [],
};
} & Override<BaseModule, executeSlash>;
export type Both = {
export type BothCommand = {
type : CommandType.BOTH;
alias : string[] | [];
options : ApplicationCommandOptionData[] | [],
}
} & Override<BaseModule, executeSlash>;
export type Module =
(BaseModule & Slash) | (BaseModule & Both) | (BaseModule & Text);
TextCommand
| SlashCommand
| BothCommand;

View File

@@ -1,13 +1,13 @@
import { CommandType } from "../../sern";
import type { Text, Both, Slash, BaseModule } from "./module";
import type { TextCommand, BothCommand, SlashCommand, BaseModule } from "./module";
//https://stackoverflow.com/questions/64092736/alternative-to-switch-statement-for-typescript-discriminated-union
// Explicit Module Definitions for mapping
export type ModuleDefs = {
[CommandType.TEXT] : Text & BaseModule,
[CommandType.SLASH] : Slash & BaseModule,
[CommandType.BOTH] : Both & BaseModule,
[CommandType.TEXT] : TextCommand,
[CommandType.SLASH] : SlashCommand,
[CommandType.BOTH] : BothCommand,
}
//Keys of ModuleDefs

View File

@@ -11,7 +11,7 @@ function firstSome<T>(...args : Option<T>[]) : T | null {
return null;
}
export default class Context<I extends Interaction> {
export default class Context<I extends Interaction = Interaction> {
private msg: Option<Message> = None;
private interac: Option<I> = None;