From 8b95db79cbd87e136a6cd87791d2f00d637ba6f4 Mon Sep 17 00:00:00 2001 From: Histmy Date: Tue, 13 Dec 2022 17:40:40 +0100 Subject: [PATCH] finally made custom commands easier to maintain --- .gitignore | 6 +- package-lock.json | 4 +- package.json | 2 +- res/ftipy.json | 8 +++ src/app.ts | 55 ++++----------- src/modules/custom.ts | 135 ++++++++++++++++++++++++++++++++++++ src/utils/customCommands.ts | 105 ---------------------------- src/utils/helpServer.ts | 24 +++++-- src/utils/types.ts | 13 +++- 9 files changed, 193 insertions(+), 159 deletions(-) create mode 100644 res/ftipy.json create mode 100644 src/modules/custom.ts delete mode 100644 src/utils/customCommands.ts diff --git a/.gitignore b/.gitignore index d6d66b0..fdb3b13 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,5 @@ node_modules config.json out -res/ftipy.json -res/komandi.json -res/pomoc.json -res/aliasi.json +res/customKomandy.json +res/customAliasy.json diff --git a/package-lock.json b/package-lock.json index 7fc955e..c5f56a7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "denim_3001", - "version": "3001.44.0", + "version": "3001.44.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "denim_3001", - "version": "3001.44.0", + "version": "3001.44.1", "license": "ISC", "dependencies": { "@discordjs/voice": "^0.13.0", diff --git a/package.json b/package.json index 132675d..9699e09 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "denim_3001", - "version": "3001.44.0", + "version": "3001.44.1", "description": "Toto je velmi kvalitní bot.", "repository": { "url": "https://github.com/Histmy/Denim-Bot/" diff --git a/res/ftipy.json b/res/ftipy.json new file mode 100644 index 0000000..41587d1 --- /dev/null +++ b/res/ftipy.json @@ -0,0 +1,8 @@ +[ + "Tož šimša si všimnul jednoho velkeho staveniště, tak večer se tam vydal z atobatekou na boku a flefinou vruce uříznul tu největší traverzu a uříznul ji pak si ji dal na záda a šel dom ale když šel zpět tak se všimul pitbula který hlýdal staveniště a on byl na železnem řetězu tak ten řetěz popat ale nedomyslel to že ten pitbull poběží za ním a tak utíkal ale pitbull byl rychlejší a tak se mu zakousnul do prdele a tak utíkal před policií aj z pitbulem.", + "tož šimša si řekl že se pujde okoupat ale nedošlomu že na koupání potřebuje vodu a ne močůvku <:DENIM3K_weSmart:627809296624779274>", + "tož šimšu zaměstnaly na praxi do pneu servisu ale když tam došli do druhý den tak tam nic nebylo jelikož to vše vybrakoval přes noc a pak pan hlavní pneusevisák to nevydržel a exnul", + "tož šimša šel z vaku a vykopal mareše tak mocně a vykopal ho tak protože byl je závislí na cigáru a potřeboval svo mocně nemocno dáku tak vyvinul tam mocně silu aby o vykopnul", + "tož šimša jednou šel a tak moc šel do velkooooobjemního kontejnetu pro noví komponenti do jeho mašiny a podkal tam cygani a ty to tam brali a von je podkal a šel jim rozbít drčku haby se už a řek jim haby se už nevokázali u jeho kontejmetu a kdyš čel cezto dom tak mu ti kombonendi zpadli do bláda a tak mu zpadli mo ze šel sy dát zlatou dávkou", + "tož šimša seděl ve 3kole a tadytámlecti haranti mu šimšakanovali a povidaly mu 6e je římšák a šiška a šimšalátor a on povidal aby to nepovidali ale oni to povidali a pověděli mu žeto nep5estanou povidat a on ze sač4al čerchmantit a tak zasedl do traktotu a jel vo ulycy a jak jel tak jel a jel a přeji6děl kraliki a kočki a koně a jin9 zvěrstva a tak mu cajti pošeli flastr a von platil velk9 prachy, protože m2l flastra, no nem2l tag moc chlastat" +] diff --git a/src/app.ts b/src/app.ts index f2afb8a..b2fb512 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,14 +1,11 @@ import { ActionRowBuilder, ButtonBuilder, ButtonStyle, ChannelType, Client, ComponentType, GatewayIntentBits, Message, Partials } from "discord.js"; import { readdirSync } from "fs"; -import { CUser, EventSOn, KomandNaExport, Komand, ListenerFunkce, Modul, SRecord, SuperListenerFunkce, CustomKomandy, RunFunkce } from "./utils/types"; +import { CUser, EventSOn, KomandNaExport, Komand, ListenerFunkce, Modul, SRecord, SuperListenerFunkce, RunFunkce, CClient, HelpServer } from "./utils/types"; import { adminLog, formatCas, oddiakritikovat, log } from "./utils/utils.js"; import levenshtein from "js-levenshtein"; import { emouty } from "./utils/emotes"; import { existsSync } from "fs"; -const helpServer = require("./utils/helpServer"); -const custom: CustomKomandy = require("./utils/customCommands"); - if (!existsSync("config.json")) throw new Error("config.json neexistuje"); process.env = { ...process.env, ...require("../config.json") }; @@ -16,34 +13,17 @@ const ints = GatewayIntentBits; const client = new Client({ partials: [Partials.Channel], intents: [ints.Guilds, ints.GuildVoiceStates, ints.GuildPresences, ints.GuildMessages, ints.DirectMessages, ints.MessageContent] -}); +}) as CClient; const prefix = process.env.prefix || "more"; const modulFolder = `${__dirname}/modules/`; const eventy: SRecord = {}; const superEventy: SRecord = {}; -const komandy: SRecord = {}; -const aliasy: SRecord = {}; const kuldan_log: SRecord> = {}; const komandyNaPoslani: KomandNaExport[] = []; -let customKomandy: SRecord = custom.cKomandy; -let customAliasy: SRecord = custom.cAliasy; +const helpServer: HelpServer = require("./utils/helpServer"); -custom.emitter.on("komandi", (komandi, aliasi, naPoslani) => { - customKomandy = komandi; - customAliasy = aliasi; - zpracovatAliasyAKomandy(aliasi, naPoslani); -}); - -function zpracovatAliasyAKomandy(aliasi: SRecord, naPoslani: KomandNaExport[]) { - Object.keys(aliasi).forEach((a: string) => { - const i = komandyNaPoslani.findIndex(k => k.name == aliasi[a]); - if (!komandyNaPoslani[i]) return; - const arr = komandyNaPoslani[i].als; - if (arr && !arr.includes(a)) arr.push(a); - if (!arr) komandyNaPoslani[i].als = [a]; - }); - helpServer.cmds = [...komandyNaPoslani, ...naPoslani].sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0); -} +client.komandy = {}; +client.aliasy = {}; function getFirstArg(fn: RunFunkce | string) { if (typeof fn != "function") return; @@ -107,13 +87,13 @@ readdirSync(modulFolder).forEach(soubor => { const toCoExportuju: KomandNaExport = { name: cmdName }; let hide = false; if (typeof value !== "object") { - komandy[cmdName] = { run: value }; + client.komandy[cmdName] = { run: value }; toCoExportuju.arg = getFirstArg(value); } else { - komandy[cmdName] = { run: value.run, cd: value.cd, DMUnsafe: value.DMUnsafe }; + client.komandy[cmdName] = { run: value.run, cd: value.cd, DMUnsafe: value.DMUnsafe }; Object.assign(toCoExportuju, { als: value.als, arg: value.arg ?? getFirstArg(value.run) }); hide = !!value.hidden; - value.als?.forEach(al => aliasy[al] = cmdName); + value.als?.forEach(al => client.aliasy[al] = cmdName); } if (!hide) komandyNaPoslani.push(toCoExportuju); }); @@ -121,14 +101,7 @@ readdirSync(modulFolder).forEach(soubor => { }); }); -custom.realKomandy = komandy; -custom.realAliasy = aliasy; -komandy["naucse"] = { run: custom.naucse }; -komandy["zapomen"] = { run: custom.zapomen }; -komandy["kohoje"] = { run: custom.kohoje }; -komandy["naucsealias"] = { run: custom.naucsealias }; -komandy["zapomenalias"] = { run: custom.zapomenalias }; -zpracovatAliasyAKomandy(custom.cAliasy, custom.naSend); +helpServer.komandy = komandyNaPoslani; const maKuldan = (id: string, komand: string, kuldan_komandu: number) => { if (!kuldan_log[komand]) kuldan_log[komand] = {}; @@ -172,18 +145,18 @@ client.on("messageCreate", async mes => { const celArgs = args.join(" "); const komandBez = oddiakritikovat(komandSDiakritikou).toLowerCase(); - const cmdName = aliasy[komandBez] ?? customAliasy[komandBez] ?? komandBez; + const cmdName = client.aliasy[komandBez] ?? komandBez; if (runEvent("messageCreate", [mes, cmdName])) return; - const komand = komandy[cmdName] ?? customKomandy[cmdName]; + const komand = client.komandy[cmdName]; if (mes.channel.type == ChannelType.DM && komand?.DMUnsafe) return void mes.channel.send("tuten komand bohuzel v sz nefunkuje"); if (komand) return void runKomand(mes, komand, cmdName, celArgs); // neměl jsi na mysli? const slova: string[] = []; - [...Object.keys(komandy), ...Object.keys(customKomandy), ...Object.keys(aliasy), ...Object.keys(customAliasy)].forEach(cmd => { + [...Object.keys(client.komandy), ...Object.keys(client.aliasy)].forEach(cmd => { const distance = levenshtein(cmd, cmdName); if (distance <= Math.ceil(cmdName.length * 0.3)) slova.push(cmd); }); @@ -202,8 +175,8 @@ client.on("messageCreate", async mes => { const lookupId = Number(i.customId); if (isNaN(lookupId)) return void mes.channel.send("neco nefunguje idk"); const komand = nabidka[lookupId]; - const cmdName = aliasy[komand] ?? customAliasy[komand] ?? komand; - const cmd = komandy[cmdName] ?? customKomandy[cmdName]; + const cmdName = client.aliasy[komand] ?? komand; + const cmd = client.komandy[cmdName]; radek.components.forEach(btn => btn.setDisabled()); i.update({ content: `ok vole ${emouty.d3k}`, components: [radek] }); runKomand(mes, cmd, cmdName, celArgs); diff --git a/src/modules/custom.ts b/src/modules/custom.ts new file mode 100644 index 0000000..98372e4 --- /dev/null +++ b/src/modules/custom.ts @@ -0,0 +1,135 @@ +import { CClient, HelpServer, Komand, KomandNaExport, Modul, SRecord } from "../utils/types"; +import { join } from "path"; +import { existsSync, readFileSync, writeFileSync } from "fs"; +import { oddiakritikovat } from "../utils/utils"; + +let client: CClient; +const cesta = `${join(__dirname, "../../res/")}custom`; +const helpServer: HelpServer = require("../utils/helpServer"); +let zakladniKomandy: SRecord; +let zakladniAliasy: SRecord; +let customKomandy: SRecord<{ text: string; owner: string; }>; +let customAliasy: SRecord<{ cmd: string; owner: string; }>; + +function spojit() { + const cKomandyAsKomand: SRecord = {}; + const cKomandyForExport: KomandNaExport[] = []; + + for (const komand in customKomandy) { + cKomandyAsKomand[komand] = { run: customKomandy[komand].text }; + cKomandyForExport.push({ name: komand }); + } + + client.komandy = { ...zakladniKomandy, ...cKomandyAsKomand }; + + helpServer.customKomandy = cKomandyForExport; + + const cAliasyForExport: SRecord = {}; + + for (const alias in customAliasy) { + cAliasyForExport[alias] = customAliasy[alias].cmd; + } + + client.aliasy = { ...zakladniAliasy, ...cAliasyForExport }; + helpServer.customAliasy = cAliasyForExport; +} + +function save() { + writeFileSync(`${cesta}Komandy.json`, JSON.stringify(customKomandy)); + writeFileSync(`${cesta}Aliasy.json`, JSON.stringify(customAliasy)); + spojit(); +} + +const exp: Modul = { + more_komandy: { + naucse: { + arg: " ", + run: (mes, arg) => { + const args = arg.split(" ").filter(v => v != ""); + if (args.length == 0) return "a co se mam jako naucit"; + if (args.length == 1) return "a co bich nato mnel rict????"; + + const name = oddiakritikovat(args[0].toLowerCase()); + if (zakladniKomandy[name] || customKomandy[name]) return "tuten komand uz ale egzistuje"; + if (zakladniAliasy[name] || customAliasy[name]) return "tuto uz je ale alijas"; + + customKomandy[name] = { text: args.splice(1).join(" "), owner: mes.author.id }; + save(); + return "jo"; + } + }, + + naucsealias: { + arg: " ", + als: ["naucsea"], + run: (mes, arg) => { + const args = arg.split(" ").filter(v => v != "").map(e => oddiakritikovat(e).toLowerCase()); + + if (args.length == 0) return "a co se mam jako naucit"; + if (args.length == 1) return "a co to ma znamenat????"; + + if (zakladniKomandy[args[0]] || customKomandy[args[0]]) return "tuto uz je ale komand"; + if (zakladniAliasy[args[0]] || customAliasy[args[0]]) return "tuto uz ale egzistuje"; + + let jmeno: string; + if (!zakladniKomandy[args[1]] && !zakladniAliasy[args[1]]) { + if (!customKomandy[args[1]] && !customAliasy[args[1]]) return `nejze "${args[1]}" neni realnej ani vlastni komand ani alias`; + jmeno = customKomandy[args[1]] ? args[1] : customAliasy[args[1]].cmd; + } else jmeno = zakladniKomandy[args[1]] ? args[1] : zakladniAliasy[args[1]]; + + customAliasy[args[0]] = { cmd: jmeno, owner: mes.author.id }; + save(); + return "jo"; + } + }, + + zapomen: { + arg: "název komandu nebo aliasu", + run: (mes, arg) => { + const name = oddiakritikovat(arg.toLowerCase()); + + if (zakladniKomandy[name]) return "tuten komand se neda smazat ti smazko"; + if (zakladniAliasy[name]) return "tuten alijaas se neda smazat ti smazko"; + + let jeKomand: boolean; + let cmd; + if (cmd = customKomandy[name]) jeKomand = true; + else if (cmd = customAliasy[name]) jeKomand = false; + else return `"${name}" nen komnad an alias`; + + if (cmd.owner != mes.author.id) return "tuto ael neni tvuj komand toxikale zkurvenej"; + + jeKomand ? delete customKomandy[name] : delete customAliasy[name]; + save(); + return "jo"; + } + }, + + kohoje: { + arg: "název komandu nebo aliasu", + run: (_, arg) => { + const cmdName = oddiakritikovat(arg.toLowerCase()); + const cmd = customKomandy[cmdName] ?? customAliasy[cmdName]; + + if (!cmd) return `"${cmdName}" nen vlastny komand an alijas`; + + return `vlastnitel je <@${cmd.owner}>`; + } + } + }, + + on_ready: () => { + client = module.exports.client; + zakladniKomandy = client.komandy; + zakladniAliasy = client.aliasy; + + if (!existsSync(`${cesta}Komandy.json`)) customKomandy = {}; + else customKomandy = JSON.parse(readFileSync(`${cesta}Komandy.json`).toString()); + if (!existsSync(`${cesta}Aliasy.json`)) customAliasy = {}; + else customAliasy = JSON.parse(readFileSync(`${cesta}Aliasy.json`).toString()); + + spojit(); + } +}; + +module.exports = exp; diff --git a/src/utils/customCommands.ts b/src/utils/customCommands.ts deleted file mode 100644 index ef156ac..0000000 --- a/src/utils/customCommands.ts +++ /dev/null @@ -1,105 +0,0 @@ -import { EventEmitter } from "events"; -import { KomandNaExport, Komand, RunFunkce, SRecord } from "./types"; -import { readFileSync, writeFileSync } from "fs"; -import { join } from "path"; -import { oddiakritikovat } from "./utils"; - -const ex = module.exports; -const cesta = join(__dirname, "../../res/"); -const customKomandi: SRecord<{ text: string; owner: string; }> = JSON.parse(readFileSync(`${cesta}komandi.json`).toString()); -const customAliasy: SRecord<{ cmd: string; owner: string; }> = JSON.parse(readFileSync(`${cesta}aliasi.json`).toString()); -const baseNaSend: KomandNaExport[] = [ - { name: "naucse", arg: " " }, - { name: "zapomen", arg: "jméno komandu" }, - { name: "naucsealias", arg: " ", als: ["nausea"] }, - { name: "zapomenalias", arg: "jméno aliasu", als: ["zapomena"] } -]; -export let [cKomandy, cAliasy, naSend] = save(true); -export const emitter = new EventEmitter(); - -function save(): void; -function save(nesend: true): [SRecord, SRecord, KomandNaExport[]]; -function save(nesend?: true) { - const cKomandi: SRecord = {}; - const cAliasi: SRecord = { naucsea: "naucsealias", zepomena: "zapomenalias" }; - const naSend = [...baseNaSend]; - Object.keys(customKomandi).forEach(c => { - cKomandi[c] = { run: customKomandi[c].text }; - naSend.push({ name: c }); - }); - Object.keys(customAliasy).forEach(a => { - cAliasi[a] = customAliasy[a].cmd; - }); - if (nesend) return [cKomandi, cAliasi, naSend]; - cKomandy = cKomandi; - emitter.emit("komandi", cKomandy, cAliasi, naSend); - writeFileSync(`${cesta}komandi.json`, JSON.stringify(customKomandi)); - writeFileSync(`${cesta}aliasi.json`, JSON.stringify(customAliasy)); -} - -export const naucse: RunFunkce = (mes, arg) => { - const args = arg.split(" ").filter(v => v != ""); - if (args.length == 0) return "a co se mam jako naucit"; - if (args.length == 1) return "a co bich nato mnel rict????"; - - args[0] = oddiakritikovat(args[0].toLowerCase()); - if (ex.realKomandy[args[0]] || customKomandi[args[0]] || ex.realAliasy[args[0]] || customAliasy[args[0]]) return "tuten komand uz ale egzistuje"; - - customKomandi[args[0]] = { text: args.splice(1).join(" "), owner: mes.author.id }; - save(); - return "jo"; -}; - -export const zapomen: RunFunkce = (mes, arg) => { - const com = oddiakritikovat(arg.toLowerCase()); - - if (ex.realKomandy[com]) return "tuten komand se neda smazat ti smazko"; - const cmd = customKomandi[com]; - if (!cmd) return `komand "${arg}" neznam`; - if (cmd.owner != mes.author.id) return "tuto ael neni tvuj komand toxikale zkurvenej"; - - delete customKomandi[com]; - save(); - return "jo"; -}; - -export const kohoje: RunFunkce = (_, arg) => { - const cmdName = oddiakritikovat(arg.toLowerCase()); - const cmd = customKomandi[cmdName] ?? customAliasy[cmdName]; - - if (!cmd) return `"${cmdName}" nen ani komand an alijas`; - - return `vlastnitel je <@${cmd.owner}>`; -}; - -export const naucsealias: RunFunkce = (mes, arg) => { - const args = arg.split(" ").filter(v => v != "").map(e => oddiakritikovat(e).toLowerCase()); - - if (args.length == 0) return "a co se mam jako naucit"; - if (args.length == 1) return "a co to ma znamenat????"; - if (ex.realKomandy[args[0]] || customKomandi[args[0]] || ex.realAliasy[args[0]] || customAliasy[args[0]]) return "tuto uz ale egzistuje"; - - let jmeno: string; - if (!ex.realKomandy[args[1]] && !ex.realAliasy[args[1]]) { - if (!customKomandi[args[1]] && !customAliasy[args[1]]) return `nejze "${args[1]}" neni realnej ani vlastni komand ani alias`; - jmeno = customKomandi[args[1]] ? args[1] : customAliasy[args[1]].cmd; - } - else jmeno = (ex.realKomandy[args[1]]) ? args[1] : ex.realAliasy[args[1]]; - - customAliasy[args[0]] = { cmd: jmeno, owner: mes.author.id }; - save(); - return "jo"; -}; - -export const zapomenalias: RunFunkce = (mes, arg) => { - const al = oddiakritikovat(arg.toLowerCase()); - - if (ex.realAliasy[al]) return "tutn alijas se ale neda smazat ty smazenice"; - const als = customAliasy[al]; - if (!als) return `alijas "${al}" neznam`; - if (als.owner != mes.author.id) return "tuto ael neni tvuj alijas toxikale zkurvenej"; - - delete customAliasy[al]; - save(); - return "jo"; -}; diff --git a/src/utils/helpServer.ts b/src/utils/helpServer.ts index c4f9d9b..3dbca43 100644 --- a/src/utils/helpServer.ts +++ b/src/utils/helpServer.ts @@ -1,9 +1,23 @@ import { createServer } from "http"; -import { KomandNaExport } from "./types"; +import { KomandNaExport, SRecord } from "./types"; +import { log } from "./utils"; createServer((_, res) => { - const komandi: KomandNaExport[] = module.exports.cmds; - const odpoved = JSON.stringify({ komandy: komandi }); - res.setHeader("Content-Type", "application/json"); - res.end(odpoved); + const komandy: KomandNaExport[] = module.exports.komandy; + const customKomandy: KomandNaExport[] = module.exports.customKomandy; + const customAliasy: SRecord = module.exports.customAliasy; + + const vsechnyKomandy: KomandNaExport[] = [...komandy, ...customKomandy]; + + for (const key in customAliasy) { + const i = vsechnyKomandy.findIndex(komand => komand.name == customAliasy[key]); + if (i == -1) { log(new Error(`ičko neexistuje. hledal sem "${key}"`)); continue; } + (vsechnyKomandy[i].als ??= []).push(key); + } + + vsechnyKomandy.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0); + + const odpoved = JSON.stringify({ komandy: vsechnyKomandy }); + res.setHeader("Content-Type", "application/json"); + res.end(odpoved); }).listen(5712); diff --git a/src/utils/types.ts b/src/utils/types.ts index b5c5be5..e044d22 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -7,7 +7,7 @@ type OutputRunFunkce = string | MessagePayload | MessageCreateOptions | void; export type RunFunkce = (message: Message, argumenty: string) => OutputRunFunkce | Promise; -interface BaseKomandProps { +export interface BaseKomandProps { als?: string[]; arg?: string; } @@ -102,3 +102,14 @@ export interface CustomKomandy { realKomandy: SRecord; realAliasy: SRecord; } + +export interface HelpServer { + komandy: KomandNaExport[]; + customKomandy: KomandNaExport[]; + customAliasy: SRecord; +} + +export class CClient extends Client { + komandy!: SRecord; + aliasy!: SRecord; +}