New syntax for sendas command

This commit is contained in:
Cohee 2023-11-21 02:54:04 +02:00
parent 52d9855916
commit 01b629bd49
2 changed files with 37 additions and 23 deletions

View File

@ -77,21 +77,22 @@ class SlashCommandParser {
let unnamedArg; let unnamedArg;
if (args.length > 0) { if (args.length > 0) {
const argsArray = args.split(' '); // Match named arguments
for (let arg of argsArray) { const namedArgPattern = /(\w+)=("(?:\\.|[^"\\])*"|\S+)/g;
const equalsIndex = arg.indexOf('='); let match;
if (equalsIndex !== -1) { while ((match = namedArgPattern.exec(args)) !== null) {
const key = arg.substring(0, equalsIndex); const key = match[1];
const value = arg.substring(equalsIndex + 1); const value = match[2];
// Replace "wrapping quotes" used for escaping spaces // Remove the quotes around the value, if any
argObj[key] = value.replace(/(^")|("$)/g, ''); argObj[key] = value.replace(/(^")|("$)/g, '');
}
else {
break;
}
} }
unnamedArg = argsArray.slice(Object.keys(argObj).length).join(' '); // Match unnamed argument
const unnamedArgPattern = /(?:\w+=(?:"(?:\\.|[^"\\])*"|\S+)\s*)*(.*)/s;
match = unnamedArgPattern.exec(args);
if (match !== null) {
unnamedArg = match[1].trim();
}
// Excluded commands format in their own function // Excluded commands format in their own function
if (!excludedFromRegex.includes(command)) { if (!excludedFromRegex.includes(command)) {
@ -132,7 +133,7 @@ parser.addCommand('name', setNameCallback, ['persona'], '<span class="monospace"
parser.addCommand('sync', syncCallback, [], ' syncs user name in user-attributed messages in the current chat', true, true); parser.addCommand('sync', syncCallback, [], ' syncs user name in user-attributed messages in the current chat', true, true);
parser.addCommand('lock', bindCallback, ['bind'], ' locks/unlocks a persona (name and avatar) to the current chat', true, true); parser.addCommand('lock', bindCallback, ['bind'], ' locks/unlocks a persona (name and avatar) to the current chat', true, true);
parser.addCommand('bg', setBackgroundCallback, ['background'], '<span class="monospace">(filename)</span> sets a background according to filename, partial names allowed', false, true); parser.addCommand('bg', setBackgroundCallback, ['background'], '<span class="monospace">(filename)</span> sets a background according to filename, partial names allowed', false, true);
parser.addCommand('sendas', sendMessageAs, [], ` sends message as a specific character. Uses character avatar if it exists in the characters list. Example that will send "Hello, guys!" from "Chloe": <pre><code>/sendas Chloe&#10;Hello, guys!</code></pre>`, true, true); parser.addCommand('sendas', sendMessageAs, [], ` sends message as a specific character. Uses character avatar if it exists in the characters list. Example that will send "Hello, guys!" from "Chloe": <code>/sendas name="Chloe" Hello, guys!</code>`, true, true);
parser.addCommand('sys', sendNarratorMessage, ['nar'], '<span class="monospace">(text)</span> sends message as a system narrator', false, true); parser.addCommand('sys', sendNarratorMessage, ['nar'], '<span class="monospace">(text)</span> sends message as a system narrator', false, true);
parser.addCommand('sysname', setNarratorName, [], '<span class="monospace">(name)</span> sets a name for future system narrator messages in this chat (display only). Default: System. Leave empty to reset.', true, true); parser.addCommand('sysname', setNarratorName, [], '<span class="monospace">(name)</span> sets a name for future system narrator messages in this chat (display only). Default: System. Leave empty to reset.', true, true);
parser.addCommand('comment', sendCommentMessage, [], '<span class="monospace">(text)</span> adds a note/comment message not part of the chat', false, true); parser.addCommand('comment', sendCommentMessage, [], '<span class="monospace">(text)</span> adds a note/comment message not part of the chat', false, true);
@ -499,19 +500,32 @@ async function setNarratorName(_, text) {
await saveChatConditional(); await saveChatConditional();
} }
export async function sendMessageAs(_, text) { export async function sendMessageAs(namedArgs, text) {
if (!text) { if (!text) {
return; return;
} }
const parts = text.split('\n'); let name;
if (parts.length <= 1) { let mesText;
toastr.warning('Both character name and message are required. Separate them with a new line.');
return;
}
const name = parts.shift().trim(); if (namedArgs.name) {
let mesText = parts.join('\n').trim(); name = namedArgs.name.trim();
mesText = text.trim();
if (!name && !text) {
toastr.warning('You must specify a name and text to send as');
return;
}
} else {
const parts = text.split('\n');
if (parts.length <= 1) {
toastr.warning('Both character name and message are required. Separate them with a new line.');
return;
}
name = parts.shift().trim();
mesText = parts.join('\n').trim();
}
// Requires a regex check after the slash command is pushed to output // Requires a regex check after the slash command is pushed to output
mesText = getRegexedString(mesText, regex_placement.SLASH_COMMAND, { characterOverride: name }); mesText = getRegexedString(mesText, regex_placement.SLASH_COMMAND, { characterOverride: name });

View File

@ -218,7 +218,7 @@ table.responsiveTable {
color: var(--white50a); color: var(--white50a);
} }
.mes[is_system="true"] .mes_text br { .mes[is_system="true"][ch_name="SillyTavern System"] .mes_text br {
display: none; display: none;
} }