Extend random and roll syntax for frontend compatibility

This commit is contained in:
Cohee 2023-08-08 22:36:42 +03:00
parent ef3a9a810e
commit 2fea218661
2 changed files with 14 additions and 4 deletions

View File

@ -136,6 +136,7 @@ import {
download,
isDataURL,
getCharaFilename,
isDigitsOnly,
} from "./scripts/utils.js";
import { extension_settings, getContext, loadExtensionSettings, runGenerationInterceptors, saveMetadataDebounced } from "./scripts/extensions.js";
@ -1686,7 +1687,7 @@ function getTimeSinceLastMessage() {
}
function randomReplace(input, emptyListPlaceholder = '') {
const randomPattern = /{{random:([^}]+)}}/gi;
const randomPattern = /{{random[ : ]([^}]+)}}/gi;
return input.replace(randomPattern, (match, listString) => {
const list = listString.split(',').map(item => item.trim()).filter(item => item.length > 0);
@ -1704,10 +1705,15 @@ function randomReplace(input, emptyListPlaceholder = '') {
}
function diceRollReplace(input, invalidRollPlaceholder = '') {
const randomPattern = /{{roll:([^}]+)}}/gi;
const randomPattern = /{{roll[ : ]([^}]+)}}/gi;
return input.replace(randomPattern, (match, matchValue) => {
const formula = matchValue.trim();
let formula = matchValue.trim();
if (isDigitsOnly(formula)) {
formula = `1d${formula}`;
}
const isValid = droll.validate(formula);
if (!isValid) {
@ -1844,7 +1850,7 @@ export function extractMessageBias(message) {
const match = curMatch[1].trim();
// Ignore random/roll pattern matches
if (/^random:.+/i.test(match) || /^roll:.+/i.test(match)) {
if (/^random[ : ].+/i.test(match) || /^roll[ : ].+/i.test(match)) {
continue;
}

View File

@ -4,6 +4,10 @@ export function onlyUnique(value, index, array) {
return array.indexOf(value) === index;
}
export function isDigitsOnly(str) {
return /^\d+$/.test(str);
}
export function shuffle(array) {
let currentIndex = array.length,
randomIndex;