From 03b3b9189e0a6456f24017138d740d3aa30ffd73 Mon Sep 17 00:00:00 2001 From: Supositware Date: Sun, 28 Aug 2022 17:04:51 +0200 Subject: [PATCH] rand text thigny --- utils/rand.js | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 utils/rand.js diff --git a/utils/rand.js b/utils/rand.js new file mode 100644 index 0000000..2bddfa0 --- /dev/null +++ b/utils/rand.js @@ -0,0 +1,69 @@ +import fs from 'node:fs'; +export function rand(text, message) { + // Find a value in an array of objects in Javascript - https://stackoverflow.com/a/12462387 + function search(nameKey, myArray) { + for (let i = 0; i < myArray.length; i++) { + if (new RegExp(myArray[i].name).test(nameKey)) { + return myArray[i]; + } + } + } + + + fs.readdirSync('./json/dictionary/').forEach(file => { + file = file.slice(0, -5); + const dictionary = JSON.parse(fs.readFileSync(`./json/dictionary/${file}.json`)); + const re = new RegExp('\\[' + file + '\\]'); + do { + text = text.replace(re, dictionary[Math.floor((Math.random() * dictionary.length))]); + } while (text.includes(`[${file}]`)); + return text; + }); + + const variables = [ + { + name: /\[author\]/, + value: message ? message.author.username : '', + }, + { + name: /\[member\]/, + value: message ? message.guild ? message.guild.members.cache.random().user.username : '' : '', + }, + { + name: /\[memberRand\]/, + value: (() => message.guild ? message.guild.members.cache.random().user.username : ''), + }, + { + name: /\[dice\d*\]/, + value: (() => Math.floor((Math.random() * text.match(/\[dice\d*\]/g)[0].replace(/\D/g, '')) + 1)), + }, + { + name: /\[number\]/, + value: (() => Math.floor((Math.random() * 9) + 1)), + }, + { + name: /\[kick\]/, + value: '', + }, + { + name: /\[ban\]/, + value: '', + }, + { + name: /\[delete\]/, + value: '', + }, + { + name: /\[n\]/, + value: '\n', + }, + ]; + + const matches = text.matchAll(/\[.*?\]\s?/g); + + for (const match of matches) { + if (search(match[0].trim(), variables)) { text = text.replace(match[0].trim(), search(match[0].trim(), variables).value); } + } + + return text; +}