From 5b968ce44222dfd31c8f75fa6a524a0ccd4994aa Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Sun, 13 Mar 2022 01:37:24 -0600 Subject: [PATCH] feat(handler) create command-options interface, remove module --- src/handler/events/messageEvent.ts | 2 +- src/handler/events/readyEvent.ts | 6 ++--- src/handler/sern.ts | 21 ++------------- .../structures/commands/command-options.ts | 21 +++++++++++++++ src/handler/structures/commands/command.ts | 16 +++++------- src/handler/structures/module.ts | 26 ------------------- 6 files changed, 34 insertions(+), 58 deletions(-) create mode 100644 src/handler/structures/commands/command-options.ts delete mode 100644 src/handler/structures/module.ts diff --git a/src/handler/events/messageEvent.ts b/src/handler/events/messageEvent.ts index af2ff99..b623148 100644 --- a/src/handler/events/messageEvent.ts +++ b/src/handler/events/messageEvent.ts @@ -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"; diff --git a/src/handler/events/readyEvent.ts b/src/handler/events/readyEvent.ts index 92e942c..ec2cb21 100644 --- a/src/handler/events/readyEvent.ts +++ b/src/handler/events/readyEvent.ts @@ -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 }); } diff --git a/src/handler/sern.ts b/src/handler/sern.ts index c4cef72..7434c9a 100644 --- a/src/handler/sern.ts +++ b/src/handler/sern.ts @@ -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); diff --git a/src/handler/structures/commands/command-options.ts b/src/handler/structures/commands/command-options.ts new file mode 100644 index 0000000..cb8ade8 --- /dev/null +++ b/src/handler/structures/commands/command-options.ts @@ -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 + * @property {string} desc + * @property {CommandType} type + * @property {(eventParams : Context, args : Ok Awaitable)} execute + * @prop {(ctx: Context, args: Arg) => Utils.ArgType} parse + */ + +interface CommandOptions { + commandType : CommandType, + alias : string[] | [], + options?: ApplicationCommandOptionData[], + name? : string | undefined + +} + +export default CommandOptions; diff --git a/src/handler/structures/commands/command.ts b/src/handler/structures/commands/command.ts index ec557cc..ccb461b 100644 --- a/src/handler/structures/commands/command.ts +++ b/src/handler/structures/commands/command.ts @@ -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 ( args: Ok ) : Awaitable; abstract parse? ( args: Arg ) : Utils.ArgType; - 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 } } diff --git a/src/handler/structures/module.ts b/src/handler/structures/module.ts deleted file mode 100644 index f387b31..0000000 --- a/src/handler/structures/module.ts +++ /dev/null @@ -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 - * @property {string} desc - * @property {Visibility} visibility - * @property {CommandType} type - * @property {(eventParams : Context, args : Ok Awaitable)} execute - * @prop {(ctx: Context, args: Arg) => Utils.ArgType} parse - */ - -interface Module { - alias: string[]; - desc: string; - type: CommandType; - execute: (eventParams: Context, args: Ok) => Awaitable; - parse?: (ctx: Context, args: Arg) => Utils.ArgType; -} - -export default Module;