Move macros replacement to script execution time

This commit is contained in:
Cohee 2023-11-24 01:56:43 +02:00
parent 461e8d7929
commit 74fbc88d7d
2 changed files with 20 additions and 10 deletions

View File

@ -229,14 +229,14 @@ async function performQuickReply(prompt, index) {
newText = `${prompt} `; newText = `${prompt} `;
} }
newText = substituteParams(newText);
// the prompt starts with '/' - execute slash commands natively // the prompt starts with '/' - execute slash commands natively
if (prompt.startsWith('/')) { if (prompt.startsWith('/')) {
await executeSlashCommands(newText); await executeSlashCommands(newText);
return; return;
} }
newText = substituteParams(newText);
$("#send_textarea").val(newText); $("#send_textarea").val(newText);
// Set the focus back to the textarea // Set the focus back to the textarea
@ -560,23 +560,29 @@ function saveQROrder() {
}); });
} }
async function onMessageReceived() { async function onMessageReceived(index) {
for (let i = 0; i < extension_settings.quickReply.numberOfSlots; i++) { for (let i = 0; i < extension_settings.quickReply.numberOfSlots; i++) {
const qr = extension_settings.quickReply.quickReplySlots[i]; const qr = extension_settings.quickReply.quickReplySlots[i];
if (qr?.autoExecute_botMessage) { if (qr?.autoExecute_botMessage) {
const message = getContext().chat[index];
if (message?.mes && message?.mes !== '...') {
await sendQuickReply(i); await sendQuickReply(i);
} }
} }
} }
}
async function onMessageSent() { async function onMessageSent(index) {
for (let i = 0; i < extension_settings.quickReply.numberOfSlots; i++) { for (let i = 0; i < extension_settings.quickReply.numberOfSlots; i++) {
const qr = extension_settings.quickReply.quickReplySlots[i]; const qr = extension_settings.quickReply.quickReplySlots[i];
if (qr?.autoExecute_userMessage) { if (qr?.autoExecute_userMessage) {
const message = getContext().chat[index];
if (message?.mes && message?.mes !== '...') {
await sendQuickReply(i); await sendQuickReply(i);
} }
} }
} }
}
jQuery(async () => { jQuery(async () => {
moduleWorker(); moduleWorker();

View File

@ -86,7 +86,7 @@ class SlashCommandParser {
const key = match[1]; const key = match[1];
const value = match[2]; const value = match[2];
// Remove the quotes around the value, if any // Remove the quotes around the value, if any
argObj[key] = substituteParams(value.replace(/(^")|("$)/g, '')); argObj[key] = value.replace(/(^")|("$)/g, '');
} }
// Match unnamed argument // Match unnamed argument
@ -1048,11 +1048,15 @@ async function executeSlashCommands(text, unescape = false) {
console.debug('Slash command executing:', result); console.debug('Slash command executing:', result);
let unnamedArg = result.value || pipeResult; let unnamedArg = result.value || pipeResult;
if (pipeResult && typeof result.args === 'object') { if (typeof result.args === 'object') {
for (const [key, value] of Object.entries(result.args)) { for (const [key, value] of Object.entries(result.args)) {
if (typeof value === 'string' && /{{pipe}}/i.test(value)) { if (typeof value === 'string') {
if (pipeResult && /{{pipe}}/i.test(value)) {
result.args[key] = value.replace(/{{pipe}}/i, pipeResult); result.args[key] = value.replace(/{{pipe}}/i, pipeResult);
} }
result.args[key] = substituteParams(value.trim());
}
} }
} }