From 76cde592ade8cfa061f52a35165ea3b3c2d34100 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sat, 23 Mar 2024 00:57:33 +0200 Subject: [PATCH] #1940 Allow both random syntaxes in one message --- public/scripts/macros.js | 46 ++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/public/scripts/macros.js b/public/scripts/macros.js index e8954f874..63da09b0c 100644 --- a/public/scripts/macros.js +++ b/public/scripts/macros.js @@ -185,31 +185,27 @@ function randomReplace(input, emptyListPlaceholder = '') { const randomPatternNew = /{{random\s?::\s?([^}]+)}}/gi; const randomPatternOld = /{{random\s?:\s?([^}]+)}}/gi; - if (randomPatternNew.test(input)) { - return input.replace(randomPatternNew, (match, listString) => { - //split on double colons instead of commas to allow for commas inside random items - const list = listString.split('::').filter(item => item.length > 0); - if (list.length === 0) { - return emptyListPlaceholder; - } - var rng = new Math.seedrandom('added entropy.', { entropy: true }); - const randomIndex = Math.floor(rng() * list.length); - //trim() at the end to allow for empty random values - return list[randomIndex].trim(); - }); - } else if (randomPatternOld.test(input)) { - return input.replace(randomPatternOld, (match, listString) => { - const list = listString.split(',').map(item => item.trim()).filter(item => item.length > 0); - if (list.length === 0) { - return emptyListPlaceholder; - } - var rng = new Math.seedrandom('added entropy.', { entropy: true }); - const randomIndex = Math.floor(rng() * list.length); - return list[randomIndex]; - }); - } else { - return input; - } + input = input.replace(randomPatternNew, (match, listString) => { + //split on double colons instead of commas to allow for commas inside random items + const list = listString.split('::').filter(item => item.length > 0); + if (list.length === 0) { + return emptyListPlaceholder; + } + const rng = new Math.seedrandom('added entropy.', { entropy: true }); + const randomIndex = Math.floor(rng() * list.length); + //trim() at the end to allow for empty random values + return list[randomIndex].trim(); + }); + input = input.replace(randomPatternOld, (match, listString) => { + const list = listString.split(',').map(item => item.trim()).filter(item => item.length > 0); + if (list.length === 0) { + return emptyListPlaceholder; + } + const rng = new Math.seedrandom('added entropy.', { entropy: true }); + const randomIndex = Math.floor(rng() * list.length); + return list[randomIndex]; + }); + return input; } function diceRollReplace(input, invalidRollPlaceholder = '') {