mirror of
https://github.com/sern-handler/handler
synced 2026-06-26 09:42:15 +00:00
refactoring handler types, added ctx handler class
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
16
src/handler/utils/ctxHandler.ts
Normal file
16
src/handler/utils/ctxHandler.ts
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -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 => {
|
||||
|
||||
@@ -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 };
|
||||
@@ -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> = {
|
||||
Reference in New Issue
Block a user