From 36831def0fc7bfc96edb0ae0e21afefd5dddc414 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Mon, 7 Mar 2022 23:49:38 -0600 Subject: [PATCH] chore(module.ts) Move Command Modules to different file, change context objs to class counterparts --- src/handler/context.ts | 12 ++++++----- src/handler/module.ts | 28 ++++++++++++++++++++++++++ src/handler/sern.ts | 33 ++++--------------------------- src/handler/utilities/markup.ts | 12 +++++------ src/handler/utilities/readFile.ts | 9 +++++---- src/types/handler.ts | 12 ++--------- 6 files changed, 52 insertions(+), 54 deletions(-) create mode 100644 src/handler/module.ts diff --git a/src/handler/context.ts b/src/handler/context.ts index 170b050..3de364d 100644 --- a/src/handler/context.ts +++ b/src/handler/context.ts @@ -1,17 +1,18 @@ import type { - CommandInteraction, + Interaction, Message } from 'discord.js'; import { None, Option } from 'ts-results'; -export class Context { +export default class Context { private msg: Option = None; - private interac: Option = None; + private interac: Option = None; - constructor(message : Option, interaction: Option ) { + constructor(message : Option, interaction: Option ) { this.msg = message; - this.interac = interaction + this.interac = interaction; } + get messageUnchecked() { return this.msg.unwrap(); } @@ -25,3 +26,4 @@ export class Context { return this.interac; } } + diff --git a/src/handler/module.ts b/src/handler/module.ts new file mode 100644 index 0000000..9671c0c --- /dev/null +++ b/src/handler/module.ts @@ -0,0 +1,28 @@ +import type { Visibility, 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; + visibility: Visibility; + type: CommandType; + test: boolean; + execute: (eventParams: Context, args: Ok) => Awaitable; + parse?: (ctx: Context, args: Arg) => Utils.ArgType; +} + +export default Module; diff --git a/src/handler/sern.ts b/src/handler/sern.ts index da9924a..c516287 100644 --- a/src/handler/sern.ts +++ b/src/handler/sern.ts @@ -1,16 +1,11 @@ import * as Files from './utilities/readFile'; -import type * as Utils from './utilities/preprocessors/args'; import type { possibleOutput, - Visibility, - Context, - Arg } from '../types/handler'; import type { ApplicationCommandOptionData, - Awaitable, Client, CommandInteraction, Message @@ -20,7 +15,8 @@ import { Ok, None, Some } from 'ts-results'; import { isNotFromBot, hasPrefix, fmt } from './utilities/messageHelpers'; import Logger, { sEvent } from './logger'; import { AllTrue } from './utilities/higherOrders'; - +import type Module from './module'; +import Context from './context'; /** * @class */ @@ -101,7 +97,7 @@ export class Handler { if (name === undefined) return `Could not find ${interaction.commandName} command!`; if (module.mod.type < CommandType.SLASH) return 'This is not a slash command'; - const context = { message: None, interaction: Some(interaction) }; + const context = new Context(None, Some(interaction)); const parsedArgs = module.mod.parse?.(context, ['slash', interaction.options]) ?? Ok(''); if (parsedArgs.err) return parsedArgs.val; @@ -148,10 +144,7 @@ export class Handler { return; } } - const context = { - message: Some(message), - interaction: None, - }; + const context = new Context ( Some(message), None ); const args = message.content.slice(this.prefix.length).trim().split(/s+/g); const parsedArgs = module.mod.parse?.(context, ['text', args]) ?? Ok(args); if (parsedArgs.err) return parsedArgs.val; @@ -296,25 +289,7 @@ export interface Wrapper { readonly privateServers: { test: boolean; id: string }[]; } -/** - * 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 - */ -export interface Module { - alias: string[]; - desc: string; - visibility: Visibility; - type: CommandType; - test: boolean; - execute: (eventParams: Context, args: Ok) => Awaitable; - parse?: (ctx: Context, args: Arg) => Utils.ArgType; -} /** * @enum { number }; diff --git a/src/handler/utilities/markup.ts b/src/handler/utilities/markup.ts index 3f9017e..4dba827 100644 --- a/src/handler/utilities/markup.ts +++ b/src/handler/utilities/markup.ts @@ -212,7 +212,7 @@ function toTimeString( unix: bigint | number, units: Record, - isFromNow: boolean = false, + isFromNow = false, limit?: number ) { if (typeof unix === 'number') unix = BigInt(unix); @@ -255,7 +255,7 @@ const date = new Date(unix); const timestamp = formatDate(date); let ret = FrozenTimestampStyles[style]; - for (let [key, value] of Object.entries(timestamp)) { + for (const [key, value] of Object.entries(timestamp)) { ret = ret.split(`{${key}}`).join(value); } return ret; @@ -361,7 +361,7 @@ static timestamp( unix: number | Date | string, format: TimestampStyles = TimestampStyles.BOTH_SHORT, - isSeconds: boolean = false + isSeconds = false ) { if (typeof unix === 'string') unix = Number(unix); if (unix instanceof Date) unix = unix.getTime(); @@ -375,7 +375,7 @@ static date( unix: number | Date | string, format: TimestampStyles = TimestampStyles.BOTH_SHORT, - isSeconds: boolean = false + isSeconds = false ) { if (typeof unix === 'string') unix = Number(unix); if (unix instanceof Date) unix = unix.getTime(); @@ -592,7 +592,7 @@ match( type: DiscordRegexNames, - onlyFirst: boolean = false + onlyFirst = false ): DiscordRegexPayload { const regex = DiscordRegex[type]; if (regex === undefined) { @@ -698,7 +698,7 @@ static match( raw: string, what: DiscordRegexNames, - onlyFirst: boolean = false + onlyFirst = false ) { return new this(raw).match(what, onlyFirst); } diff --git a/src/handler/utilities/readFile.ts b/src/handler/utilities/readFile.ts index 319f8c9..6287a6d 100644 --- a/src/handler/utilities/readFile.ts +++ b/src/handler/utilities/readFile.ts @@ -1,11 +1,12 @@ import type { ApplicationCommandOptionData } from 'discord.js'; import type * as Sern from '../sern'; +import type Module from '../module'; import { readdirSync, statSync } from 'fs'; import { basename, join } from 'path'; export type CommandVal = { - mod: Sern.Module & { name : string }; + mod: Module & { name : string }; options: ApplicationCommandOptionData[]; }; @@ -32,20 +33,20 @@ export const fmtFileName = (n: string) => n.substring(0, n.length - 3); /** * * @param {Sern.Handler} handler an instance of Sern.Handler - * @returns {Promise<{ name: string; mod: Sern.Module; absPath: string; }[]>} data from command files + * @returns {Promise<{ name: string; mod: Module; absPath: string; }[]>} data from command files */ export async function buildData(handler: Sern.Handler): Promise< { name: string; - mod: Sern.Module; + mod: Module; absPath: string; }[] > { const commandDir = handler.commandDir; return Promise.all( (await getCommands(commandDir)).map(async (absPath) => { - return { name: basename(absPath), mod: (await import(absPath)).default as Sern.Module, absPath }; + return { name: basename(absPath), mod: (await import(absPath)).default as Module, absPath }; }), ); } diff --git a/src/types/handler.ts b/src/types/handler.ts index bc6bf79..82f38d0 100644 --- a/src/types/handler.ts +++ b/src/types/handler.ts @@ -1,30 +1,22 @@ -import type { Option } from 'ts-results'; import type { - CommandInteraction, CommandInteractionOptionResolver, - Message, MessagePayload, MessageOptions, } from 'discord.js'; -import type * as Sern from '../handler/sern'; +import type Module from '../handler/module'; export type Visibility = 'private' | 'public'; // Anything that can be sent in a `#send` or `#reply` export type possibleOutput = T | (MessagePayload & MessageOptions); -export type execute = Sern.Module['execute']; +export type execute = Module['execute']; // Thanks @cursorsdottsx export type ParseType = { [K in keyof T]: T[K] extends unknown ? [k: K, args: T[K]] : never; }[keyof T]; -// A Sern.Module['delegate'] will carry a Context Parameter -export type Context = { - message: Option; - interaction: Option; -}; export type Arg = ParseType<{ text: string[]; slash: SlashOptions }>;