This is now WAY more acceptable than the shit it used to be

Signed-off-by: loicbersier <loic.bersier1@gmail.com>
This commit is contained in:
loicbersier 2020-07-21 21:11:24 +02:00
parent 4891189daf
commit 36f3a9e0b0

70
rand.js
View file

@ -1,8 +1,11 @@
exports.random = function (text, message) { exports.random = function (text, message) {
// Generate a random number // Find a value in an array of objects in Javascript - https://stackoverflow.com/a/12462387
function randNumber(file) { function search(nameKey, myArray){
let Rand = Math.floor((Math.random() * file.length)); for (let i=0; i < myArray.length; i++) {
return Rand; if (new RegExp(myArray[i].name).test(nameKey)) {
return myArray[i];
}
}
} }
const fs = require('fs'); const fs = require('fs');
@ -12,28 +15,55 @@ exports.random = function (text, message) {
const dictionary = require(`./dictionary/${file}`); const dictionary = require(`./dictionary/${file}`);
const re = new RegExp('\\[' + file + '\\]'); const re = new RegExp('\\[' + file + '\\]');
do { do {
text = text.replace(re, dictionary[randNumber(dictionary)]); text = text.replace(re, dictionary[Math.floor((Math.random() * dictionary.length))]);
} while(text.includes(`[${file}]`)); } while(text.includes(`[${file}]`));
return text; return text;
}); });
do { let variables = [
if (message) { {
if (message.member) { name: /\[author\]/,
text = text.replace(/\[author\]/, message.author.username); value: message ? message.author.username : ''
text = text.replace(/\[member\]/g, message.guild.members.cache.random().user.username); },
text = text.replace(/\[memberRand\]/, message.guild.members.cache.random().user.username); {
} name: /\[member\]/,
value: message ? message.guild.members.cache.random().user.username : ''
},
{
name: /\[memberRand\]/,
value: (() => message ? 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'
} }
];
text = text.replace(/\[dice\]/, Math.floor((Math.random() * 6) + 1)); let matches = text.matchAll(/\[.*?\]\s?/g);
text = text.replace(/\[number\]/, Math.floor((Math.random() * 9) + 1));
text = text.replace(/\[kick\]/, ' '); for (const match of matches)
text = text.replace(/\[ban\]/, ' '); if (search(match[0].trim(), variables))
text = text.replace(/\[delete\]/, ' '); text = text.replace(match[0].trim(), search(match[0].trim(), variables).value);
text = text.replace(/\{n\}/, '\n');
// Verify if it replaced everything
} while( text.includes('[member]') || text.includes('[memberRand]') || text.includes('[number]') || text.includes('[author]') || text.includes('[kick]') || text.includes('[ban]') || text.includes('{n}' || text.includes('[delete]')));
return text; return text;
}; };