refactoring handler types, added ctx handler class

This commit is contained in:
jacoobes
2022-02-06 23:17:57 -06:00
parent c78741f565
commit f15bb13204
5 changed files with 37 additions and 45 deletions

View File

@@ -1,10 +1,10 @@
import type { Arg, Context, MessagePackage, Nullable, ParseType, Visibility } from "../../types/handler/handler";
import { CommandType } from "../../types/handler/handler";
import { Files } from "../utils/readFile"
import type { Arg, Context, Nullable, ParseType, Visibility } from "../types/handler/handler";
import { Files } from "./utils/readFile"
import type { ApplicationCommandOptionData, Awaitable, Client, CommandInteraction, CommandInteractionOptionResolver, Message} from "discord.js";
import type { possibleOutput } from "../../types/handler/handler"
import type { possibleOutput } from "../types/handler/handler"
import { Err, Ok, Result, Option, None, Some } from "ts-results";
import type { Utils } from "../utils/preprocessors/args";
import type { Utils } from "./utils/preprocessors/args";
import { CtxHandler } from "./utils/ctxHandler";
@@ -18,7 +18,7 @@ export namespace Sern {
this.wrapper = wrapper;
this.wrapper.client
.on("ready", async () => {
if (this.wrapper.init !== undefined) this.wrapper.init();
if (this.wrapper.init !== undefined) this.wrapper.init(this);
await Files.registerModules(this);
@@ -116,13 +116,10 @@ export namespace Sern {
readonly client : Client,
readonly prefix: string,
readonly commands : string
init? : () => void,
init? : (handler : Sern.Handler) => void,
readonly privateServerId : string
}
export interface Module<T> {
export interface Module<T = string> {
alias: string[],
desc : string,
visibility : Visibility,
@@ -131,20 +128,10 @@ export namespace Sern {
parse? : (ctx: Context, args: ParseType<Arg> ) => Utils.ArgType<T>
}
export enum CommandType {
TEXT = 2,
SLASH = 4,
}
}
class CtxHandler {
static isBot(message: Message) {
return message.author.bot;
}
static hasPrefix(message: Message, prefix: string) {
return (message.content.slice(0, prefix.length).toLowerCase().trim()) === prefix;
}
static fmt(msg: Message, prefix: string) : string[] {
return msg.content.slice(prefix.length).trim().split(/\s+/g)
}
}

View File

@@ -0,0 +1,16 @@
import type { Message } from "discord.js";
export class CtxHandler {
static isBot(message: Message) {
return message.author.bot;
}
static hasPrefix(message: Message, prefix: string) {
return (message.content.slice(0, prefix.length).toLowerCase().trim()) === prefix;
}
static fmt(msg: Message, prefix: string) : string[] {
return msg.content.slice(prefix.length).trim().split(/\s+/g)
}
}

View File

@@ -1,7 +1,7 @@
import type { ApplicationCommandOptionData, CommandInteractionOption } from "discord.js";
import { readdirSync, statSync } from "fs";
import { basename, join } from "path";
import type { Sern } from "../sern/sern";
import type { Sern } from "../sern";
export namespace Files {
export const Commands = new Map<string, { mod: Sern.Module<unknown>, options: ApplicationCommandOptionData[] }>();
@@ -26,8 +26,7 @@ export namespace Files {
}
export async function registerModules(handler : Sern.Handler) : Promise<void> {
const commandDir = handler.commandDir,
client = handler.client;
const commandDir = handler.commandDir;
Promise.all((await getCommands(commandDir)).map(async absPath => {
return { name : basename(absPath), mod: ( await import(absPath)).default as Sern.Module<unknown>, absPath }
})).then( async modArr => {

View File

@@ -1,5 +1,5 @@
import { Sern } from "./handler/sern/sern";
import { Sern } from "./handler/sern";
import { Utils } from "./handler/utils/preprocessors/args"
module.exports = { Sern, Utils };
export { Sern, Utils };
import * as Types from "./types/handler/handler"
module.exports = { Sern, Utils, Types };
export { Sern, Utils, Types };

View File

@@ -1,24 +1,14 @@
import type { Ok, Result, Option } from 'ts-results';
import type { Awaitable, Client, CommandInteraction, CommandInteractionOptionResolver, Message, MessagePayload} from 'discord.js';
import type { Option } from 'ts-results';
import type { CommandInteraction, CommandInteractionOptionResolver, Message, MessagePayload} from 'discord.js';
import type { MessageOptions } from 'child_process';
import type { Sern } from '../../handler/sern/sern';
import type { Sern } from '../../handler/sern';
export type Visibility = "private" | "public"
export type possibleOutput = string | MessagePayload & MessageOptions
export type Nullable<T> = T | null;
export type MessagePackage = {
message : Option<Message>,
interaction : Option<CommandInteraction>,
prefix : string
}
export type delegate = Sern.Module<unknown>["delegate"]
export enum CommandType {
TEXT = 2,
SLASH = 4,
}
/// Thanks @cursorsdottsx
export type ParseType<T> = {