From 1d6f0386013991b6acd56e3c6d4a121a513387cf Mon Sep 17 00:00:00 2001 From: Wolfsblvt Date: Sat, 29 Jun 2024 02:16:36 +0200 Subject: [PATCH 1/2] Add optional args to /echo command --- public/scripts/slash-commands.js | 77 ++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 8 deletions(-) diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index 3573453bd..6abec1972 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -745,6 +745,32 @@ export function initDefaultSlashCommands() { new SlashCommandEnumValue('success', 'success', enumTypes.enum, '✅'), ], }), + SlashCommandNamedArgument.fromProps({ + name: 'timeOut', + description: 'time in milliseconds to display the toast message. Set this and \'extendedTimeout\' to 0 to show indefinitely until dismissed.', + typeList: [ARGUMENT_TYPE.NUMBER], + defaultValue: `${toastr.options.timeOut}`, + }), + SlashCommandNamedArgument.fromProps({ + name: 'extendedTimeout', + description: 'time in milliseconds to display the toast message. Set this and \'timeOut\' to 0 to show indefinitely until dismissed.', + typeList: [ARGUMENT_TYPE.NUMBER], + defaultValue: `${toastr.options.extendedTimeOut}`, + }), + SlashCommandNamedArgument.fromProps({ + name: 'preventDuplicates', + description: 'prevent duplicate toasts with the same message from being displayed.', + typeList: [ARGUMENT_TYPE.BOOLEAN], + defaultValue: 'false', + enumList: commonEnumProviders.boolean('trueFalse')(), + }), + SlashCommandNamedArgument.fromProps({ + name: 'awaitDismissal', + description: 'wait for the toast to be dismissed before continuing.', + typeList: [ARGUMENT_TYPE.BOOLEAN], + defaultValue: 'false', + enumList: commonEnumProviders.boolean('trueFalse')(), + }), ], unnamedArgumentList: [ new SlashCommandArgument( @@ -1939,32 +1965,67 @@ async function generateCallback(args, value) { } } +/** + * + * @param {{title?: string, severity?: string, timeOut?: string, extendedTimeOut?: string, preventDuplicates?: string, awaitDismissal?: string}} args - named arguments from the slash command + * @param {string} value - The string to echo (unnamed argument from the slash command) + * @returns {Promise} The text that was echoed + */ async function echoCallback(args, value) { // Note: We don't need to sanitize input, as toastr is set up by default to escape HTML via toastr options if (value === '') { console.warn('WARN: No argument provided for /echo command'); - return; + return ''; } - const title = args?.title !== undefined && typeof args?.title === 'string' ? args.title : undefined; - const severity = args?.severity !== undefined && typeof args?.severity === 'string' ? args.severity : 'info'; + + if (args.severity && !['error', 'warning', 'success', 'info'].includes(args.severity)) { + toastr.warning(`Invalid severity provided for /echo command: ${args.severity}`); + args.severity = null; + } + + const title = args.title ? args.title : undefined; + const severity = args.severity ? args.severity : 'info'; + + /** @type {ToastrOptions} */ + const options = {}; + if (args.timeOut && !isNaN(parseInt(args.timeOut))) options.timeOut = parseInt(args.timeOut); + if (args.extendedTimeOut && !isNaN(parseInt(args.extendedTimeOut))) options.extendedTimeOut = parseInt(args.extendedTimeOut); + if (isTrueBoolean(args.preventDuplicates)) options.preventDuplicates = true; + + // Prepare possible await handling + let awaitDismissal = isTrueBoolean(args.awaitDismissal); + let resolveToastDismissal; + + if (awaitDismissal) { + options.onHidden = () => resolveToastDismissal(value); + } + switch (severity) { case 'error': - toastr.error(value, title); + toastr.error(value, title, options); break; case 'warning': - toastr.warning(value, title); + toastr.warning(value, title, options); break; case 'success': - toastr.success(value, title); + toastr.success(value, title, options); break; case 'info': default: - toastr.info(value, title); + toastr.info(value, title, options); break; } - return value; + + if (awaitDismissal) { + return new Promise((resolve) => { + resolveToastDismissal = resolve; + }); + } else { + return value; + } } + async function addSwipeCallback(_, arg) { const lastMessage = chat[chat.length - 1]; From 5c3b799d6589539d6804b74733bad6f1a7dedd9e Mon Sep 17 00:00:00 2001 From: Wolfsblvt Date: Sat, 29 Jun 2024 02:24:20 +0200 Subject: [PATCH 2/2] Fix naming inconsistencies --- public/scripts/slash-commands.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index 6abec1972..cfc206e89 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -746,14 +746,14 @@ export function initDefaultSlashCommands() { ], }), SlashCommandNamedArgument.fromProps({ - name: 'timeOut', + name: 'timeout', description: 'time in milliseconds to display the toast message. Set this and \'extendedTimeout\' to 0 to show indefinitely until dismissed.', typeList: [ARGUMENT_TYPE.NUMBER], defaultValue: `${toastr.options.timeOut}`, }), SlashCommandNamedArgument.fromProps({ name: 'extendedTimeout', - description: 'time in milliseconds to display the toast message. Set this and \'timeOut\' to 0 to show indefinitely until dismissed.', + description: 'time in milliseconds to display the toast message. Set this and \'timeout\' to 0 to show indefinitely until dismissed.', typeList: [ARGUMENT_TYPE.NUMBER], defaultValue: `${toastr.options.extendedTimeOut}`, }), @@ -1967,7 +1967,7 @@ async function generateCallback(args, value) { /** * - * @param {{title?: string, severity?: string, timeOut?: string, extendedTimeOut?: string, preventDuplicates?: string, awaitDismissal?: string}} args - named arguments from the slash command + * @param {{title?: string, severity?: string, timeout?: string, extendedTimeout?: string, preventDuplicates?: string, awaitDismissal?: string}} args - named arguments from the slash command * @param {string} value - The string to echo (unnamed argument from the slash command) * @returns {Promise} The text that was echoed */ @@ -1988,8 +1988,8 @@ async function echoCallback(args, value) { /** @type {ToastrOptions} */ const options = {}; - if (args.timeOut && !isNaN(parseInt(args.timeOut))) options.timeOut = parseInt(args.timeOut); - if (args.extendedTimeOut && !isNaN(parseInt(args.extendedTimeOut))) options.extendedTimeOut = parseInt(args.extendedTimeOut); + if (args.timeout && !isNaN(parseInt(args.timeout))) options.timeOut = parseInt(args.timeout); + if (args.extendedTimeout && !isNaN(parseInt(args.extendedTimeout))) options.extendedTimeOut = parseInt(args.extendedTimeout); if (isTrueBoolean(args.preventDuplicates)) options.preventDuplicates = true; // Prepare possible await handling