#629 Random list select substitution

This commit is contained in:
Cohee
2023-07-05 17:59:53 +03:00
parent af6d9d48e0
commit f3ebea6ad8
2 changed files with 39 additions and 18 deletions

View File

@@ -1496,9 +1496,25 @@ function substituteParams(content, _name1, _name2, _original) {
content = content.replace(/<BOT>/gi, _name2); content = content.replace(/<BOT>/gi, _name2);
content = content.replace(/{{time}}/gi, moment().format('LT')); content = content.replace(/{{time}}/gi, moment().format('LT'));
content = content.replace(/{{date}}/gi, moment().format('LL')); content = content.replace(/{{date}}/gi, moment().format('LL'));
content = randomReplace(content);
return content; return content;
} }
function randomReplace(input, emptyListPlaceholder = '') {
const randomPattern = /{{random:([^}]+)}}/g;
return input.replace(randomPattern, (match, listString) => {
const list = listString.split(',').map(item => item.trim()).filter(item => item.length > 0);
if (list.length === 0) {
return emptyListPlaceholder;
}
const randomIndex = Math.floor(Math.random() * list.length);
return list[randomIndex];
});
}
function getStoppingStrings(isImpersonate, addSpace) { function getStoppingStrings(isImpersonate, addSpace) {
const charString = `\n${name2}:`; const charString = `\n${name2}:`;
const youString = `\nYou:`; const youString = `\nYou:`;
@@ -1600,7 +1616,7 @@ export function extractMessageBias(message) {
return null; return null;
} }
const forbiddenMatches = ['user', 'char', 'time', 'date']; const forbiddenMatches = ['user', 'char', 'time', 'date', 'random'];
const found = []; const found = [];
const rxp = /\{\{([\s\S]+?)\}\}/gm; const rxp = /\{\{([\s\S]+?)\}\}/gm;
//const rxp = /{([^}]+)}/g; //const rxp = /{([^}]+)}/g;
@@ -1609,7 +1625,12 @@ export function extractMessageBias(message) {
while ((curMatch = rxp.exec(message))) { while ((curMatch = rxp.exec(message))) {
const match = curMatch[1].trim(); const match = curMatch[1].trim();
if (forbiddenMatches.includes(match)) { // Ignore random pattern matches
if (/^random:.+/i.test(match)) {
continue;
}
if (forbiddenMatches.includes(match.toLowerCase())) {
continue; continue;
} }