import { SlashCommand } from '../../../slash-commands/SlashCommand.js'; import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from '../../../slash-commands/SlashCommandArgument.js'; import { enumIcons } from '../../../slash-commands/SlashCommandCommonEnumsProvider.js'; import { SlashCommandEnumValue, enumTypes } from '../../../slash-commands/SlashCommandEnumValue.js'; import { SlashCommandParser } from '../../../slash-commands/SlashCommandParser.js'; import { isTrueBoolean } from '../../../utils.js'; // eslint-disable-next-line no-unused-vars import { QuickReplyApi } from '../api/QuickReplyApi.js'; import { QuickReply } from './QuickReply.js'; import { QuickReplySet } from './QuickReplySet.js'; export class SlashCommandHandler { /**@type {QuickReplyApi}*/ api; constructor(/**@type {QuickReplyApi}*/api) { this.api = api; } init() { function getExecutionIcons(/**@type {QuickReply} */ qr) { let icons = ''; if (qr.preventAutoExecute) icons += '🚫'; if (qr.isHidden) icons += '👁️'; if (qr.executeOnStartup) icons += '🚀'; if (qr.executeOnUser) icons += enumIcons.user; if (qr.executeOnAi) icons += enumIcons.assistant; if (qr.executeOnChatChange) icons += '💬'; if (qr.executeOnGroupMemberDraft) icons += enumIcons.group; return icons; } const localEnumProviders = { /** All quick reply sets, optionally filtering out sets that wer already used in the "set" named argument */ qrSets: (executor) => QuickReplySet.list.filter(qrSet => qrSet.name != String(executor.namedArgumentList.find(x => x.name == 'set')?.value)) .map(qrSet => new SlashCommandEnumValue(qrSet.name, null, enumTypes.enum, 'S')), /** All QRs inside a set, utilizing the "set" named argument */ qrEntries: (executor) => QuickReplySet.get(String(executor.namedArgumentList.find(x => x.name == 'set')?.value))?.qrList.map(qr => { const icons = getExecutionIcons(qr); const message = `${qr.automationId ? `[${qr.automationId}]` : ''}${icons ? `[auto: ${icons}]` : ''} ${qr.title || qr.message}`.trim(); return new SlashCommandEnumValue(qr.label, message, enumTypes.enum, enumIcons.qr); }) ?? [], /** All QRs as a set.name string, to be able to execute, for example via the /run command */ qrExecutables: () => { const globalSetList = this.api.settings.config.setList; const chatSetList = this.api.settings.chatConfig?.setList; const globalQrs = globalSetList.map(link => link.set.qrList.map(qr => ({ set: link.set, qr }))).flat(); const chatQrs = chatSetList?.map(link => link.set.qrList.map(qr => ({ set: link.set, qr }))).flat() ?? []; const otherQrs = QuickReplySet.list.filter(set => !globalSetList.some(link => link.set.name === set.name && !chatSetList?.some(link => link.set.name === set.name))) .map(set => set.qrList.map(qr => ({ set, qr }))).flat(); return [ ...globalQrs.map(x => new SlashCommandEnumValue(`${x.set.name}.${x.qr.label}`, `[global] ${x.qr.title || x.qr.message}`, enumTypes.name, enumIcons.qr)), ...chatQrs.map(x => new SlashCommandEnumValue(`${x.set.name}.${x.qr.label}`, `[chat] ${x.qr.title || x.qr.message}`, enumTypes.enum, enumIcons.qr)), ...otherQrs.map(x => new SlashCommandEnumValue(`${x.set.name}.${x.qr.label}`, `${x.qr.title || x.qr.message}`, enumTypes.qr, enumIcons.qr)), ]; }, } window['qrEnumProviderExecutables'] = localEnumProviders.qrExecutables; SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'qr', callback: (_, value) => this.executeQuickReplyByIndex(Number(value)), unnamedArgumentList: [ new SlashCommandArgument( 'number', [ARGUMENT_TYPE.NUMBER], true, ), ], helpString: 'Activates the specified Quick Reply', })); SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'qrset', callback: () => { toastr.warning('The command /qrset has been deprecated. Use /qr-set, /qr-set-on, and /qr-set-off instead.'); return ''; }, helpString: 'DEPRECATED – The command /qrset has been deprecated. Use /qr-set, /qr-set-on, and /qr-set-off instead.', })); SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'qr-set', callback: (args, value) => { this.toggleGlobalSet(value, args); return ''; }, namedArgumentList: [ new SlashCommandNamedArgument( 'visible', 'set visibility', [ARGUMENT_TYPE.BOOLEAN], false, false, 'true', ), ], unnamedArgumentList: [ SlashCommandArgument.fromProps({ description: 'QR set name', typeList: [ARGUMENT_TYPE.STRING], isRequired: true, enumProvider: localEnumProviders.qrSets, }), ], helpString: 'Toggle global QR set', })); SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'qr-set-on', callback: (args, value) => { this.addGlobalSet(value, args); return ''; }, namedArgumentList: [ new SlashCommandNamedArgument( 'visible', 'set visibility', [ARGUMENT_TYPE.BOOLEAN], false, false, 'true', ), ], unnamedArgumentList: [ SlashCommandArgument.fromProps({ description: 'QR set name', typeList: [ARGUMENT_TYPE.STRING], isRequired: true, enumProvider: localEnumProviders.qrSets, }), ], helpString: 'Activate global QR set', })); SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'qr-set-off', callback: (_, value) => { this.removeGlobalSet(value); return ''; }, unnamedArgumentList: [ SlashCommandArgument.fromProps({ description: 'QR set name', typeList: [ARGUMENT_TYPE.STRING], isRequired: true, enumProvider: localEnumProviders.qrSets, }), ], helpString: 'Deactivate global QR set', })); SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'qr-chat-set', callback: (args, value) => { this.toggleChatSet(value, args); return ''; }, namedArgumentList: [ new SlashCommandNamedArgument( 'visible', 'set visibility', [ARGUMENT_TYPE.BOOLEAN], false, false, 'true', ), ], unnamedArgumentList: [ SlashCommandArgument.fromProps({ description: 'QR set name', typeList: [ARGUMENT_TYPE.STRING], isRequired: true, enumProvider: localEnumProviders.qrSets, }), ], helpString: 'Toggle chat QR set', })); SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'qr-chat-set-on', callback: (args, value) => { this.addChatSet(value, args); return ''; }, namedArgumentList: [ new SlashCommandNamedArgument( 'visible', 'whether the QR set should be visible', [ARGUMENT_TYPE.BOOLEAN], false, false, 'true', ), ], unnamedArgumentList: [ SlashCommandArgument.fromProps({ description: 'QR set name', typeList: [ARGUMENT_TYPE.STRING], isRequired: true, enumProvider: localEnumProviders.qrSets, }), ], helpString: 'Activate chat QR set', })); SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'qr-chat-set-off', callback: (_, value) => { this.removeChatSet(value); return ''; }, unnamedArgumentList: [ SlashCommandArgument.fromProps({ description: 'QR set name', typeList: [ARGUMENT_TYPE.STRING], isRequired: true, enumProvider: localEnumProviders.qrSets, }), ], helpString: 'Deactivate chat QR set', })); SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'qr-set-list', callback: (_, value) => JSON.stringify(this.listSets(value ?? 'all')), returns: 'list of QR sets', namedArgumentList: [], unnamedArgumentList: [ new SlashCommandArgument( 'set type', [ARGUMENT_TYPE.STRING], false, false, 'all', ['all', 'global', 'chat'], ), ], helpString: 'Gets a list of the names of all quick reply sets.', })); SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'qr-list', callback: (_, value) => { return JSON.stringify(this.listQuickReplies(value)); }, returns: 'list of QRs', namedArgumentList: [], unnamedArgumentList: [ SlashCommandArgument.fromProps({ description: 'QR set name', typeList: [ARGUMENT_TYPE.STRING], isRequired: true, enumProvider: localEnumProviders.qrSets, }), ], helpString: 'Gets a list of the names of all quick replies in this quick reply set.', })); const qrArgs = [ SlashCommandNamedArgument.fromProps({ name: 'set', description: 'name of the QR set, e.g., set=PresetName1', typeList: [ARGUMENT_TYPE.STRING], isRequired: true, enumProvider: localEnumProviders.qrSets, }), SlashCommandNamedArgument.fromProps({ name: 'label', description: 'text on the button, e.g., label=MyButton', typeList: [ARGUMENT_TYPE.STRING], isRequired: true, enumProvider: localEnumProviders.qrLabels, }), new SlashCommandNamedArgument('hidden', 'whether the button should be hidden, e.g., hidden=true', [ARGUMENT_TYPE.BOOLEAN], false, false, 'false'), new SlashCommandNamedArgument('startup', 'auto execute on app startup, e.g., startup=true', [ARGUMENT_TYPE.BOOLEAN], false, false, 'false'), new SlashCommandNamedArgument('user', 'auto execute on user message, e.g., user=true', [ARGUMENT_TYPE.BOOLEAN], false, false, 'false'), new SlashCommandNamedArgument('bot', 'auto execute on AI message, e.g., bot=true', [ARGUMENT_TYPE.BOOLEAN], false, false, 'false'), new SlashCommandNamedArgument('load', 'auto execute on chat load, e.g., load=true', [ARGUMENT_TYPE.BOOLEAN], false, false, 'false'), new SlashCommandNamedArgument('group', 'auto execute on group member selection, e.g., group=true', [ARGUMENT_TYPE.BOOLEAN], false, false, 'false'), new SlashCommandNamedArgument('title', 'title / tooltip to be shown on button, e.g., title="My Fancy Button"', [ARGUMENT_TYPE.STRING], false), ]; const qrUpdateArgs = [ new SlashCommandNamedArgument('newlabel', 'new text for the button', [ARGUMENT_TYPE.STRING], false), ]; SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'qr-create', callback: (args, message) => { this.createQuickReply(args, message); return ''; }, namedArgumentList: qrArgs, unnamedArgumentList: [ new SlashCommandArgument( 'command', [ARGUMENT_TYPE.STRING], true, ), ], helpString: `
/qr-create set=MyPreset label=MyButton /echo 123
/qr-update set=MyPreset label=MyButton newlabel=MyRenamedButton /echo 123
/qr-contextadd set=MyPreset label=MyButton chain=true MyOtherPreset
/qr-contextdel set=MyPreset label=MyButton MyOtherPreset
/qr-contextclear set=MyPreset MyButton
/qr-set-add MyNewPreset
/qr-set-update enabled=false MyPreset
/qr-set-delete MyPreset