From 67174c8cf85a9bbfa8039f3cf5146cde684a9644 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sat, 25 Nov 2023 19:53:00 +0200 Subject: [PATCH] Add functions to delete local and global variables --- public/scripts/variables.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/public/scripts/variables.js b/public/scripts/variables.js index f6b06e49b..1f395486f 100644 --- a/public/scripts/variables.js +++ b/public/scripts/variables.js @@ -301,6 +301,28 @@ async function executeSubCommands(command) { return result?.pipe || ''; } +function deleteLocalVariable(name) { + if (!existsLocalVariable(name)) { + console.warn(`The local variable "${name}" does not exist.`); + return ''; + } + + delete chat_metadata.variables[name]; + saveMetadataDebounced(); + return ''; +} + +function deleteGlobalVariable(name) { + if (!existsGlobalVariable(name)) { + console.warn(`The global variable "${name}" does not exist.`); + return ''; + } + + delete extension_settings.variables.global[name]; + saveSettingsDebounced(); + return ''; +} + export function registerVariableCommands() { registerSlashCommand('listvar', listVariablesCallback, [''], ' – list registered chat variables', true, true); registerSlashCommand('setvar', (args, value) => setLocalVariable(args.key || args.name, value), [], 'key=varname (value) – set a local variable value and pass it down the pipe, e.g. /setvar key=color green', true, true); @@ -311,4 +333,6 @@ export function registerVariableCommands() { registerSlashCommand('addglobalvar', (args, value) => addGlobalVariable(args.key || args.name, value), [], 'key=varname (increment) – add a value to a global variable and pass the result down the pipe, e.g. /addglobalvar score 10', true, true); registerSlashCommand('if', ifCallback, [], 'a=varname1 b=varname2 rule=comparison else="(alt.command)" "(command)" – compare the value of variable "a" with the value of variable "b", and if the condition yields true, then execute any valid slash command enclosed in quotes and pass the result of the command execution down the pipe. Numeric values and string literals for "a" and "b" supported. Available rules: gt => a > b, gte => a >= b, lt => a < b, lte => a <= b, eq => a == b, neq => a != b, not => !a, in (strings) => a includes b, nin (strings) => a not includes b, e.g. /if a=score a=10 rule=gte "/speak You win" triggers a /speak command if the value of "score" is greater or equals 10.', true, true); registerSlashCommand('while', whileCallback, [], 'a=varname1 b=varname2 rule=comparison "(command)" – compare the value of variable "a" with the value of variable "b", and if the condition yields true, then execute any valid slash command enclosed in quotes. Numeric values and string literals for "a" and "b" supported. Available rules: gt => a > b, gte => a >= b, lt => a < b, lte => a <= b, eq => a == b, neq => a != b, not => !a, in (strings) => a includes b, nin (strings) => a not includes b, e.g. /while a=i a=10 rule=let "/addvar i 1" adds 1 to the value of "i" until it reaches 10. Loops are limited to 100 iterations by default, pass guard=off to disable.', true, true); + registerSlashCommand('flushvar', (_, value) => deleteLocalVariable(value), [], '(key) – delete a local variable, e.g. /flushvar score', true, true); + registerSlashCommand('flushglobalvar', (_, value) => deleteGlobalVariable(value), [], '(key) – delete a global variable, e.g. /flushglobalvar score', true, true); }