Add optional args to /echo command

1d6f0386013991b6acd56e3c6d4a121a513387cf

Wolfsblvt <wolfsblvt@gmx.de>

1 files changed, +68 -7Showing whitespace changes
public/scripts/slash-commands.js+68 -7
@@ -745,6 +745,32 @@ export function initDefaultSlashCommands() {
745745 new SlashCommandEnumValue('success', 'success', enumTypes.enum, '✅'),
746746 ],
747747 }),
748+ SlashCommandNamedArgument.fromProps({
749+ name: 'timeOut',
750+ description: 'time in milliseconds to display the toast message. Set this and \'extendedTimeout\' to 0 to show indefinitely until dismissed.',
751+ typeList: [ARGUMENT_TYPE.NUMBER],
752+ defaultValue: `${toastr.options.timeOut}`,
753+ }),
754+ SlashCommandNamedArgument.fromProps({
755+ name: 'extendedTimeout',
756+ description: 'time in milliseconds to display the toast message. Set this and \'timeOut\' to 0 to show indefinitely until dismissed.',
757+ typeList: [ARGUMENT_TYPE.NUMBER],
758+ defaultValue: `${toastr.options.extendedTimeOut}`,
759+ }),
760+ SlashCommandNamedArgument.fromProps({
761+ name: 'preventDuplicates',
762+ description: 'prevent duplicate toasts with the same message from being displayed.',
763+ typeList: [ARGUMENT_TYPE.BOOLEAN],
764+ defaultValue: 'false',
765+ enumList: commonEnumProviders.boolean('trueFalse')(),
766+ }),
767+ SlashCommandNamedArgument.fromProps({
768+ name: 'awaitDismissal',
769+ description: 'wait for the toast to be dismissed before continuing.',
770+ typeList: [ARGUMENT_TYPE.BOOLEAN],
771+ defaultValue: 'false',
772+ enumList: commonEnumProviders.boolean('trueFalse')(),
773+ }),
748774 ],
749775 unnamedArgumentList: [
750776 new SlashCommandArgument(
@@ -1939,31 +1965,66 @@ async function generateCallback(args, value) {
19391965 }
19401966}
19411967
1968+/**
1969+ *
1970+ * @param {{title?: string, severity?: string, timeOut?: string, extendedTimeOut?: string, preventDuplicates?: string, awaitDismissal?: string}} args - named arguments from the slash command
1971+ * @param {string} value - The string to echo (unnamed argument from the slash command)
1972+ * @returns {Promise<string>} The text that was echoed
1973+ */
19421974async function echoCallback(args, value) {
19431975 // Note: We don't need to sanitize input, as toastr is set up by default to escape HTML via toastr options
19441976 if (value === '') {
19451977 console.warn('WARN: No argument provided for /echo command');
19461978 return '';
1979+ }
1980+
1981+ if (args.severity && !['error', 'warning', 'success', 'info'].includes(args.severity)) {
1982+ toastr.warning(`Invalid severity provided for /echo command: ${args.severity}`);
1983+ args.severity = null;
19471984 }
1948- const title = args?.title !== undefined && typeof args?.title === 'string' ? args.title : undefined;
1985+
1949- const severity = args?.severity !== undefined && typeof args?.severity === 'string' ? args.severity : 'info';
1986+ const title = args.title ? args.title : undefined;
1987+ const severity = args.severity ? args.severity : 'info';
1988+
1989+ /** @type {ToastrOptions} */
1990+ const options = {};
1991+ if (args.timeOut && !isNaN(parseInt(args.timeOut))) options.timeOut = parseInt(args.timeOut);
1992+ if (args.extendedTimeOut && !isNaN(parseInt(args.extendedTimeOut))) options.extendedTimeOut = parseInt(args.extendedTimeOut);
1993+ if (isTrueBoolean(args.preventDuplicates)) options.preventDuplicates = true;
1994+
1995+ // Prepare possible await handling
1996+ let awaitDismissal = isTrueBoolean(args.awaitDismissal);
1997+ let resolveToastDismissal;
1998+
1999+ if (awaitDismissal) {
2000+ options.onHidden = () => resolveToastDismissal(value);
2001+ }
2002+
19502003 switch (severity) {
19512004 case 'error':
19522005 toastr.error(value, title, options);
19532006 break;
19542007 case 'warning':
19552008 toastr.warning(value, title, options);
19562009 break;
19572010 case 'success':
19582011 toastr.success(value, title, options);
19592012 break;
19602013 case 'info':
19612014 default:
19622015 toastr.info(value, title, options);
19632016 break;
19642017 }
2018+
2019+ if (awaitDismissal) {
2020+ return new Promise((resolve) => {
2021+ resolveToastDismissal = resolve;
2022+ });
2023+ } else {
19652024 return value;
19662025 }
2026+}
2027+
19672028
19682029async function addSwipeCallback(_, arg) {
19692030 const lastMessage = chat[chat.length - 1];