Only convert variable if needed

This commit is contained in:
Cohee 2024-09-01 22:59:50 +03:00
parent b56a457c6c
commit 6f85327078
2 changed files with 8 additions and 11 deletions

View File

@ -57,7 +57,6 @@ export class SlashCommandScope {
this.variables[key] = value; this.variables[key] = value;
} }
setVariable(key, value, index = null, type = null) { setVariable(key, value, index = null, type = null) {
value = convertValueType(value, type);
if (this.existsVariableInScope(key)) { if (this.existsVariableInScope(key)) {
if (index !== null && index !== undefined) { if (index !== null && index !== undefined) {
let v = this.variables[key]; let v = this.variables[key];
@ -65,13 +64,13 @@ export class SlashCommandScope {
v = JSON.parse(v); v = JSON.parse(v);
const numIndex = Number(index); const numIndex = Number(index);
if (Number.isNaN(numIndex)) { if (Number.isNaN(numIndex)) {
v[index] = value; v[index] = convertValueType(value, type);
} else { } else {
v[numIndex] = value; v[numIndex] = convertValueType(value, type);
} }
v = JSON.stringify(v); v = JSON.stringify(v);
} catch { } catch {
v[index] = value; v[index] = convertValueType(value, type);
} }
this.variables[key] = v; this.variables[key] = v;
} else { } else {
@ -80,7 +79,7 @@ export class SlashCommandScope {
return value; return value;
} }
if (this.parent) { 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}"`); throw new SlashCommandScopeVariableNotFoundError(`No such variable: "${key}"`);
} }

View File

@ -51,19 +51,18 @@ function setLocalVariable(name, value, args = {}) {
if (args.index !== undefined) { if (args.index !== undefined) {
try { try {
value = convertValueType(value, args.type);
let localVariable = JSON.parse(chat_metadata.variables[name] ?? 'null'); let localVariable = JSON.parse(chat_metadata.variables[name] ?? 'null');
const numIndex = Number(args.index); const numIndex = Number(args.index);
if (Number.isNaN(numIndex)) { if (Number.isNaN(numIndex)) {
if (localVariable === null) { if (localVariable === null) {
localVariable = {}; localVariable = {};
} }
localVariable[args.index] = value; localVariable[args.index] = convertValueType(value, args.type);
} else { } else {
if (localVariable === null) { if (localVariable === null) {
localVariable = []; localVariable = [];
} }
localVariable[numIndex] = value; localVariable[numIndex] = convertValueType(value, args.type);
} }
chat_metadata.variables[name] = JSON.stringify(localVariable); chat_metadata.variables[name] = JSON.stringify(localVariable);
} catch { } catch {
@ -101,19 +100,18 @@ function getGlobalVariable(name, args = {}) {
function setGlobalVariable(name, value, args = {}) { function setGlobalVariable(name, value, args = {}) {
if (args.index !== undefined) { if (args.index !== undefined) {
try { try {
value = convertValueType(value, args.type);
let globalVariable = JSON.parse(extension_settings.variables.global[name] ?? 'null'); let globalVariable = JSON.parse(extension_settings.variables.global[name] ?? 'null');
const numIndex = Number(args.index); const numIndex = Number(args.index);
if (Number.isNaN(numIndex)) { if (Number.isNaN(numIndex)) {
if (globalVariable === null) { if (globalVariable === null) {
globalVariable = {}; globalVariable = {};
} }
globalVariable[args.index] = value; globalVariable[args.index] = convertValueType(value, args.type);
} else { } else {
if (globalVariable === null) { if (globalVariable === null) {
globalVariable = []; globalVariable = [];
} }
globalVariable[numIndex] = value; globalVariable[numIndex] = convertValueType(value, args.type);
} }
extension_settings.variables.global[name] = JSON.stringify(globalVariable); extension_settings.variables.global[name] = JSON.stringify(globalVariable);
} catch { } catch {