From f099153890a13dc5c351cb81b1655082a341f765 Mon Sep 17 00:00:00 2001 From: Histmy Date: Thu, 14 Jul 2022 22:39:32 +0200 Subject: [PATCH] random refactor --- src/app.ts | 29 +++++++++++------------------ src/utils/utils.ts | 10 +++++++++- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/app.ts b/src/app.ts index 918316f..1e5c43b 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,7 +1,7 @@ import { Client, Collection, Intents, MessageActionRow, MessageButton } from "discord.js"; import { readdirSync } from "fs"; import { CUser, EventSOn, KomandNaExport, Komand, ListenerFunkce, Modul, RunFunkce, SRecord, SuperListenerFunkce, CustomKomandy } from "./utils/types"; -import { adminLog, formatCas, loadEnv, oddiakritikovat, sortArr } from "./utils/utils.js"; +import { adminLog, getFirstArg, formatCas, loadEnv, oddiakritikovat, sortArr } from "./utils/utils.js"; import levenshtein from "js-levenshtein"; import { emouty } from "./utils/emotes"; @@ -32,9 +32,9 @@ custom.emitter.on("komandi", (komandi, naPoslani) => { const runEvent = (name: string, args: any[]) => { for (const listener of superEventy[name] || []) { - if (!listener) continue; + if (!listener) continue; // [after] pozice v superEventy arrayi se dají nastavovat a teoreticky může nastat mezera try { - if (listener(...args)) return true; + if (listener(...args)) return true; // if listener returns true, it means, we shouldn't continue with execustion } catch (e) { if (process.env.dieOnError) throw e; console.log("error pri spusteni super listeneru", e); @@ -53,30 +53,23 @@ const runEvent = (name: string, args: any[]) => { }); }; -const getTenParam = (fn: RunFunkce): string | undefined => fn.toString() - .match(/(?:function\s.*?)?\(([^)]*)\)|\w+ =>/)![1] - ?.split(",")[1] - ?.trim(); - -const fnToArg = (fn: RunFunkce | string) => { - if (typeof fn != "function") return; - return getTenParam(fn); -}; - +// Loading of modules readdirSync(modulFolder).forEach(soubor => { if (!soubor.endsWith(".js")) return; const modul: Modul = require(`${modulFolder}${soubor}`); console.log(`Loaded: ${modulFolder}${soubor}`); + modul.client = client; Object.keys(modul).forEach(name => { - const regex = /^(?super_)?on_(?.+)/.exec(name); - if (regex) { - const groups = regex.groups!; + const prefix = /^(?super_)?on_(?.+)/.exec(name); + if (prefix) { + const groups = prefix.groups!; const ev = groups.s ? superEventy : eventy; if (!ev[groups.h]) { ev[groups.h] = []; if (!["message", "userPresenceUpdate"].includes(groups.h)) client.on(groups.h, (...args) => void runEvent(groups.h, args)); } + const n = modul[name as EventSOn]!; if (typeof n == "object") { const prev = ev[groups.h][n.pos]; @@ -92,10 +85,10 @@ readdirSync(modulFolder).forEach(soubor => { let hide = false; if (typeof value !== "object") { komandy[cmdName] = { run: value }; - toCoExportuju.arg = fnToArg(value); + toCoExportuju.arg = getFirstArg(value); } else { komandy[cmdName] = { run: value.run, cd: value.cd, DMUnsafe: value.DMUnsafe }; - Object.assign(toCoExportuju, { als: value.als, arg: value.arg ?? fnToArg(value.run) }); + Object.assign(toCoExportuju, { als: value.als, arg: value.arg ?? getFirstArg(value.run) }); hide = !!value.hidden; value.als?.forEach(al => aliasy[al] = cmdName); } diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 6159201..bf2f406 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -1,7 +1,7 @@ import { AudioPlayerStatus, AudioResource, createAudioPlayer, createAudioResource, entersState, getVoiceConnection, joinVoiceChannel, PlayerSubscription, StreamType, VoiceConnection, VoiceConnectionStatus } from "@discordjs/voice"; import { Client, Guild, StageChannel, VoiceChannel } from "discord.js"; import { once } from "events"; -import { JoinHovna, MuzikaFace, SRecord } from "./types"; +import { JoinHovna, MuzikaFace, RunFunkce, SRecord } from "./types"; import { existsSync } from "fs"; import { Readable } from "node:stream"; @@ -195,3 +195,11 @@ export function adminLog(client: Client, text: string) { export function sortArr(arr: any[]) { return arr.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0); } + +export function getFirstArg(fn: RunFunkce | string) { + if (typeof fn != "function") return; + return fn.toString() + .match(/(?:function\s.*?)?\(([^)]*)\)|\w+ =>/)![1] + ?.split(",")[1] + ?.trim(); +};