Haha-Yes/rand.js

40 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-02-08 18:59:18 +01:00
exports.random = function (text, message) {
// Generate a random number
function randNumber(file) {
let Rand = Math.floor((Math.random() * file.length));
2019-02-08 18:59:18 +01:00
return Rand;
}
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[randNumber(dictionary)]);
} while(text.includes(`[${file}]`));
return text;
});
2019-02-08 18:59:18 +01:00
do {
2019-05-09 21:28:58 +02:00
if (message) {
2020-03-29 15:38:23 +02:00
if (message.member) {
text = text.replace(/\[author\]/, 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);
}
2019-05-09 21:28:58 +02:00
}
2020-07-11 01:24:41 +02:00
text = text.replace(/\[dice\]/, Math.floor((Math.random() * 6) + 1));
2019-02-08 18:59:18 +01:00
text = text.replace(/\[number\]/, Math.floor((Math.random() * 9) + 1));
2019-02-08 20:01:24 +01:00
text = text.replace(/\[kick\]/, ' ');
text = text.replace(/\[ban\]/, ' ');
2019-04-05 19:47:06 +02:00
text = text.replace(/\[delete\]/, ' ');
2019-02-10 02:06:57 +01:00
text = text.replace(/\{n\}/, '\n');
2019-02-08 18:59:18 +01:00
// Verify if it replaced everything
2019-04-05 19:47:06 +02:00
} 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]')));
2019-02-08 18:59:18 +01:00
return text;
2019-02-17 20:15:23 +01:00
};