Only convert variable if needed

6f85327078e1abd841a3cba9da71d254c5984aa7

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +8 -11Ignore whitespace
public/scripts/slash-commands/SlashCommandScope.js+4 -5
@@ -57,7 +57,6 @@ export class SlashCommandScope {
5757 this.variables[key] = value;
5858 }
5959 setVariable(key, value, index = null, type = null) {
60- value = convertValueType(value, type);
6160 if (this.existsVariableInScope(key)) {
6261 if (index !== null && index !== undefined) {
6362 let v = this.variables[key];
@@ -65,13 +64,13 @@ export class SlashCommandScope {
6564 v = JSON.parse(v);
6665 const numIndex = Number(index);
6766 if (Number.isNaN(numIndex)) {
6867 v[index] = convertValueType(value, type);
6968 } else {
7069 v[numIndex] = convertValueType(value, type);
7170 }
7271 v = JSON.stringify(v);
7372 } catch {
7473 v[index] = convertValueType(value, type);
7574 }
7675 this.variables[key] = v;
7776 } else {
@@ -80,7 +79,7 @@ export class SlashCommandScope {
8079 return value;
8180 }
8281 if (this.parent) {
8382 return this.parent.setVariable(key, value, index, type);
8483 }
8584 throw new SlashCommandScopeVariableNotFoundError(`No such variable: "${key}"`);
8685 }
public/scripts/variables.js+4 -6
@@ -51,19 +51,18 @@ function setLocalVariable(name, value, args = {}) {
5151
5252 if (args.index !== undefined) {
5353 try {
54- value = convertValueType(value, args.type);
5554 let localVariable = JSON.parse(chat_metadata.variables[name] ?? 'null');
5655 const numIndex = Number(args.index);
5756 if (Number.isNaN(numIndex)) {
5857 if (localVariable === null) {
5958 localVariable = {};
6059 }
6160 localVariable[args.index] = convertValueType(value, args.type);
6261 } else {
6362 if (localVariable === null) {
6463 localVariable = [];
6564 }
6665 localVariable[numIndex] = convertValueType(value, args.type);
6766 }
6867 chat_metadata.variables[name] = JSON.stringify(localVariable);
6968 } catch {
@@ -101,19 +100,18 @@ function getGlobalVariable(name, args = {}) {
101100function setGlobalVariable(name, value, args = {}) {
102101 if (args.index !== undefined) {
103102 try {
104- value = convertValueType(value, args.type);
105103 let globalVariable = JSON.parse(extension_settings.variables.global[name] ?? 'null');
106104 const numIndex = Number(args.index);
107105 if (Number.isNaN(numIndex)) {
108106 if (globalVariable === null) {
109107 globalVariable = {};
110108 }
111109 globalVariable[args.index] = convertValueType(value, args.type);
112110 } else {
113111 if (globalVariable === null) {
114112 globalVariable = [];
115113 }
116114 globalVariable[numIndex] = convertValueType(value, args.type);
117115 }
118116 extension_settings.variables.global[name] = JSON.stringify(globalVariable);
119117 } catch {