diff --git a/README.md b/README.md index f4dd48f..3cc05e5 100644 --- a/README.md +++ b/README.md @@ -16,12 +16,12 @@ yarn add sern-handler # Basic Usage -[Typescript](https://www.typescriptlang.org/) +Typescript ```ts import { Client } from 'discord.js' import { Intents } from 'discord.js' -import {prefix, token} from "../src/secrets.json" -import {Sern} from "sern-handler" +import { prefix, token } from "../src/secrets.json" +import { Sern } from "sern-handler" const client = new Client({ intents: [ @@ -33,22 +33,53 @@ const client = new Client({ }) new Sern.Handler( { - client, - prefix, - commands : "dist/commands", - privateServers : [ + client, + prefix, + commands : 'dist/commands', + privateServers : [ { test : true, - id: "server id" + id: 'server-id' } ], init: async (handler : Sern.Handler) => { - /* an optional function to initialize anything else on bot startup */ + // Optional function to initialize anything else on bot startup + }, +}); +``` + +JavaScript +```js +import { Client, Intents } from 'discord.js'; +import { Handler } from 'sern-handler'; +import { prefix, token } from '../src/secrets.json'; + +const client = new Client({ + intents: [ + Intents.FLAGS.GUILDS, + Intents.FLAGS.GUILD_MESSAGES, + Intents.FLAGS.GUILD_MEMBERS + ] +}); + +// Access handler anywhere +client.handler = new Handler({ + client, + prefix, + commands : 'dist/commands', + privateServers : [ + { + test : true, + id: 'server-id' + } + ], + init: async (handler) => { + // Optional function to initialize anything else on bot startup }, }); -client.login(token) +client.login(token); ``` # Links diff --git a/src/handler/sern.ts b/src/handler/sern.ts index e8a8feb..21edfec 100644 --- a/src/handler/sern.ts +++ b/src/handler/sern.ts @@ -31,7 +31,8 @@ export class Handler { **/ .on('ready', async () => { - Files.buildData(this).then((data) => this.registerModules(data)); + Files.buildData(this) + .then(data => this.registerModules(data)); if (wrapper.init !== undefined) wrapper.init(this); new Logger().tableRam(); }) @@ -41,8 +42,7 @@ export class Handler { if (message.channel.type === 'DM') return; // TODO: Handle dms const tryFmt = fmt(message, this.prefix); - const commandName = tryFmt.shift()!; - const module = Files.Commands.get(commandName) ?? Files.Alias.get(commandName); + const module = this.findCommand(tryFmt.shift()!); if (module === undefined) { message.channel.send('Unknown legacy command'); return; @@ -74,11 +74,10 @@ export class Handler { interaction: CommandInteraction, ): Promise { if (module === undefined) return 'Unknown slash command!'; - const name = Array.from(Files.Commands.keys()).find((it) => it === interaction.commandName); + const name = this.findCommand(interaction.commandName); 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(''); @@ -172,6 +171,16 @@ export class Handler { } } + /** + * + * @param {string} name name of possible command + * @returns {Files.CommandVal | undefined} + */ + + private findCommand(name: string): Files.CommandVal | undefined { + return Files.Commands.get(name) ?? Files.Alias.get(name); + } + /** * * @param {string} cmdName name of command diff --git a/src/handler/utilities/readFile.ts b/src/handler/utilities/readFile.ts index 981e64b..f8d694b 100644 --- a/src/handler/utilities/readFile.ts +++ b/src/handler/utilities/readFile.ts @@ -7,8 +7,8 @@ import { basename, join } from 'path'; import type * as Sern from '../sern'; export type CommandVal = { - mod: Sern.Module; - options: ApplicationCommandOptionData[]; + mod: Sern.Module; + options: ApplicationCommandOptionData[]; }; export const Commands = new Map(); @@ -16,17 +16,17 @@ export const Alias = new Map(); // Courtesy of Townsy#0001 on Discord async function readPath(dir: string, arrayOfFiles: string[] = []): Promise { - try { - const files = readdirSync(dir); - for (const file of files) { - if (statSync(dir + '/' + file).isDirectory()) await readPath(dir + '/' + file, arrayOfFiles); - else arrayOfFiles.push(join(dir, '/', file)); - } - } catch (err) { - throw err; + try { + const files = readdirSync(dir); + for (const file of files) { + if (statSync(dir + '/' + file).isDirectory()) await readPath(dir + '/' + file, arrayOfFiles); + else arrayOfFiles.push(join(dir, '/', file)); } + } catch (err) { + throw err; + } - return arrayOfFiles; + return arrayOfFiles; } export const fmtFileName = (n: string) => n.substring(0, n.length - 3); @@ -37,20 +37,20 @@ export const fmtFileName = (n: string) => n.substring(0, n.length - 3); */ export async function buildData(handler: Sern.Handler): Promise< - { - name: string; - mod: Sern.Module; - absPath: string; - }[] + { + name: string; + mod: Sern.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 }; - }), - ); + 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 }; + }), + ); } export async function getCommands(dir: string): Promise { - return readPath(join(process.cwd(), dir)); + return readPath(join(process.cwd(), dir)); }