Replace one-time bias regex parser with Handlebars helper

This commit is contained in:
Cohee 2023-08-22 16:14:21 +03:00
parent c7ce6a4953
commit 1abb739a8c

View File

@ -1937,34 +1937,24 @@ export function extractMessageBias(message) {
return null;
}
const forbiddenMatches = ['user', 'char', 'time', 'date', 'random', 'idle_duration', 'roll'];
const found = [];
const rxp = /\{\{([\s\S]+?)\}\}/gm;
//const rxp = /{([^}]+)}/g;
let curMatch;
try {
const biasHandlebars = Handlebars.create();
const biasMatches = [];
biasHandlebars.registerHelper('bias', function (text) {
biasMatches.push(text);
return '';
});
const template = biasHandlebars.compile(message);
template({});
while ((curMatch = rxp.exec(message))) {
const match = curMatch[1].trim();
// Ignore random/roll pattern matches
if (/^random[ : ].+/i.test(match) || /^roll[ : ].+/i.test(match)) {
continue;
if (biasMatches && biasMatches.length > 0) {
return ` ${biasMatches.join(" ")}`;
}
if (forbiddenMatches.includes(match.toLowerCase())) {
continue;
return '';
} catch {
return '';
}
found.push(match);
}
let biasString = '';
if (found.length) {
biasString = ` ${found.join(" ")}`
}
return biasString;
}
function cleanGroupMessage(getMessage) {