import { registerSlashCommand } from '../../../slash-commands.js';
// eslint-disable-next-line no-unused-vars
import { QuickReplyApi } from '../api/QuickReplyApi.js';
export class SlashCommandHandler {
/**@type {QuickReplyApi}*/ api;
constructor(/**@type {QuickReplyApi}*/api) {
this.api = api;
}
init() {
registerSlashCommand('qr', (_, value) => this.executeQuickReplyByIndex(Number(value)), [], '(number) – activates the specified Quick Reply', true, true);
registerSlashCommand('qrset', ()=>toastr.warning('The command /qrset has been deprecated. Use /qr-set, /qr-set-on, and /qr-set-off instead.'), [], 'DEPRECATED – The command /qrset has been deprecated. Use /qr-set, /qr-set-on, and /qr-set-off instead.', true, true);
registerSlashCommand('qr-set', (args, value)=>this.toggleGlobalSet(value, args), [], '[visible=true] (number) – toggle global QR set', true, true);
registerSlashCommand('qr-set-on', (args, value)=>this.addGlobalSet(value, args), [], '[visible=true] (number) – activate global QR set', true, true);
registerSlashCommand('qr-set-off', (_, value)=>this.removeGlobalSet(value), [], '(number) – deactivate global QR set', true, true);
registerSlashCommand('qr-chat-set', (args, value)=>this.toggleChatSet(value, args), [], '[visible=true] (number) – toggle chat QR set', true, true);
registerSlashCommand('qr-chat-set-on', (args, value)=>this.addChatSet(value, args), [], '[visible=true] (number) – activate chat QR set', true, true);
registerSlashCommand('qr-chat-set-off', (_, value)=>this.removeChatSet(value), [], '(number) – deactivate chat QR set', true, true);
registerSlashCommand('qr-set-list', (_, value)=>this.listSets(value ?? 'all'), [], '(all|global|chat) – gets a list of the names of all quick reply sets', true, true);
registerSlashCommand('qr-list', (_, value)=>this.listQuickReplies(value), [], '(set name) – gets a list of the names of all quick replies in this quick reply set', true, true);
const qrArgs = `
label - string - text on the button, e.g., label=MyButton
set - string - name of the QR set, e.g., set=PresetName1
hidden - bool - whether the button should be hidden, e.g., hidden=true
startup - bool - auto execute on app startup, e.g., startup=true
user - bool - auto execute on user message, e.g., user=true
bot - bool - auto execute on AI message, e.g., bot=true
load - bool - auto execute on chat load, e.g., load=true
title - bool - title / tooltip to be shown on button, e.g., title="My Fancy Button"
`.trim();
const qrUpdateArgs = `
newlabel - string - new text for the button, e.g. newlabel=MyRenamedButton
${qrArgs}
`.trim();
registerSlashCommand('qr-create', (args, message)=>this.createQuickReply(args, message), [], `[arguments] (message)\n arguments:\n ${qrArgs} – creates a new Quick Reply, example: /qr-create set=MyPreset label=MyButton /echo 123`, true, true);
registerSlashCommand('qr-update', (args, message)=>this.updateQuickReply(args, message), [], `[arguments] (message)\n arguments:\n ${qrUpdateArgs} – updates Quick Reply, example: /qr-update set=MyPreset label=MyButton newlabel=MyRenamedButton /echo 123`, true, true);
registerSlashCommand('qr-delete', (args, name)=>this.deleteQuickReply(args, name), [], 'set=string [label] – deletes Quick Reply', true, true);
registerSlashCommand('qr-contextadd', (args, name)=>this.createContextItem(args, name), [], 'set=string label=string [chain=false] (preset name) – add context menu preset to a QR, example: /qr-contextadd set=MyPreset label=MyButton chain=true MyOtherPreset', true, true);
registerSlashCommand('qr-contextdel', (args, name)=>this.deleteContextItem(args, name), [], 'set=string label=string (preset name) – remove context menu preset from a QR, example: /qr-contextdel set=MyPreset label=MyButton MyOtherPreset', true, true);
registerSlashCommand('qr-contextclear', (args, label)=>this.clearContextMenu(args, label), [], 'set=string (label) – remove all context menu presets from a QR, example: /qr-contextclear set=MyPreset MyButton', true, true);
const presetArgs = `
nosend - bool - disable send / insert in user input (invalid for slash commands)
before - bool - place QR before user input
inject - bool - inject user input automatically (if disabled use {{input}})
`.trim();
registerSlashCommand('qr-set-create', (args, name)=>this.createSet(name, args), ['qr-presetadd'], `[arguments] (name)\n arguments:\n ${presetArgs} – create a new preset (overrides existing ones), example: /qr-set-add MyNewPreset`, true, true);
registerSlashCommand('qr-set-update', (args, name)=>this.updateSet(name, args), ['qr-presetupdate'], `[arguments] (name)\n arguments:\n ${presetArgs} – update an existing preset, example: /qr-set-update enabled=false MyPreset`, true, true);
registerSlashCommand('qr-set-delete', (args, name)=>this.deleteSet(name), ['qr-presetdelete'], `(name)\n arguments:\n ${presetArgs} – delete an existing preset, example: /qr-set-delete MyPreset`, true, true);
}
getSetByName(name) {
const set = this.api.getSetByName(name);
if (!set) {
toastr.error(`No Quick Reply Set with the name "${name}" could be found.`);
}
return set;
}
getQrByLabel(setName, label) {
const qr = this.api.getQrByLabel(setName, label);
if (!qr) {
toastr.error(`No Quick Reply with the label "${label}" could be found in the set "${setName}"`);
}
return qr;
}
async executeQuickReplyByIndex(idx) {
try {
return await this.api.executeQuickReplyByIndex(idx);
} catch (ex) {
toastr.error(ex.message);
}
}
toggleGlobalSet(name, args = {}) {
try {
this.api.toggleGlobalSet(name, JSON.parse(args.visible ?? 'true') === true);
} catch (ex) {
toastr.error(ex.message);
}
}
addGlobalSet(name, args = {}) {
try {
this.api.addGlobalSet(name, JSON.parse(args.visible ?? 'true') === true);
} catch (ex) {
toastr.error(ex.message);
}
}
removeGlobalSet(name) {
try {
this.api.removeGlobalSet(name);
} catch (ex) {
toastr.error(ex.message);
}
}
toggleChatSet(name, args = {}) {
try {
this.api.toggleChatSet(name, JSON.parse(args.visible ?? 'true') === true);
} catch (ex) {
toastr.error(ex.message);
}
}
addChatSet(name, args = {}) {
try {
this.api.addChatSet(name, JSON.parse(args.visible ?? 'true') === true);
} catch (ex) {
toastr.error(ex.message);
}
}
removeChatSet(name) {
try {
this.api.removeChatSet(name);
} catch (ex) {
toastr.error(ex.message);
}
}
createQuickReply(args, message) {
try {
this.api.createQuickReply(
args.set ?? '',
args.label ?? '',
{
message: message ?? '',
title: args.title,
isHidden: JSON.parse(args.hidden ?? 'false') === true,
executeOnStartup: JSON.parse(args.startup ?? 'false') === true,
executeOnUser: JSON.parse(args.user ?? 'false') === true,
executeOnAi: JSON.parse(args.bot ?? 'false') === true,
executeOnChatChange: JSON.parse(args.load ?? 'false') === true,
},
);
} catch (ex) {
toastr.error(ex.message);
}
}
updateQuickReply(args, message) {
try {
this.api.updateQuickReply(
args.set ?? '',
args.label ?? '',
{
newLabel: args.newlabel,
message: (message ?? '').trim().length > 0 ? message : undefined,
title: args.title,
isHidden: args.hidden,
executeOnStartup: args.startup,
executeOnUser: args.user,
executeOnAi: args.bot,
executeOnChatChange: args.load,
},
);
} catch (ex) {
toastr.error(ex.message);
}
}
deleteQuickReply(args, label) {
try {
this.api.deleteQuickReply(args.set, label);
} catch (ex) {
toastr.error(ex.message);
}
}
createContextItem(args, name) {
try {
this.api.createContextItem(
args.set,
args.label,
name,
JSON.parse(args.chain ?? 'false') === true,
);
} catch (ex) {
toastr.error(ex.message);
}
}
deleteContextItem(args, name) {
try {
this.api.deleteContextItem(args.set, args.label, name);
} catch (ex) {
toastr.error(ex.message);
}
}
clearContextMenu(args, label) {
try {
this.api.clearContextMenu(args.set, args.label ?? label);
} catch (ex) {
toastr.error(ex.message);
}
}
createSet(name, args) {
try {
this.api.createSet(
args.name ?? name ?? '',
{
disableSend: JSON.parse(args.nosend ?? 'false') === true,
placeBeforeInput: JSON.parse(args.before ?? 'false') === true,
injectInput: JSON.parse(args.inject ?? 'false') === true,
},
);
} catch (ex) {
toastr.error(ex.message);
}
}
updateSet(name, args) {
try {
this.api.updateSet(
args.name ?? name ?? '',
{
disableSend: args.nosend !== undefined ? JSON.parse(args.nosend ?? 'false') === true : undefined,
placeBeforeInput: args.before !== undefined ? JSON.parse(args.before ?? 'false') === true : undefined,
injectInput: args.inject !== undefined ? JSON.parse(args.inject ?? 'false') === true : undefined,
},
);
} catch (ex) {
toastr.error(ex.message);
}
}
deleteSet(name) {
try {
this.api.deleteSet(name ?? '');
} catch (ex) {
toastr.error(ex.message);
}
}
listSets(source) {
try {
switch (source) {
case 'global':
return this.api.listGlobalSets();
case 'chat':
return this.api.listChatSets();
default:
return this.api.listSets();
}
} catch (ex) {
toastr.error(ex.message);
}
}
listQuickReplies(name) {
try {
return this.api.listQuickReplies(name);
} catch (ex) {
toastr.error(ex.message);
}
}
}