From 6f85327078e1abd841a3cba9da71d254c5984aa7 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sun, 1 Sep 2024 22:59:50 +0300 Subject: [PATCH] Only convert variable if needed --- public/scripts/slash-commands/SlashCommandScope.js | 9 ++++----- public/scripts/variables.js | 10 ++++------ 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/public/scripts/slash-commands/SlashCommandScope.js b/public/scripts/slash-commands/SlashCommandScope.js index 7fd573cac..e7ec2a58b 100644 --- a/public/scripts/slash-commands/SlashCommandScope.js +++ b/public/scripts/slash-commands/SlashCommandScope.js @@ -57,7 +57,6 @@ export class SlashCommandScope { this.variables[key] = value; } setVariable(key, value, index = null, type = null) { - value = convertValueType(value, type); if (this.existsVariableInScope(key)) { if (index !== null && index !== undefined) { let v = this.variables[key]; @@ -65,13 +64,13 @@ export class SlashCommandScope { v = JSON.parse(v); const numIndex = Number(index); if (Number.isNaN(numIndex)) { - v[index] = value; + v[index] = convertValueType(value, type); } else { - v[numIndex] = value; + v[numIndex] = convertValueType(value, type); } v = JSON.stringify(v); } catch { - v[index] = value; + v[index] = convertValueType(value, type); } this.variables[key] = v; } else { @@ -80,7 +79,7 @@ export class SlashCommandScope { return value; } if (this.parent) { - return this.parent.setVariable(key, value, index); + return this.parent.setVariable(key, value, index, type); } throw new SlashCommandScopeVariableNotFoundError(`No such variable: "${key}"`); } diff --git a/public/scripts/variables.js b/public/scripts/variables.js index e0ccfbdc6..c72605236 100644 --- a/public/scripts/variables.js +++ b/public/scripts/variables.js @@ -51,19 +51,18 @@ function setLocalVariable(name, value, args = {}) { if (args.index !== undefined) { try { - value = convertValueType(value, args.type); let localVariable = JSON.parse(chat_metadata.variables[name] ?? 'null'); const numIndex = Number(args.index); if (Number.isNaN(numIndex)) { if (localVariable === null) { localVariable = {}; } - localVariable[args.index] = value; + localVariable[args.index] = convertValueType(value, args.type); } else { if (localVariable === null) { localVariable = []; } - localVariable[numIndex] = value; + localVariable[numIndex] = convertValueType(value, args.type); } chat_metadata.variables[name] = JSON.stringify(localVariable); } catch { @@ -101,19 +100,18 @@ function getGlobalVariable(name, args = {}) { function setGlobalVariable(name, value, args = {}) { if (args.index !== undefined) { try { - value = convertValueType(value, args.type); let globalVariable = JSON.parse(extension_settings.variables.global[name] ?? 'null'); const numIndex = Number(args.index); if (Number.isNaN(numIndex)) { if (globalVariable === null) { globalVariable = {}; } - globalVariable[args.index] = value; + globalVariable[args.index] = convertValueType(value, args.type); } else { if (globalVariable === null) { globalVariable = []; } - globalVariable[numIndex] = value; + globalVariable[numIndex] = convertValueType(value, args.type); } extension_settings.variables.global[name] = JSON.stringify(globalVariable); } catch {