2022-08-28 17:04:51 +02:00
|
|
|
import fs from 'node:fs';
|
2022-09-23 19:45:59 +02:00
|
|
|
export function rand(text, interaction) {
|
2022-10-10 19:07:53 +02:00
|
|
|
if (interaction) {
|
|
|
|
interaction.author = interaction.user;
|
|
|
|
}
|
2022-08-28 17:04:51 +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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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\]/,
|
2022-10-16 20:09:59 +02:00
|
|
|
value: interaction ? interaction.author ? interaction.author.username : '' : '',
|
2022-08-28 17:04:51 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: /\[member\]/,
|
2022-09-23 19:45:59 +02:00
|
|
|
value: interaction ? interaction.guild ? interaction.guild.members.cache.random().user.username : '' : '',
|
2022-08-28 17:04:51 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: /\[memberRand\]/,
|
2022-09-23 19:45:59 +02:00
|
|
|
value: (() => interaction.guild ? interaction.guild.members.cache.random().user.username : ''),
|
2022-08-28 17:04:51 +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\]/,
|
2023-04-04 19:51:36 +02:00
|
|
|
value: '[This used to kick you but no more!]',
|
2022-08-28 17:04:51 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: /\[ban\]/,
|
2023-04-04 19:51:36 +02:00
|
|
|
value: '[This used to ban you but no more!]',
|
2022-08-28 17:04:51 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|