feat(handler) create command-options interface, remove module

This commit is contained in:
Jacob Nguyen
2022-03-13 01:37:24 -06:00
parent 840619d7c5
commit 5b968ce442
6 changed files with 34 additions and 58 deletions

View File

@@ -1,5 +1,5 @@
import type { Message } from "discord.js";
import { filter, fromEvent, map, Observable } from "rxjs";
import { filter, fromEvent, Observable } from "rxjs";
import type Wrapper from "../structures/wrapper";
import { isNotFromDM, isNotFromBot, hasPrefix } from "../utilities/messageHelpers";

View File

@@ -21,10 +21,10 @@ export const onReady = ( wrapper : Wrapper ) => {
}
function setCommands ( { mod, absPath } : { mod : Command, absPath : string } ) {
const options = mod.options ?? [] as ApplicationCommandOptionData[];
const name = mod.name ?? Files.fmtFileName(basename(absPath));
const options = mod.getOptions() ?? [] as ApplicationCommandOptionData[];
const name = mod.getName() ?? Files.fmtFileName(basename(absPath));
mod.alias?.forEach( n => Files.Alias.set( n, { mod, options } ));
mod.getAlias()?.forEach( n => Files.Alias.set( n, { mod, options } ));
Files.Commands.set(name, { mod, options });
}

View File

@@ -1,19 +1,12 @@
import type {
DiscordEvent,
possibleOutput,
} from '../types/handler';
import type {
ApplicationCommandOptionData,
Client,
CommandInteraction,
Message
} from 'discord.js';
import { Ok, None, Some } from 'ts-results';
import Logger, { sEvent } from './logger';
import type Module from './structures/module';
import Context from './structures/context';
import Logger from './logger';
import type Wrapper from './structures/wrapper';
import { fromEvent } from 'rxjs';
import { SernError } from './structures/errors';
@@ -27,6 +20,7 @@ export function init( wrapper : Wrapper) {
onReady( wrapper );
onMessageCreate( wrapper );
}
@@ -38,17 +32,6 @@ function eventObserver(client: Client, events: DiscordEvent[] ) {
}
export class Handler {
private wrapper: Wrapper;
private defaultLogger: Logger = new Logger();
/**
*
* @constructor
* @param {Wrapper} wrapper The data that is required to run sern handler
*/
constructor(wrapper: Wrapper) {
this.wrapper = wrapper;
}
/**
.on('messageCreate', async (message: Message) => {
const module = this.findModuleFrom(message);

View File

@@ -0,0 +1,21 @@
import type { ApplicationCommandOptionData } from 'discord.js';
import type { CommandType } from '../sern';
/**
* An object that gets imported and acts as a command.
* @typedef {object} Module<T=string>
* @property {string} desc
* @property {CommandType} type
* @property {(eventParams : Context, args : Ok<T=string) => Awaitable<possibleOutput | void>)} execute
* @prop {(ctx: Context, args: Arg) => Utils.ArgType<T>} parse
*/
interface CommandOptions {
commandType : CommandType,
alias : string[] | [],
options?: ApplicationCommandOptionData[],
name? : string | undefined
}
export default CommandOptions;

View File

@@ -5,6 +5,7 @@ import type * as Utils from '../../utilities/preprocessors/args';
import { None, Ok } from "ts-results";
import { CommandType } from "../../sern";
import { SernError } from "../errors";
import type CommandOptions from "./command-options";
export abstract class Command {
@@ -15,10 +16,7 @@ export abstract class Command {
protected _options : ApplicationCommandOptionData[] | undefined;
protected _alias : string[] | undefined;
protected constructor (
commandType : CommandType,
alias : string[] | [],
options?: ApplicationCommandOptionData[],
name? : string | undefined
{ name, commandType, options, alias } : CommandOptions
) {
this._name = name;
this._commandType = commandType;
@@ -42,9 +40,9 @@ export abstract class Command {
abstract execute<T> ( args: Ok<T> ) : Awaitable<possibleOutput | void>;
abstract parse?<T> ( args: Arg ) : Utils.ArgType<T>;
public set ctx ( context: Context ) { this._ctx = context; }
public get name () { return this._name; }
public get commandType () { return this._commandType; }
public get options() { return this._options; }
public get alias() { return this._alias }
public setCtx ( context: Context ) { this._ctx = context; }
public getName () { return this._name; }
public getCommandType () { return this._commandType; }
public getOptions() { return this._options; }
public getAlias() { return this._alias }
}

View File

@@ -1,26 +0,0 @@
import type { possibleOutput, Arg } from '../../types/handler';
import type { CommandType } from '../sern';
import type Context from './context' ;
import type { Awaitable } from 'discord.js';
import type { Ok } from 'ts-results';
import type * as Utils from '../utilities/preprocessors/args';
/**
* An object that gets imported and acts as a command.
* @typedef {object} Module<T=string>
* @property {string} desc
* @property {Visibility} visibility
* @property {CommandType} type
* @property {(eventParams : Context, args : Ok<T=string) => Awaitable<possibleOutput | void>)} execute
* @prop {(ctx: Context, args: Arg) => Utils.ArgType<T>} parse
*/
interface Module<T = string> {
alias: string[];
desc: string;
type: CommandType;
execute: (eventParams: Context, args: Ok<T>) => Awaitable<possibleOutput | void>;
parse?: (ctx: Context, args: Arg) => Utils.ArgType<T>;
}
export default Module;