random refactor

This commit is contained in:
Histmy 2022-07-14 22:39:32 +02:00
parent 8820aa5c20
commit f099153890
2 changed files with 20 additions and 19 deletions

View File

@ -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 = /^(?<s>super_)?on_(?<h>.+)/.exec(name);
if (regex) {
const groups = regex.groups!;
const prefix = /^(?<s>super_)?on_(?<h>.+)/.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);
}

View File

@ -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();
};