Haha-Yes/rand.js

70 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-02-08 18:59:18 +01:00
exports.random = function (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];
}
}
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 {
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;
});
let variables = [
{
name: /\[author\]/,
value: message ? message.author.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'
2019-05-09 21:28:58 +02:00
}
];
let matches = text.matchAll(/\[.*?\]\s?/g);
2020-07-11 01:24:41 +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
};