mirror of
https://github.com/sern-handler/handler
synced 2026-06-06 01:16:55 +00:00
Fix bugs in sern.ts & improve
This commit is contained in:
@@ -2,10 +2,10 @@ import * as Files from './utils/readFile'
|
||||
import type * as Utils from './utils/preprocessors/args';
|
||||
|
||||
import type {
|
||||
Arg,
|
||||
Context,
|
||||
Visibility,
|
||||
possibleOutput
|
||||
Arg,
|
||||
Context,
|
||||
Visibility,
|
||||
possibleOutput
|
||||
} from '../types/handler';
|
||||
|
||||
import type {
|
||||
@@ -26,31 +26,31 @@ import { isBot, hasPrefix, fmt } from './utils/messageHelpers';
|
||||
|
||||
export class Handler {
|
||||
private wrapper: Wrapper;
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @param {Wrapper} wrapper Some data that is required to run sern handler
|
||||
*/
|
||||
|
||||
constructor(
|
||||
wrapper: Wrapper,
|
||||
) {
|
||||
this.wrapper = wrapper;
|
||||
|
||||
this.client
|
||||
|
||||
/**
|
||||
* On ready, builds command data and registers them all
|
||||
* from command directory
|
||||
**/
|
||||
|
||||
.on('ready', async () => {
|
||||
Files.buildData(this)
|
||||
.then(data => this.registerModules(data))
|
||||
if (wrapper.init !== undefined) wrapper.init(this);
|
||||
})
|
||||
|
||||
.on('messageCreate', async (message: {
|
||||
channel: {
|
||||
type: string; send: (arg0: string) => void;
|
||||
};
|
||||
}) => {
|
||||
.on('messageCreate', async (message: any) => {
|
||||
if (isBot(message) || !hasPrefix(message, this.prefix)) return;
|
||||
if (message.channel.type === 'DM') return; // TODO: Handle dms
|
||||
|
||||
@@ -68,9 +68,7 @@ export class Handler {
|
||||
|
||||
})
|
||||
|
||||
.on('interactionCreate', async (interaction: {
|
||||
isCommand: () => boolean; commandName: string; reply: (arg0: any) => any;
|
||||
}) => {
|
||||
.on('interactionCreate', async (interaction) => {
|
||||
if (!interaction.isCommand()) return;
|
||||
const module = Files.Commands.get(interaction.commandName);
|
||||
const res = await this.interactionResult(module, interaction);
|
||||
@@ -81,9 +79,9 @@ export class Handler {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Files.CommandVal | undefined} module command file information
|
||||
* @param {CommandInteraction} interaction a Discord.js command interaction
|
||||
* @returns {possibleOutput | undefined} takes return value and replies it, if possible input
|
||||
* @param {Files.CommandVal | undefined} module Command file information
|
||||
* @param {CommandInteraction} interaction The Discord.js command interaction (DiscordJS#CommandInteraction))
|
||||
* @returns {possibleOutput | undefined} Takes return value and replies it, if possible input
|
||||
*/
|
||||
|
||||
private async interactionResult(
|
||||
@@ -95,18 +93,21 @@ 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 parsedArgs = module.mod.parse?.(context, ['slash', interaction.options]) ?? Ok('');
|
||||
|
||||
if (parsedArgs.err) return parsedArgs.val;
|
||||
|
||||
return (await module.mod.delegate(context, parsedArgs))?.val;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Files.CommandVal | undefined} module command file information
|
||||
* @param {Message} message a message object
|
||||
* @param {string} args anything after the command
|
||||
* @returns takes return value and replies it, if possible input
|
||||
* @param {Files.CommandVal | undefined} module Command file information
|
||||
* @param {Message} message The message object
|
||||
* @param {string} args Anything after the command
|
||||
* @returns Takes return value and replies it, if possible input
|
||||
*/
|
||||
|
||||
private async commandResult(module: Files.CommandVal | undefined, message: Message, args: string): Promise<possibleOutput | undefined> {
|
||||
@@ -220,7 +221,7 @@ export class Handler {
|
||||
|
||||
/**
|
||||
* @readonly
|
||||
* @returns {Client<boolean>} the DiscordJS.Client();
|
||||
* @returns {Client<boolean>} the discord.js client (DiscordJS#Client));
|
||||
*/
|
||||
|
||||
get client(): Client<boolean> {
|
||||
@@ -278,9 +279,6 @@ export interface Module<T = string> {
|
||||
*/
|
||||
|
||||
export enum CommandType {
|
||||
TEXT = 1,
|
||||
SLASH = 2,
|
||||
TEXT = 1,
|
||||
SLASH = 2,
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ export type ArgType<T> = Result<T, possibleOutput>;
|
||||
|
||||
export function parseInt(arg: string, onFailure: possibleOutput): ArgType<number> {
|
||||
const val = Number.parseInt(arg);
|
||||
|
||||
return val === NaN ? Err(onFailure) : Ok(val);
|
||||
}
|
||||
|
||||
@@ -30,7 +31,13 @@ export function parseInt(arg: string, onFailure: possibleOutput): ArgType<number
|
||||
export function parseBool(
|
||||
arg: string,
|
||||
onFailure: possibleOutput = `Cannot parse '${arg}' as a boolean`,
|
||||
regexes: { yesRegex: RegExp, noRegex: RegExp } = { yesRegex: /^(?:y(?:es)?|👍)$/i, noRegex: /^(?:n(?:o)?|👎)$/i }
|
||||
regexes: {
|
||||
yesRegex: RegExp,
|
||||
noRegex: RegExp
|
||||
} = {
|
||||
yesRegex: /^(?:y(?:es)?|👍)$/i,
|
||||
noRegex: /^(?:n(?:o)?|👎)$/i
|
||||
}
|
||||
): ArgType<boolean> {
|
||||
if (arg.match(regexes.yesRegex)) return Ok(true);
|
||||
if (arg.match(regexes.noRegex)) return Ok(false);
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
import type { ApplicationCommandOptionData } from 'discord.js';
|
||||
import { readdirSync, statSync } from 'fs';
|
||||
import { basename, join } from 'path';
|
||||
|
||||
import {
|
||||
readdirSync,
|
||||
statSync
|
||||
} from 'fs';
|
||||
|
||||
import {
|
||||
basename,
|
||||
join
|
||||
} from 'path';
|
||||
|
||||
import type * as Sern from '../sern';
|
||||
|
||||
@@ -17,11 +25,10 @@ async function readPath(dir: string, arrayOfFiles: string[] = []): Promise<strin
|
||||
try {
|
||||
const files = readdirSync(dir);
|
||||
for (const file of files) {
|
||||
if (statSync(dir + '/' + file).isDirectory()) {
|
||||
if (statSync(dir + '/' + file).isDirectory())
|
||||
await readPath(dir + '/' + file, arrayOfFiles)
|
||||
} else {
|
||||
else
|
||||
arrayOfFiles.push(join(dir, '/', file));
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
throw err;
|
||||
@@ -38,10 +45,9 @@ export const fmtFileName = (n: string) => {
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Sern.Handler} handler an instance of Sern.Handler
|
||||
* @returns {Promise<{ name: string; mod: Sern.Module<unknown>; absPath: string; }[]>} data from command files
|
||||
*/
|
||||
*/
|
||||
|
||||
export async function buildData(handler: Sern.Handler)
|
||||
: Promise<{
|
||||
|
||||
Reference in New Issue
Block a user