diff --git a/public/script.js b/public/script.js index 98044e233..198e44c78 100644 --- a/public/script.js +++ b/public/script.js @@ -5431,70 +5431,91 @@ export function setSendButtonState(value) { is_send_press = value; } -async function renameCharacter() { +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 = await callPopup('
Past chats will still contain the old character name. Would you like to update the character name in previous chats as well?
- Sprites folder (if any) should be renamed manually.`, 'confirm'); - - if (renamePastChatsConfirm) { - await renamePastChats(newAvatar, newValue); - await reloadCurrentChat(); - toastr.success('Character renamed and past chats updated!'); - } + if (this_chid === -1) { + throw new Error('New character not selected'); } - else { - throw new Error('Newly renamed character was lost?'); + + // Also rename as a group member + await renameGroupMember(oldAvatar, newAvatar, newValue); + const renamePastChatsConfirm = renameChats !== null ? renameChats + : silent ? false : await callPopup(`Past chats will still contain the old character name. Would you like to update the character name in previous chats as well?
+ Sprites folder (if any) should be renamed manually.`, 'confirm'); + + if (renamePastChatsConfirm) { + await renamePastChats(newAvatar, newValue); + await reloadCurrentChat(); + toastr.success('Character renamed and past chats updated!', 'Rename Character'); + } else { + toastr.success('Character renamed!', 'Rename Character'); } } else { - throw new Error('Could not rename the character'); + throw new Error('Newly renamed character was lost?'); } } - catch { - // Reloading to prevent data corruption - await callPopup('Something went wrong. The page will be reloaded.', 'text'); - location.reload(); + else { + throw new Error('Could not rename the character'); } } + catch (error) { + // Reloading to prevent data corruption + 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) { diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index 5baa11435..c910a20bf 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -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, '