diff --git a/src/app.ts b/src/app.ts index 8fbb65d..9a66b5d 100644 --- a/src/app.ts +++ b/src/app.ts @@ -3,7 +3,7 @@ import { Client, Intents, Message } from "discord.js"; import { config } from "dotenv"; import { readdirSync } from "fs"; import { emouty } from "./utils/emotes"; -import { Komand, ListenerFunkce, Modul } from "./utils/types"; +import { Komand, ListenerFunkce, Modul, SuperListenerFunkce } from "./utils/types"; import { formatCas, oddiakritikovat } from "./utils/utils.js"; const ints = Intents.FLAGS; @@ -12,14 +12,20 @@ config(); const prefix = process.env.PREFIX || "more"; const modulFolder = `${__dirname}/modules/`; -const eventy: Record = { on_message: [] }; +const eventy: Record = {}; +const superEventy: Record = {}; const komandy: Record = {}; const aliasy: Record = {}; let spink = false; const kuldan_log: Record> = {}; const runEvent = (name: string, args: any[]) => { - eventy[name].forEach(listener => { + for (const listener of superEventy[name] || []) { + if (!listener) continue; + if (listener(...args)) return true; + } + + eventy[name]?.forEach(listener => { listener(...args); }); }; @@ -30,12 +36,21 @@ readdirSync(modulFolder).forEach(soubor => { console.log(`Loaded: ${modulFolder}${soubor}`); modul.client = client; Object.keys(modul).forEach(name => { - if (name.startsWith("on_")) { - if (!eventy[name]) { - eventy[name] = []; - if (name !== "on_message") client.on(name.slice(3), (...args) => runEvent(name, args)); + const regex = /^(?super_)?on_(?.+)/.exec(name); + if (regex) { + const groups = regex.groups!; + const ev = groups.s ? superEventy : eventy; + if (!ev[groups.h]) { + ev[groups.h] = []; + if (groups.h != "message") client.on(groups.h, (...args) => void runEvent(groups.h, args)); } - eventy[name].push(modul[name]); + const n = modul[name]; + if (typeof n == "object") { + const prev = ev[groups.h][n.pos] as SuperListenerFunkce | undefined; + ev[groups.h][n.pos] = n.fun; + if (prev) ev[groups.h].push(prev); + } + else ev[groups.h].push(n); } else if (name === "more_komandy") { Object.keys(modul[name]).forEach(cmdName => { const value = modul[name][cmdName]; @@ -85,14 +100,14 @@ const maKuldan = (id: string, komand: string, kuldan_komandu: number) => { client.on("messageCreate", mes => { if (process.env.IGNORE_MESS || spim(mes)) return; - runEvent("on_message", [mes]); const [mes_prefix, komandSDiakritikou, ...args] = mes.content.split(" "); - if (mes_prefix.toLowerCase() !== prefix) return; + if (mes_prefix.toLowerCase() !== prefix) return void runEvent("message", [mes]); if (!komandSDiakritikou) return void mes.channel.send("coe voe"); const komand = oddiakritikovat(komandSDiakritikou).toLowerCase(); const celArgs = args.join(" "); const cmdName = aliasy[komand] ?? komand; + if (runEvent("message", [mes, cmdName])) return; const cmd = komandy[cmdName]; if (!cmd) return void mes.channel.send("co to znamena ti gadzovko"); diff --git a/src/utils/types.ts b/src/utils/types.ts index 74e72d2..f9a5bec 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -10,10 +10,17 @@ interface KomandRaw { export type ListenerFunkce = (...args: any[]) => void; +export type SuperListenerFunkce = (...args: any[]) => boolean; + +interface SuperObject { + pos: number; + fun: SuperListenerFunkce; +} + export type Modul = { more_komandy: Record; client: Client; -} & Record; +} & Record; export interface Komand { run: RunFunkce | string;