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