Add STscript procedure calls with /run command

This commit is contained in:
Cohee 2023-11-24 15:58:00 +02:00
parent dd17c2483f
commit c9b3ccc585
2 changed files with 41 additions and 3 deletions

View File

@ -221,8 +221,25 @@ async function onAutoInputInject() {
async function sendQuickReply(index) {
const prompt = extension_settings.quickReply.quickReplySlots[index]?.mes || '';
await performQuickReply(prompt, index);
return await performQuickReply(prompt, index);
}
async function executeQuickReplyByName(name) {
if (!extension_settings.quickReply.quickReplyEnabled) {
throw new Error('Quick Reply is disabled');
}
const qr = extension_settings.quickReply.quickReplySlots.find(x => x.label == name);
if (!qr) {
throw new Error(`Quick Reply "${name}" not found`);
}
return await performQuickReply(qr.mes);
}
window['executeQuickReplyByName'] = executeQuickReplyByName;
async function performQuickReply(prompt, index) {
if (!prompt) {
console.warn(`Quick reply slot ${index} is empty! Aborting.`);
@ -245,8 +262,8 @@ async function performQuickReply(prompt, index) {
// the prompt starts with '/' - execute slash commands natively
if (prompt.startsWith('/')) {
await executeSlashCommands(newText);
return;
const result = await executeSlashCommands(newText);
return result?.pipe;
}
newText = substituteParams(newText);

View File

@ -171,12 +171,33 @@ parser.addCommand('fuzzy', fuzzyCallback, [], 'list=["a","b","c"] (search value)
parser.addCommand('pass', (_, arg) => arg, [], '<span class="monospace">(text)</span> passes the text to the next command through the pipe.', true, true);
parser.addCommand('delay', delayCallback, ['wait', 'sleep'], '<span class="monospace">(milliseconds)</span> delays the next command in the pipe by the specified number of milliseconds.', true, true);
parser.addCommand('input', inputCallback, ['prompt'], '<span class="monospace">(prompt)</span> shows a popup with the provided prompt and passes the user input to the next command through the pipe.', true, true);
parser.addCommand('run', runCallback, ['call', 'exec'], '<span class="monospace">(QR label)</span> runs a Quick Reply with the specified name from the current preset.', true, true);
registerVariableCommands();
const NARRATOR_NAME_KEY = 'narrator_name';
const NARRATOR_NAME_DEFAULT = 'System';
export const COMMENT_NAME_DEFAULT = 'Note';
async function runCallback(_, name) {
if (!name) {
toastr.warning('No name provided for /run command');
return '';
}
if (typeof window['executeQuickReplyByName'] !== 'function') {
toastr.warning('Quick Reply extension is not loaded');
return '';
}
try {
name = name.trim();
return await window['executeQuickReplyByName'](name);
} catch (error) {
toastr.error(`Error running Quick Reply "${name}": ${error.message}`, 'Error');
return '';
}
}
function abortCallback() {
$('#send_textarea').val('');
throw new Error('/abort command executed');