2019-02-08 18:59:18 +01:00
|
|
|
exports.random = function (text, message) {
|
2020-07-21 21:11:24 +02:00
|
|
|
// 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];
|
|
|
|
}
|
|
|
|
}
|
2019-02-08 18:59:18 +01:00
|
|
|
}
|
2019-03-29 05:21:58 +01:00
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
fs.readdirSync('./dictionary/').forEach(file => {
|
|
|
|
file = file.slice(0, -5);
|
|
|
|
const dictionary = require(`./dictionary/${file}`);
|
|
|
|
const re = new RegExp('\\[' + file + '\\]');
|
|
|
|
do {
|
2020-07-21 21:11:24 +02:00
|
|
|
text = text.replace(re, dictionary[Math.floor((Math.random() * dictionary.length))]);
|
2019-03-29 05:21:58 +01:00
|
|
|
} while(text.includes(`[${file}]`));
|
|
|
|
return text;
|
|
|
|
});
|
|
|
|
|
2020-07-21 21:11:24 +02:00
|
|
|
let variables = [
|
|
|
|
{
|
|
|
|
name: /\[author\]/,
|
|
|
|
value: message ? message.author.username : ''
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: /\[member\]/,
|
2020-07-28 16:00:43 +02:00
|
|
|
value: message ? message.guild ? message.guild.members.cache.random().user.username : '' : '' // What the fuck am i doing
|
2020-07-21 21:11:24 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: /\[memberRand\]/,
|
2020-07-23 21:18:29 +02:00
|
|
|
value: (() => message.guild ? message.guild.members.cache.random().user.username : '')
|
2020-07-21 21:11:24 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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'
|
2019-05-09 21:28:58 +02:00
|
|
|
}
|
2020-07-21 21:11:24 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
let matches = text.matchAll(/\[.*?\]\s?/g);
|
2020-07-11 01:24:41 +02:00
|
|
|
|
2020-07-21 21:11:24 +02:00
|
|
|
for (const match of matches)
|
|
|
|
if (search(match[0].trim(), variables))
|
|
|
|
text = text.replace(match[0].trim(), search(match[0].trim(), variables).value);
|
2019-02-08 18:59:18 +01:00
|
|
|
|
|
|
|
return text;
|
2019-02-17 20:15:23 +01:00
|
|
|
};
|