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() {
745 new SlashCommandEnumValue('success', 'success', enumTypes.enum, '✅'),745 new SlashCommandEnumValue('success', 'success', enumTypes.enum, '✅'),
746 ],746 ],
747 }),747 }),
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 }),
748 ],774 ],
749 unnamedArgumentList: [775 unnamedArgumentList: [
750 new SlashCommandArgument(776 new SlashCommandArgument(
@@ -1939,31 +1965,66 @@ async function generateCallback(args, value) {
1939 }1965 }
1940}1966}
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 */
1942async function echoCallback(args, value) {1974async function echoCallback(args, value) {
1943 // Note: We don't need to sanitize input, as toastr is set up by default to escape HTML via toastr options1975 // Note: We don't need to sanitize input, as toastr is set up by default to escape HTML via toastr options
1944 if (value === '') {1976 if (value === '') {
1945 console.warn('WARN: No argument provided for /echo command');1977 console.warn('WARN: No argument provided for /echo command');
1946 return;1978 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;
1947 }1984 }
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
1950 switch (severity) {2003 switch (severity) {
1951 case 'error':2004 case 'error':
1952 toastr.error(value, title);2005 toastr.error(value, title, options);
1953 break;2006 break;
1954 case 'warning':2007 case 'warning':
1955 toastr.warning(value, title);2008 toastr.warning(value, title, options);
1956 break;2009 break;
1957 case 'success':2010 case 'success':
1958 toastr.success(value, title);2011 toastr.success(value, title, options);
1959 break;2012 break;
1960 case 'info':2013 case 'info':
1961 default:2014 default:
1962 toastr.info(value, title);2015 toastr.info(value, title, options);
1963 break;2016 break;
1964 }2017 }
2018
2019 if (awaitDismissal) {
2020 return new Promise((resolve) => {
2021 resolveToastDismissal = resolve;
2022 });
2023 } else {
1965 return value;2024 return value;
1966 }2025 }
2026}
2027
19672028
1968async function addSwipeCallback(_, arg) {2029async function addSwipeCallback(_, arg) {
1969 const lastMessage = chat[chat.length - 1];2030 const lastMessage = chat[chat.length - 1];