From e58dbec3de2c7ab78d474fccb33cc8601ff0e6bd Mon Sep 17 00:00:00 2001 From: Cohee Date: Sun, 16 Jul 2023 22:43:54 +0300 Subject: [PATCH] Slash command to delete messages by name --- public/scripts/slash-commands.js | 36 ++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index 3e53feb7b..2754fdd48 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -20,6 +20,7 @@ import { replaceCurrentChat, setCharacterId, generateQuietPrompt, + reloadCurrentChat, } from "../script.js"; import { humanizedDateTime } from "./RossAscends-mods.js"; import { resetSelectedGroup } from "./group-chats.js"; @@ -125,11 +126,46 @@ parser.addCommand('flat', setFlatModeCallback, ['default'], ' – sets the messa parser.addCommand('continue', continueChatCallback, ['cont'], ' – continues the last message in the chat', true, true); parser.addCommand('go', goToCharacterCallback, ['char'], '(name) – opens up a chat with the character by its name', true, true); parser.addCommand('sysgen', generateSystemMessage, [], '(prompt) – generates a system message using a specified prompt', true, true); +parser.addCommand('delname', deleteMessagesByNameCallback, ['cancel'], '(name) – deletes all messages attributed to a specified name', true, true); const NARRATOR_NAME_KEY = 'narrator_name'; const NARRATOR_NAME_DEFAULT = 'System'; const COMMENT_NAME_DEFAULT = 'Note'; +async function deleteMessagesByNameCallback(_, name) { + if (!name) { + console.warn('WARN: No name provided for /delname command'); + return; + } + + name = name.trim(); + + const messagesToDelete = []; + chat.forEach((value) => { + if (value.name === name) { + messagesToDelete.push(value); + } + }); + + if (!messagesToDelete.length) { + console.debug('/delname: Nothing to delete'); + return; + } + + for (const message of messagesToDelete) { + const index = chat.indexOf(message); + if (index !== -1) { + console.debug(`/delname: Deleting message #${index}`, message); + chat.splice(index, 1); + } + } + + await saveChatConditional(); + await reloadCurrentChat(); + + toastr.info(`Deleted ${messagesToDelete.length} messages from ${name}`); +} + function findCharacterIndex(name) { const matchTypes = [ (a, b) => a === b,