From a42e12015da08b35c4bbc34b8d2899d3cfbc091e Mon Sep 17 00:00:00 2001 From: Histmy Date: Sun, 20 Feb 2022 14:12:14 +0100 Subject: [PATCH] pomoc3 will now actually show custom commands --- src/app.ts | 12 +++++++----- src/utils/customCommands.ts | 19 ++++++++++--------- src/utils/utils.ts | 4 ++++ 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/app.ts b/src/app.ts index 868374c..8def77c 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,10 +1,11 @@ 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 } from "./utils/utils.js"; +import { adminLog, formatCas, loadEnv, oddiakritikovat, sortArr } from "./utils/utils.js"; import levenshtein from "js-levenshtein"; import { emouty } from "./utils/emotes"; +const helpServer = require("./utils/helpServer"); const custom: CustomKomandy = require("./utils/customCommands"); loadEnv(); @@ -21,7 +22,10 @@ const kuldan_log: SRecord> = {}; const komandyNaPoslani: KomandNaExport[] = []; let customKomandy: SRecord = custom.cKomandy; -custom.emitter.on("komandi", komandi => customKomandy = komandi); +custom.emitter.on("komandi", (komandi, naPoslani) => { + customKomandy = komandi; + helpServer.cmds = sortArr([...komandyNaPoslani, ...naPoslani]); +}); const runEvent = (name: string, args: any[]) => { for (const listener of superEventy[name] || []) { @@ -101,9 +105,7 @@ readdirSync(modulFolder).forEach(soubor => { custom.realKomandy = komandy; komandy["naucse"] = { run: custom.naucse }; komandy["zapomen"] = { run: custom.zapomen }; -komandyNaPoslani.push(...custom.naSend); - -require("./utils/helpServer").cmds = komandyNaPoslani; +helpServer.cmds = sortArr([...komandyNaPoslani, ...custom.naSend]); const maKuldan = (id: string, komand: string, kuldan_komandu: number) => { if (!kuldan_log[komand]) kuldan_log[komand] = {}; diff --git a/src/utils/customCommands.ts b/src/utils/customCommands.ts index 6b03884..1754433 100644 --- a/src/utils/customCommands.ts +++ b/src/utils/customCommands.ts @@ -5,19 +5,25 @@ import { join } from "path"; const cesta = join(__dirname, "../../res/komandi.json"); const komandi: SRecord<{ text: string; owner: string; }> = JSON.parse(readFileSync(cesta).toString()); -export let cKomandy = save(true); +const baseNaSend: KomandNaExport[] = [ + { name: "naucse", arg: " " }, + { name: "zapomen", arg: "název komandu" } +]; +export let [cKomandy, naSend] = save(true); export const emitter = new EventEmitter(); function save(): void; -function save(nesend: true): SRecord; +function save(nesend: true): [SRecord, KomandNaExport[]]; function save(nesend?: true) { const cKomandi: SRecord = {}; + const naSend = baseNaSend; Object.keys(komandi).forEach(c => { cKomandi[c] = { run: komandi[c].text }; + naSend.push({ name: c }); }); - if (nesend) return cKomandi; + if (nesend) return [cKomandi, naSend]; cKomandy = cKomandi; - emitter.emit("komandi", cKomandy); + emitter.emit("komandi", cKomandy, naSend); writeFileSync(cesta, JSON.stringify(komandi)); } @@ -41,8 +47,3 @@ export const zapomen: RunFunkce = (mes, arg) => { save(); return "jo"; }; - -export const naSend: KomandNaExport[] = [ - { name: "naucse", arg: " " }, - { name: "zapomen", arg: "název komandu" } -]; diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 47d02aa..c219f96 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -190,3 +190,7 @@ export function adminLog(client: Client, text: string) { client.users.cache.get(process.env.adminID)?.createDM().then(ch => ch.send(text)); } } + +export function sortArr(arr: any[]) { + return arr.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0); +}