diff --git a/src/lib/ChatScreens/DefaultChatScreen.svelte b/src/lib/ChatScreens/DefaultChatScreen.svelte index 34685802..22991339 100644 --- a/src/lib/ChatScreens/DefaultChatScreen.svelte +++ b/src/lib/ChatScreens/DefaultChatScreen.svelte @@ -21,8 +21,9 @@ import { downloadFile } from 'src/ts/storage/globalApi'; import { runTrigger } from 'src/ts/process/triggers'; import { v4 } from 'uuid'; - import { postInlayImage } from 'src/ts/image'; - import { PreUnreroll, Prereroll } from 'src/ts/process/prereroll'; + import { postInlayImage } from 'src/ts/image'; + import { PreUnreroll, Prereroll } from 'src/ts/process/prereroll'; + import { processMultiCommand } from 'src/ts/process/command'; let messageInput:string = '' let messageInputTranslate:string = '' @@ -56,6 +57,15 @@ let cha = $DataBase.characters[selectedChar].chats[$DataBase.characters[selectedChar].chatPage].message + if(messageInput.startsWith('/')){ + const commandProcessed = await processMultiCommand(messageInput) + if(commandProcessed !== false){ + messageInput = '' + return + } + } + + if(messageInput === ''){ if($DataBase.characters[selectedChar].type !== 'group'){ if(cha.length === 0 || cha[cha.length - 1].role !== 'user'){ diff --git a/src/ts/process/command.ts b/src/ts/process/command.ts new file mode 100644 index 00000000..d0a8aabe --- /dev/null +++ b/src/ts/process/command.ts @@ -0,0 +1,184 @@ +import { get } from "svelte/store"; +import { DataBase, setDatabase } from "../storage/database"; +import { selectedCharID } from "../stores"; +import { alertInput, alertMd, alertSelect, alertToast } from "../alert"; +import { sayTTS } from "./tts"; +import { risuChatParser } from "../parser"; + +export async function processMultiCommand(command:string) { + let pipe = '' + const splited:string[] = [] + let lastIndex = 0 + let quoteDepth = false + for(let i = 0; i{ + const db = get(DataBase) + const currentChar = db.characters[get(selectedCharID)] + const currentChat = currentChar.chats[currentChar.chatPage] + let {commandName, arg, namedArg} = commandParser(command, pipe) + + arg = risuChatParser(arg, { + chara: currentChar.type === 'character' ? currentChar : null + }) + + const namedArgKeys = Object.keys(namedArg) + for(const key of namedArgKeys){ + namedArg[key] = risuChatParser(namedArg[key], { + chara: currentChar.type === 'character' ? currentChar : null + }) + } + console.log(commandName, arg, namedArg) + + switch(commandName){ + //STScript compatibility commands + case 'input':{ + pipe = await alertInput(arg) + return pipe + } + case 'echo':{ + alertToast(arg) + return pipe + } + case 'popup':{ + alertMd(arg) + return pipe + } + case 'pass':{ + pipe = arg + return pipe + } + case 'buttons': { + if(namedArg.labels){ + try { + const JSONLabels = JSON.parse(namedArg.labels) + if(Array.isArray(JSONLabels)){ + pipe = await alertSelect(JSONLabels) + } + } catch (error) {} + } + return pipe + } + case 'setinput': { + //NOT IMPLEMENTED + return false + } + case 'speak': { + if(currentChar.type === 'character'){ + await sayTTS(currentChar, arg) + return pipe + } + if(currentChar.type === 'group'){ + //NOT IMPLEMENTED + return pipe + } + } + case 'send': { + currentChat.message.push({ + role: "user", + data: arg + }) + setDatabase(db) + return pipe + } + case 'sendas': { + //name not implemented + currentChat.message.push({ + role: "char", + data: arg + }) + setDatabase(db) + return pipe + } + case 'comment': { + //works differently, but its close enough + const addition = `\n${arg}\n` + currentChat.message[currentChat.message.length-1].data += addition + setDatabase(db) + return pipe + } + case 'cut':{ + if(arg.includes('-')){ + const [start, end] = arg.split('-') + currentChat.message = currentChat.message.slice(parseInt(start), parseInt(end)) + setDatabase(db) + } + else if(!isNaN(parseInt(arg))){ + const index = parseInt(arg) + currentChat.message = currentChat.message.splice(index, 1) + setDatabase(db) + } + else{ //For risu, doesn'ts work for STScript + const id = arg + currentChat.message = currentChat.message.filter((e)=>e.chatId !== id) + setDatabase(db) + } + return pipe + } + case 'del': { + const size = parseInt(arg) + if(!isNaN(size)){ + currentChat.message = currentChat.message.slice(currentChat.message.length-size) + setDatabase(db) + } + return pipe + } + case 'len':{ + try { + const parsed = JSON.parse(arg) + if(Array.isArray(parsed)){ + pipe = parsed.length.toString() + } + } catch (error) {} + return pipe + } + } + return false +} + + +function commandParser(command:string, pipe:string){ + if(command.startsWith('/')){ + command = command.slice(1) + } + const sliced = command.split(' ').filter((e)=>e!='') + const commandName = sliced[0] + let argArray:string[] = [] + let namedArg:{[key:string]:string} = {} + for(let i = 1; i