#1940 Allow both random syntaxes in one message

This commit is contained in:
Cohee 2024-03-23 00:57:33 +02:00
parent a645889455
commit 76cde592ad
1 changed files with 21 additions and 25 deletions

View File

@ -185,31 +185,27 @@ function randomReplace(input, emptyListPlaceholder = '') {
const randomPatternNew = /{{random\s?::\s?([^}]+)}}/gi; const randomPatternNew = /{{random\s?::\s?([^}]+)}}/gi;
const randomPatternOld = /{{random\s?:\s?([^}]+)}}/gi; const randomPatternOld = /{{random\s?:\s?([^}]+)}}/gi;
if (randomPatternNew.test(input)) { input = input.replace(randomPatternNew, (match, listString) => {
return input.replace(randomPatternNew, (match, listString) => {
//split on double colons instead of commas to allow for commas inside random items //split on double colons instead of commas to allow for commas inside random items
const list = listString.split('::').filter(item => item.length > 0); const list = listString.split('::').filter(item => item.length > 0);
if (list.length === 0) { if (list.length === 0) {
return emptyListPlaceholder; return emptyListPlaceholder;
} }
var rng = new Math.seedrandom('added entropy.', { entropy: true }); const rng = new Math.seedrandom('added entropy.', { entropy: true });
const randomIndex = Math.floor(rng() * list.length); const randomIndex = Math.floor(rng() * list.length);
//trim() at the end to allow for empty random values //trim() at the end to allow for empty random values
return list[randomIndex].trim(); return list[randomIndex].trim();
}); });
} else if (randomPatternOld.test(input)) { input = input.replace(randomPatternOld, (match, listString) => {
return input.replace(randomPatternOld, (match, listString) => {
const list = listString.split(',').map(item => item.trim()).filter(item => item.length > 0); const list = listString.split(',').map(item => item.trim()).filter(item => item.length > 0);
if (list.length === 0) { if (list.length === 0) {
return emptyListPlaceholder; return emptyListPlaceholder;
} }
var rng = new Math.seedrandom('added entropy.', { entropy: true }); const rng = new Math.seedrandom('added entropy.', { entropy: true });
const randomIndex = Math.floor(rng() * list.length); const randomIndex = Math.floor(rng() * list.length);
return list[randomIndex]; return list[randomIndex];
}); });
} else {
return input; return input;
}
} }
function diceRollReplace(input, invalidRollPlaceholder = '') { function diceRollReplace(input, invalidRollPlaceholder = '') {