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;
}
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}"`);
}

View File

@ -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 {