/rename-char slash command

- Rename char slash command (with optional parameters for silent and rename old chats)
- JSDoc fix for STscript named args in callbacks
This commit is contained in:
Wolfsblvt
2024-06-05 02:19:22 +02:00
parent 6bed373f0a
commit 858e5f2efb
3 changed files with 97 additions and 47 deletions

View File

@ -5431,11 +5431,24 @@ export function setSendButtonState(value) {
is_send_press = value;
}
async function renameCharacter() {
const oldAvatar = characters[this_chid].avatar;
const newValue = await callPopup('<h3>New name:</h3>', 'input', characters[this_chid].name);
export async function renameCharacter(name = null, { silent = false, renameChats = null } = {}) {
if (!name && silent) {
toastr.warning('No character name provided.', 'Rename Character');
return false;
}
const oldAvatar = characters[this_chid].avatar;
const newValue = name || await callPopup('<h3>New name:</h3>', 'input', characters[this_chid].name);
if (!newValue) {
toastr.warning('No character name provided.', 'Rename Character');
return false;
}
if (newValue === characters[this_chid].name) {
toastr.info('Same character name provided, so name did not change.', 'Rename Character');
return false;
}
if (newValue && newValue !== characters[this_chid].name) {
const body = JSON.stringify({ avatar_url: oldAvatar, new_name: newValue });
const response = await fetch('/api/characters/rename', {
method: 'POST',
@ -5471,14 +5484,17 @@ async function renameCharacter() {
// Also rename as a group member
await renameGroupMember(oldAvatar, newAvatar, newValue);
const renamePastChatsConfirm = await callPopup(`<h3>Character renamed!</h3>
const renamePastChatsConfirm = renameChats !== null ? renameChats
: silent ? false : await callPopup(`<h3>Character renamed!</h3>
<p>Past chats will still contain the old character name. Would you like to update the character name in previous chats as well?</p>
<i><b>Sprites folder (if any) should be renamed manually.</b></i>`, 'confirm');
if (renamePastChatsConfirm) {
await renamePastChats(newAvatar, newValue);
await reloadCurrentChat();
toastr.success('Character renamed and past chats updated!');
toastr.success('Character renamed and past chats updated!', 'Rename Character');
} else {
toastr.success('Character renamed!', 'Rename Character');
}
}
else {
@ -5489,12 +5505,17 @@ async function renameCharacter() {
throw new Error('Could not rename the character');
}
}
catch {
catch (error) {
// Reloading to prevent data corruption
await callPopup('Something went wrong. The page will be reloaded.', 'text');
if (!silent) await callPopup('Something went wrong. The page will be reloaded.', 'text');
else toastr.error('Something went wrong. The page will be reloaded.', 'Rename Character');
console.log('Renaming character error:', error);
location.reload();
return false;
}
}
return true;
}
async function renamePastChats(newAvatar, newValue) {

View File

@ -23,6 +23,7 @@ import {
name2,
reloadCurrentChat,
removeMacros,
renameCharacter,
saveChatConditional,
sendMessageAsUser,
sendSystemMessage,
@ -323,6 +324,29 @@ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
helpString: 'Opens up a chat with the character or group by its name',
aliases: ['char'],
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'rename-char',
/** @param {{silent: string, renameChats: string}} options @param {string} name */
callback: async ({ silent = 'true', renameChats = null }, name) => {
const renamed = await renameCharacter(name, { silent: silent === 'true', renameChats: renameChats !== null ? renameChats === 'true' : null });
return String(renamed);
},
returns: 'true/false - Whether the rename was successful',
namedArgumentList: [
new SlashCommandNamedArgument(
'silent', 'Hide any blocking popups. (if false, the name is optional. If not supplied, a popup asking for it will appear)', [ARGUMENT_TYPE.BOOLEAN], false, false, 'true'
),
new SlashCommandNamedArgument(
'renameChats', 'Rename char in all previous chats', [ARGUMENT_TYPE.BOOLEAN], false, false, '<null>'
),
],
unnamedArgumentList: [
new SlashCommandArgument(
'new char name', [ARGUMENT_TYPE.STRING], true,
),
],
helpString: 'Renames the current character.'
}));
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'sysgen',
callback: generateSystemMessage,

View File

@ -17,6 +17,11 @@ import { SlashCommandScope } from './SlashCommandScope.js';
* }} NamedArguments
*/
/**
* Alternative object for local JSDocs, where you don't need existing pipe, scope, etc. arguments
* @typedef {{[id:string]:string|SlashCommandClosure}} NamedArgumentsCapture
*/
/**
* @typedef {string|SlashCommandClosure|(string|SlashCommandClosure)[]} UnnamedArguments
*/
@ -28,7 +33,7 @@ export class SlashCommand {
* Creates a SlashCommand from a properties object.
* @param {Object} props
* @param {string} [props.name]
* @param {(namedArguments:NamedArguments, unnamedArguments:string|SlashCommandClosure|(string|SlashCommandClosure)[])=>string|SlashCommandClosure|void|Promise<string|SlashCommandClosure|void>} [props.callback]
* @param {(namedArguments:NamedArguments|NamedArgumentsCapture, unnamedArguments:string|SlashCommandClosure|(string|SlashCommandClosure)[])=>string|SlashCommandClosure|void|Promise<string|SlashCommandClosure|void>} [props.callback]
* @param {string} [props.helpString]
* @param {boolean} [props.splitUnnamedArgument]
* @param {string[]} [props.aliases]