2024-05-12 21:15:05 +02:00
|
|
|
import { SlashCommandClosure } from './SlashCommandClosure.js';
|
2024-08-29 14:55:54 +02:00
|
|
|
import { convertValueType } from '../utils.js';
|
2024-05-12 21:15:05 +02:00
|
|
|
|
|
|
|
export class SlashCommandScope {
|
|
|
|
/**@type {string[]}*/ variableNames = [];
|
|
|
|
get allVariableNames() {
|
|
|
|
const names = [...this.variableNames, ...(this.parent?.allVariableNames ?? [])];
|
|
|
|
return names.filter((it,idx)=>idx == names.indexOf(it));
|
|
|
|
}
|
|
|
|
// @ts-ignore
|
|
|
|
/**@type {object.<string, string|SlashCommandClosure>}*/ variables = {};
|
|
|
|
// @ts-ignore
|
|
|
|
/**@type {object.<string, string|SlashCommandClosure>}*/ macros = {};
|
|
|
|
/**@type {{key:string, value:string|SlashCommandClosure}[]} */
|
|
|
|
get macroList() {
|
|
|
|
return [...Object.keys(this.macros).map(key=>({ key, value:this.macros[key] })), ...(this.parent?.macroList ?? [])];
|
|
|
|
}
|
|
|
|
/**@type {SlashCommandScope}*/ parent;
|
|
|
|
/**@type {string}*/ #pipe;
|
|
|
|
get pipe() {
|
|
|
|
return this.#pipe ?? this.parent?.pipe;
|
|
|
|
}
|
|
|
|
set pipe(value) {
|
|
|
|
this.#pipe = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
constructor(parent) {
|
|
|
|
this.parent = parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
getCopy() {
|
|
|
|
const scope = new SlashCommandScope(this.parent);
|
|
|
|
scope.variableNames = [...this.variableNames];
|
|
|
|
scope.variables = Object.assign({}, this.variables);
|
|
|
|
scope.macros = Object.assign({}, this.macros);
|
|
|
|
scope.#pipe = this.#pipe;
|
|
|
|
return scope;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-06 01:13:11 +02:00
|
|
|
setMacro(key, value, overwrite = true) {
|
|
|
|
if (overwrite || !this.macroList.find(it=>it.key == key)) {
|
|
|
|
this.macros[key] = value;
|
|
|
|
}
|
2024-05-12 21:15:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
existsVariableInScope(key) {
|
|
|
|
return Object.keys(this.variables).includes(key);
|
|
|
|
}
|
|
|
|
existsVariable(key) {
|
|
|
|
return Object.keys(this.variables).includes(key) || this.parent?.existsVariable(key);
|
|
|
|
}
|
|
|
|
letVariable(key, value = undefined) {
|
|
|
|
if (this.existsVariableInScope(key)) throw new SlashCommandScopeVariableExistsError(`Variable named "${key}" already exists.`);
|
|
|
|
this.variables[key] = value;
|
|
|
|
}
|
2024-08-29 14:55:54 +02:00
|
|
|
setVariable(key, value, index = null, type = null) {
|
|
|
|
value = convertValueType(value, type);
|
2024-05-12 21:15:05 +02:00
|
|
|
if (this.existsVariableInScope(key)) {
|
|
|
|
if (index !== null && index !== undefined) {
|
|
|
|
let v = this.variables[key];
|
|
|
|
try {
|
|
|
|
v = JSON.parse(v);
|
|
|
|
const numIndex = Number(index);
|
|
|
|
if (Number.isNaN(numIndex)) {
|
|
|
|
v[index] = value;
|
|
|
|
} else {
|
|
|
|
v[numIndex] = value;
|
|
|
|
}
|
|
|
|
v = JSON.stringify(v);
|
|
|
|
} catch {
|
|
|
|
v[index] = value;
|
|
|
|
}
|
|
|
|
this.variables[key] = v;
|
|
|
|
} else {
|
|
|
|
this.variables[key] = value;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (this.parent) {
|
|
|
|
return this.parent.setVariable(key, value, index);
|
|
|
|
}
|
|
|
|
throw new SlashCommandScopeVariableNotFoundError(`No such variable: "${key}"`);
|
|
|
|
}
|
|
|
|
getVariable(key, index = null) {
|
|
|
|
if (this.existsVariableInScope(key)) {
|
|
|
|
if (index !== null && index !== undefined) {
|
|
|
|
let v = this.variables[key];
|
|
|
|
try { v = JSON.parse(v); } catch { /* empty */ }
|
|
|
|
const numIndex = Number(index);
|
|
|
|
if (Number.isNaN(numIndex)) {
|
|
|
|
v = v[index];
|
|
|
|
} else {
|
|
|
|
v = v[numIndex];
|
|
|
|
}
|
|
|
|
if (typeof v == 'object') return JSON.stringify(v);
|
2024-06-14 23:48:41 +02:00
|
|
|
return v ?? '';
|
2024-05-12 21:15:05 +02:00
|
|
|
} else {
|
|
|
|
const value = this.variables[key];
|
2024-07-20 17:59:52 +02:00
|
|
|
return (value?.trim?.() === '' || isNaN(Number(value))) ? (value || '') : Number(value);
|
2024-05-12 21:15:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.parent) {
|
|
|
|
return this.parent.getVariable(key, index);
|
|
|
|
}
|
|
|
|
throw new SlashCommandScopeVariableNotFoundError(`No such variable: "${key}"`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class SlashCommandScopeVariableExistsError extends Error {}
|
|
|
|
|
|
|
|
|
|
|
|
export class SlashCommandScopeVariableNotFoundError extends Error {}
|