diff --git a/package-lock.json b/package-lock.json index a05dc0c..6ded3eb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "denim_3001", - "version": "3001.36.1", + "version": "3001.37.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "denim_3001", - "version": "3001.36.1", + "version": "3001.37.0", "license": "ISC", "dependencies": { "@discordjs/opus": "github:discordjs/opus", diff --git a/package.json b/package.json index acec824..0979dc0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "denim_3001", - "version": "3001.36.1", + "version": "3001.37.0", "description": "Toto je velmi kvalitní bot.", "repository": { "url": "https://github.com/Histmy/Denim-Bot/" diff --git a/res/komandi.json b/res/komandi.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/res/komandi.json @@ -0,0 +1 @@ +{} diff --git a/src/app.ts b/src/app.ts index fca6ef3..b9419b5 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,10 +1,12 @@ -import { Client, Collection, Intents, Message, MessageActionRow, MessageButton } from "discord.js"; +import { Client, Collection, Intents, MessageActionRow, MessageButton } from "discord.js"; import { readdirSync } from "fs"; -import { CUser, EventSOn, KoamndNaExport, Komand, ListenerFunkce, Modul, RunFunkce, SRecord, SuperListenerFunkce } from "./utils/types"; +import { CUser, EventSOn, KomandNaExport, Komand, ListenerFunkce, Modul, RunFunkce, SRecord, SuperListenerFunkce, CustomKomandy } from "./utils/types"; import { adminLog, formatCas, loadEnv, oddiakritikovat } from "./utils/utils.js"; import levenshtein from "js-levenshtein"; import { emouty } from "./utils/emotes"; +const custom: CustomKomandy = require("./utils/customCommands"); + loadEnv(); const ints = Intents.FLAGS; @@ -16,7 +18,10 @@ const superEventy: SRecord = {}; const komandy: SRecord = {}; const aliasy: SRecord = {}; const kuldan_log: SRecord> = {}; -const komandyNaPoslani: KoamndNaExport[] = []; +const komandyNaPoslani: KomandNaExport[] = []; +let customKomandy: SRecord = custom.cKomandy; + +custom.emitter.on("komandi", komandi => customKomandy = komandi); const runEvent = (name: string, args: any[]) => { for (const listener of superEventy[name] || []) { @@ -76,7 +81,7 @@ readdirSync(modulFolder).forEach(soubor => { const n = modul[name]!; Object.keys(n).forEach(cmdName => { const value = n[cmdName]; - const toCoExportuju: KoamndNaExport = { name: cmdName }; + const toCoExportuju: KomandNaExport = { name: cmdName }; let hide = false; if (typeof value !== "object") { komandy[cmdName] = { run: value }; @@ -93,6 +98,11 @@ 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; const maKuldan = (id: string, komand: string, kuldan_komandu: number) => { @@ -137,11 +147,11 @@ client.on("messageCreate", async mes => { const cmdName = aliasy[komand] ?? komand; if (runEvent("message", [mes, cmdName])) return; - const cmd = komandy[cmdName]; + const cmd = komandy[cmdName] ?? customKomandy[cmdName]; if (!cmd) { const slova: string[] = []; - [...Object.keys(komandy), ...Object.keys(aliasy)].forEach(cmnd => { + [...Object.keys(komandy), ...Object.keys(customKomandy), ...Object.keys(aliasy)].forEach(cmnd => { const distance = levenshtein(cmnd, cmdName); if (distance <= Math.ceil(cmdName.length * 0.3)) slova.push(cmnd); }); @@ -160,7 +170,7 @@ client.on("messageCreate", async mes => { if (isNaN(lookupId)) return void mes.channel.send("neco nefunguje idk"); const komand = nabidka[lookupId]; const cmdName = aliasy[komand] ?? komand; - const cmd = komandy[cmdName]; + const cmd = komandy[cmdName] ?? customKomandy[cmdName]; radek.components.forEach(btn => btn.setDisabled()); i.update({ content: `ok vole ${emouty.d3k}`, components: [radek] }); runKomand(cmd, cmdName); diff --git a/src/modules/zbytek.ts b/src/modules/zbytek.ts index f53a0d7..21a3298 100644 --- a/src/modules/zbytek.ts +++ b/src/modules/zbytek.ts @@ -15,7 +15,7 @@ const exp: Modul = { debug_log: { hidden: true, run: (_, arg) => { - console.log("log: ", arg); + console.log("log:", arg); return "je to v konzoli"; } }, diff --git a/src/utils/customCommands.ts b/src/utils/customCommands.ts new file mode 100644 index 0000000..6b03884 --- /dev/null +++ b/src/utils/customCommands.ts @@ -0,0 +1,48 @@ +import { EventEmitter } from "events"; +import { KomandNaExport, Komand, RunFunkce, SRecord } from "./types"; +import { readFileSync, writeFileSync } from "fs"; +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); +export const emitter = new EventEmitter(); + +function save(): void; +function save(nesend: true): SRecord; +function save(nesend?: true) { + const cKomandi: SRecord = {}; + Object.keys(komandi).forEach(c => { + cKomandi[c] = { run: komandi[c].text }; + }); + if (nesend) return cKomandi; + cKomandy = cKomandi; + emitter.emit("komandi", cKomandy); + writeFileSync(cesta, JSON.stringify(komandi)); +} + +export const naucse: RunFunkce = (mes, arg) => { + const args = arg.split(" "); + if (module.exports.realKomandy[args[0]] || komandi[args[0]]) return "tuten komand uz ale egzistuje"; + if (!args.length) return "a co se mam jako naucit"; + if (arg.length == 1) return "a co bich nato mnel rict????"; + komandi[args[0].toLowerCase()] = { text: args.splice(1).join(" "), owner: mes.author.id }; + save(); + return "jo"; +}; + +export const zapomen: RunFunkce = (mes, arg) => { + const com = arg.toLowerCase(); + if (module.exports.realKomandy[com]) return "tuten komand se neda smazat ti smazko"; + const cmd = komandi[com]; + if (!cmd) return `komand "${arg}" neznam`; + if (cmd.owner != mes.author.id) return "tuto ael neni tvuj komand toxikale zkurvenej"; + delete komandi[com]; + save(); + return "jo"; +}; + +export const naSend: KomandNaExport[] = [ + { name: "naucse", arg: " " }, + { name: "zapomen", arg: "název komandu" } +]; diff --git a/src/utils/helpServer.ts b/src/utils/helpServer.ts index 279c60f..c4f9d9b 100644 --- a/src/utils/helpServer.ts +++ b/src/utils/helpServer.ts @@ -1,8 +1,8 @@ import { createServer } from "http"; -import { KoamndNaExport } from "./types"; +import { KomandNaExport } from "./types"; createServer((_, res) => { - const komandi: KoamndNaExport[] = module.exports.cmds; + const komandi: KomandNaExport[] = module.exports.cmds; const odpoved = JSON.stringify({ komandy: komandi }); res.setHeader("Content-Type", "application/json"); res.end(odpoved); diff --git a/src/utils/types.ts b/src/utils/types.ts index 9cd7b7b..a9657a5 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -1,5 +1,6 @@ import { VoiceConnection } from "@discordjs/voice"; import { Client, ClientEvents, ClientPresenceStatusData, Message, MessageOptions, User } from "discord.js"; +import { EventEmitter } from "events"; type OutputRunFunkce = string | MessageOptions | void; @@ -71,7 +72,7 @@ export interface MuzikaFace { volume: number; } -export type KoamndNaExport = { +export type KomandNaExport = { name: string; } & BaseKomandProps; @@ -83,3 +84,12 @@ export interface JoinHovna { export interface CUser extends User { presence?: ClientPresenceStatusData; } + +export interface CustomKomandy { + cKomandy: SRecord; + emitter: EventEmitter; + naucse: RunFunkce; + zapomen: RunFunkce; + naSend: KomandNaExport[]; + realKomandy: SRecord; +}