diff --git a/package-lock.json b/package-lock.json index c5f56a7..2871110 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "denim_3001", - "version": "3001.44.1", + "version": "3001.45.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "denim_3001", - "version": "3001.44.1", + "version": "3001.45.0", "license": "ISC", "dependencies": { "@discordjs/voice": "^0.13.0", diff --git a/package.json b/package.json index 9699e09..fb58fc1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "denim_3001", - "version": "3001.44.1", + "version": "3001.45.0", "description": "Toto je velmi kvalitní bot.", "repository": { "url": "https://github.com/Histmy/Denim-Bot/" diff --git a/src/app.ts b/src/app.ts index b2fb512..040a281 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,7 +1,7 @@ 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, RunFunkce, CClient, HelpServer } from "./utils/types"; -import { adminLog, formatCas, oddiakritikovat, log } from "./utils/utils.js"; +import { adminLog, formatCas, oddiakritikovat, log, rand } from "./utils/utils.js"; import levenshtein from "js-levenshtein"; import { emouty } from "./utils/emotes"; import { existsSync } from "fs"; @@ -15,6 +15,7 @@ const client = new Client({ intents: [ints.Guilds, ints.GuildVoiceStates, ints.GuildPresences, ints.GuildMessages, ints.DirectMessages, ints.MessageContent] }) as CClient; const prefix = process.env.prefix || "more"; +const DJ_PREFIX = "dj:"; const modulFolder = `${__dirname}/modules/`; const eventy: SRecord = {}; const superEventy: SRecord = {}; @@ -114,6 +115,104 @@ const maKuldan = (id: string, komand: string, kuldan_komandu: number) => { return 0; }; +function assume(cond: boolean, msg: string) { + if (!cond) throw msg; +} + +function prase(expression: string, i: number, terminators: string[], args: string[]): [string, number, string?] { + let ret = ""; + let escaped = false; + + while (i < expression.length) { + const char = expression[i]; + i++; + + if (escaped) { + escaped = false; + ret += char; + continue; + } + + switch (char) { + case "~": // eskejp + escaped = true; + break; + + case "[": // RNG vyber + let option: string, term: string | undefined; + const options: string[] = []; + + do { + [option, i, term] = prase(expression, i, ["|", "]"], args); + options.push(option); + } while (term == "|"); + + if (options.length > 0) + ret += options[rand(options.length)]; + + break; + + case "<": // RNG cislo + let min: string, max: string; + [min, i] = prase(expression, i, [","], args); + [max, i] = prase(expression, i, [">"], args); + + const imin = parseInt(min); + const imax = parseInt(max); + + assume(!Number.isNaN(min), "RNG cislo: Min neni integer"); + assume(!Number.isNaN(max), "RNG cislo: Max neni integer"); + + ret += rand(imax - imin + 1) + imin; + + break; + + case "{": // ARG + let argId: string; + [argId, i] = prase(expression, i, ["}"], args); + + if (argId === "*") { // join args + ret += args.join(" "); + } else if (argId === "#") { // number of args + ret += args.length; + } else { // arg id # + const numArgId = parseInt(argId); + assume(!Number.isNaN(numArgId) && numArgId > 0 && numArgId <= args.length, "ARG: Neplatnej index argumentu"); + ret += args[numArgId - 1]; + } + + break; + + default: + for (const terminator of terminators) { + if (terminator == char) + return [ret, i, char]; + } + + ret += char; + break; + } + } + + assume(terminators.length == 0, "Neocekavanej konec vejrazu"); + + return [ret, expression.length - 1]; +} + +function renderMessage(expression: string, args: string[]) { + if (!expression.toLowerCase().startsWith(DJ_PREFIX)) + return expression; + + expression = expression.substring(DJ_PREFIX.length); + + try { + return prase(expression, 0, [], args)[0]; + } catch (ex) { + return `Chyba: ${ex}`; + } +} + + async function runKomand(mes: Message, cmd: Komand, cmdName: string, arg: string) { if (cmd.cd) { const zbyva = Math.round(maKuldan(mes.author.id, cmdName, cmd.cd)); @@ -121,7 +220,7 @@ async function runKomand(mes: Message, cmd: Komand, cmdName: string, arg: string } const akce = cmd.run; - if (typeof akce == "string") return void mes.channel.send(akce); + if (typeof akce == "string") return void mes.channel.send(renderMessage(akce, arg.split(" "))); try { const result = await akce(mes, arg); if (result) mes.channel.send(result); diff --git a/src/modules/komRNG.ts b/src/modules/komRNG.ts index d937434..2200391 100644 --- a/src/modules/komRNG.ts +++ b/src/modules/komRNG.ts @@ -1,13 +1,12 @@ // Komandy, který jenom pošlou random hovno a jsou nějakým způsobem ovlivněný RNG import { Modul } from "../utils/types"; +import { rand } from "../utils/utils"; const ftipy: string[] = require("../../res/ftipy.json"); const mista = ["na šroťák", "na vrakoviště", "na smetiště", "do kontejneru", "na skládku", "do kriminálu", "pod most", "do sběru", "do hospody", "do najt klubu", "na folmavu"]; const uz = ["ne", "jeste ne", "jiz brzy", "za chvili", "vubec", "nikdy", "za dlouho", "za 5 let", "zejtra", "davno", "jo", "mozna"]; -const rand = (max: number) => Math.floor(Math.random() * max); - const choose = (arr: string[]) => arr[rand(arr.length)]; const exp: Modul = { diff --git a/src/utils/helpServer.ts b/src/utils/helpServer.ts index 11b97ee..16c28a0 100644 --- a/src/utils/helpServer.ts +++ b/src/utils/helpServer.ts @@ -1,6 +1,5 @@ import { createServer } from "http"; import { KomandNaExport, SRecord } from "./types"; -import { log } from "./utils"; createServer((_, res) => { const komandy: KomandNaExport[] = module.exports.komandy; diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 2f416f1..1756b2d 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -194,3 +194,5 @@ export function log(...content: (string | Error)[]) { export async function sendDM(user: User, txt: string) { user.send(txt).catch(() => console.log(`dementovi ${user} nejde poslat DM`)); } + +export const rand = (max: number) => Math.floor(Math.random() * max);