From db07c4a120c9b4e3da9616a111caaeac589fb4dd Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Fri, 13 Mar 2026 20:45:54 +0200 Subject: [PATCH] feat: add array-wrap and array-unwrap commands (#5277) * feat: add array-wrap and array-unwrap commands * Add LIST as available argument type Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Throw when trying to wrap a closure Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Throw on closure in array-unwrap * Add the rest of argument types for array-unwrap Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Add Python-friendly aliases * Don't filter out just strings Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Proper primitive value unwrapping * Throw on multiple unnamed arguments * Enhance parsed object wrapping * Update comment to clarify boolean support in value wrapping --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- public/scripts/slash-commands.js | 103 +++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index e43dbb831..0196b1ccf 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -3214,6 +3214,109 @@ export function initDefaultSlashCommands() { helpString: t`Plays the message received sound effect.`, })); + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ + name: 'array-wrap', + aliases: ['list-wrap'], + returns: t`unnamed argument value wrapped into an array`, + helpString: t`Wraps a single unnamed argument into an array if it's not already an array. If the value is an empty string, returns an empty array.`, + unnamedArgumentList: [ + SlashCommandArgument.fromProps({ + description: t`value`, + acceptsMultiple: false, + isRequired: true, + typeList: [ARGUMENT_TYPE.STRING, ARGUMENT_TYPE.DICTIONARY, ARGUMENT_TYPE.BOOLEAN, ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.LIST], + }), + ], + callback: (_args, value) => { + // Closures are not supported + if (value instanceof SlashCommandClosure) { + throw new SlashCommandExecutionError(t`Closures are not supported as unnamed arguments for /array-wrap. Did you forget to call the closure with parentheses?`); + } + + // Multiple unnamed arguments are not supported since acceptsMultiple is false, but check just in case + if (Array.isArray(value)) { + throw new SlashCommandExecutionError(t`/array-wrap does not support multiple unnamed arguments.`); + } + + // Empty string - empty arrays + if (value === '') { + return JSON.stringify([]); + } + + try { + // If the value is a valid JSON string, parse it + const parsedValue = JSON.parse(value); + + // Already an array - return as-is + if (Array.isArray(parsedValue)) { + return value; + } + + // If it's an object, wrap it into an array and stringify + if (typeof parsedValue === 'object' && parsedValue !== null) { + return JSON.stringify([parsedValue]); + } + + // Wrap the original value (string, number, boolean) into an array, preserving quotes for strings + return JSON.stringify([value]); + } catch { + // Not a valid JSON string - wrap the original value + return JSON.stringify([value]); + } + }, + })); + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ + name: 'array-unwrap', + aliases: ['list-unwrap'], + returns: t`unnamed argument value unwrapped from an array`, + helpString: t`Unwraps the first element of an array provided as an unnamed argument. If the value is not an array, returns the value as-is. If the array is empty, returns an empty string.`, + unnamedArgumentList: [ + SlashCommandArgument.fromProps({ + description: t`value`, + acceptsMultiple: false, + isRequired: true, + typeList: [ARGUMENT_TYPE.STRING, ARGUMENT_TYPE.DICTIONARY, ARGUMENT_TYPE.BOOLEAN, ARGUMENT_TYPE.NUMBER, ARGUMENT_TYPE.LIST], + }), + ], + callback: (_args, value) => { + // Closures are not supported + if (value instanceof SlashCommandClosure) { + throw new SlashCommandExecutionError(t`Closures are not supported as unnamed arguments for /array-unwrap. Did you forget to call the closure with parentheses?`); + } + + // Multiple unnamed arguments are not supported since acceptsMultiple is false, but check just in case + if (Array.isArray(value)) { + throw new SlashCommandExecutionError(t`/array-unwrap does not support multiple unnamed arguments.`); + } + + try { + // If the value is a JSON array, get the first element + const parsed = JSON.parse(value); + + if (Array.isArray(parsed)) { + const unwrappedValue = parsed?.[0] ?? ''; + + // If the first element is null or undefined, return an empty string + if (unwrappedValue === null || unwrappedValue === undefined) { + return ''; + } + + // If the first element is an object, stringify it. + if (typeof unwrappedValue === 'object') { + return JSON.stringify(unwrappedValue); + } + + // Otherwise, return it as a string. + return String(unwrappedValue); + } + return value; + } catch { + // Not a valid JSON - return as-is + return value; + } + }, + })); + registerVariableCommands(); }