2023-12-21 18:44:58 +00:00
import { registerSlashCommand } from '../../../slash-commands.js' ;
2023-12-22 13:55:37 +00:00
// eslint-disable-next-line no-unused-vars
2023-12-21 20:05:42 +00:00
import { QuickReplyApi } from '../api/QuickReplyApi.js' ;
2023-12-21 18:44:58 +00:00
export class SlashCommandHandler {
2023-12-21 20:05:42 +00:00
/**@type {QuickReplyApi}*/ api ;
2023-12-21 18:44:58 +00:00
2023-12-21 20:05:42 +00:00
constructor ( /**@type {QuickReplyApi}*/ api ) {
this . api = api ;
2023-12-21 18:44:58 +00:00
}
init ( ) {
registerSlashCommand ( 'qr' , ( _ , value ) => this . executeQuickReplyByIndex ( Number ( value ) ) , [ ] , '<span class="monospace">(number)</span> – 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.' ) , [ ] , '<strong>DEPRECATED</strong> – 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 ) , [ ] , '<span class="monospace">[visible=true] (number)</span> – toggle global QR set' , true , true ) ;
registerSlashCommand ( 'qr-set-on' , ( args , value ) => this . addGlobalSet ( value , args ) , [ ] , '<span class="monospace">[visible=true] (number)</span> – activate global QR set' , true , true ) ;
registerSlashCommand ( 'qr-set-off' , ( _ , value ) => this . removeGlobalSet ( value ) , [ ] , '<span class="monospace">(number)</span> – deactivate global QR set' , true , true ) ;
registerSlashCommand ( 'qr-chat-set' , ( args , value ) => this . toggleChatSet ( value , args ) , [ ] , '<span class="monospace">[visible=true] (number)</span> – toggle chat QR set' , true , true ) ;
registerSlashCommand ( 'qr-chat-set-on' , ( args , value ) => this . addChatSet ( value , args ) , [ ] , '<span class="monospace">[visible=true] (number)</span> – activate chat QR set' , true , true ) ;
registerSlashCommand ( 'qr-chat-set-off' , ( _ , value ) => this . removeChatSet ( value ) , [ ] , '<span class="monospace">(number)</span> – deactivate chat QR set' , true , true ) ;
2023-12-23 16:36:41 +00:00
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 ) ;
2023-12-21 18:44:58 +00:00
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
2024-01-16 22:36:55 +00:00
title - string - title / tooltip to be shown on button , e . g . , title = "My Fancy Button"
2023-12-21 18:44:58 +00:00
` .trim();
const qrUpdateArgs = `
2023-12-22 12:15:44 +00:00
newlabel - string - new text for the button , e . g . newlabel = MyRenamedButton
2023-12-21 18:44:58 +00:00
$ { qrArgs }
` .trim();
2023-12-22 12:15:44 +00:00
registerSlashCommand ( 'qr-create' , ( args , message ) => this . createQuickReply ( args , message ) , [ ] , ` <span class="monospace" style="white-space:pre-line;">[arguments] (message) \n arguments: \n ${ qrArgs } </span> – creates a new Quick Reply, example: <tt>/qr-create set=MyPreset label=MyButton /echo 123</tt> ` , true , true ) ;
registerSlashCommand ( 'qr-update' , ( args , message ) => this . updateQuickReply ( args , message ) , [ ] , ` <span class="monospace" style="white-space:pre-line;">[arguments] (message) \n arguments: \n ${ qrUpdateArgs } </span> – updates Quick Reply, example: <tt>/qr-update set=MyPreset label=MyButton newlabel=MyRenamedButton /echo 123</tt> ` , true , true ) ;
registerSlashCommand ( 'qr-delete' , ( args , name ) => this . deleteQuickReply ( args , name ) , [ ] , '<span class="monospace">set=string [label]</span> – deletes Quick Reply' , true , true ) ;
registerSlashCommand ( 'qr-contextadd' , ( args , name ) => this . createContextItem ( args , name ) , [ ] , '<span class="monospace">set=string label=string [chain=false] (preset name)</span> – add context menu preset to a QR, example: <tt>/qr-contextadd set=MyPreset label=MyButton chain=true MyOtherPreset</tt>' , true , true ) ;
registerSlashCommand ( 'qr-contextdel' , ( args , name ) => this . deleteContextItem ( args , name ) , [ ] , '<span class="monospace">set=string label=string (preset name)</span> – remove context menu preset from a QR, example: <tt>/qr-contextdel set=MyPreset label=MyButton MyOtherPreset</tt>' , true , true ) ;
registerSlashCommand ( 'qr-contextclear' , ( args , label ) => this . clearContextMenu ( args , label ) , [ ] , '<span class="monospace">set=string (label)</span> – remove all context menu presets from a QR, example: <tt>/qr-contextclear set=MyPreset MyButton</tt>' , true , true ) ;
2023-12-21 18:44:58 +00:00
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();
2023-12-22 12:56:06 +00:00
registerSlashCommand ( 'qr-set-create' , ( args , name ) => this . createSet ( name , args ) , [ 'qr-presetadd' ] , ` <span class="monospace" style="white-space:pre-line;">[arguments] (name) \n arguments: \n ${ presetArgs } </span> – create a new preset (overrides existing ones), example: <tt>/qr-set-add MyNewPreset</tt> ` , true , true ) ;
registerSlashCommand ( 'qr-set-update' , ( args , name ) => this . updateSet ( name , args ) , [ 'qr-presetupdate' ] , ` <span class="monospace" style="white-space:pre-line;">[arguments] (name) \n arguments: \n ${ presetArgs } </span> – update an existing preset, example: <tt>/qr-set-update enabled=false MyPreset</tt> ` , true , true ) ;
registerSlashCommand ( 'qr-set-delete' , ( args , name ) => this . deleteSet ( name ) , [ 'qr-presetdelete' ] , ` <span class="monospace" style="white-space:pre-line;">(name) \n arguments: \n ${ presetArgs } </span> – delete an existing preset, example: <tt>/qr-set-delete MyPreset</tt> ` , true , true ) ;
2023-12-21 18:44:58 +00:00
}
getSetByName ( name ) {
2023-12-21 20:05:42 +00:00
const set = this . api . getSetByName ( name ) ;
2023-12-21 18:44:58 +00:00
if ( ! set ) {
toastr . error ( ` No Quick Reply Set with the name " ${ name } " could be found. ` ) ;
}
return set ;
}
getQrByLabel ( setName , label ) {
2023-12-21 20:05:42 +00:00
const qr = this . api . getQrByLabel ( setName , label ) ;
2023-12-21 18:44:58 +00:00
if ( ! qr ) {
2023-12-21 20:05:42 +00:00
toastr . error ( ` No Quick Reply with the label " ${ label } " could be found in the set " ${ setName } " ` ) ;
2023-12-21 18:44:58 +00:00
}
return qr ;
}
async executeQuickReplyByIndex ( idx ) {
2023-12-21 20:05:42 +00:00
try {
return await this . api . executeQuickReplyByIndex ( idx ) ;
} catch ( ex ) {
toastr . error ( ex . message ) ;
2023-12-21 18:44:58 +00:00
}
}
toggleGlobalSet ( name , args = { } ) {
2023-12-22 12:55:25 +00:00
try {
this . api . toggleGlobalSet ( name , JSON . parse ( args . visible ? ? 'true' ) === true ) ;
} catch ( ex ) {
toastr . error ( ex . message ) ;
}
2023-12-21 18:44:58 +00:00
}
addGlobalSet ( name , args = { } ) {
2023-12-22 12:55:25 +00:00
try {
this . api . addGlobalSet ( name , JSON . parse ( args . visible ? ? 'true' ) === true ) ;
} catch ( ex ) {
toastr . error ( ex . message ) ;
}
2023-12-21 18:44:58 +00:00
}
removeGlobalSet ( name ) {
2023-12-22 12:55:25 +00:00
try {
this . api . removeGlobalSet ( name ) ;
} catch ( ex ) {
toastr . error ( ex . message ) ;
}
2023-12-21 18:44:58 +00:00
}
toggleChatSet ( name , args = { } ) {
2023-12-22 12:55:25 +00:00
try {
this . api . toggleChatSet ( name , JSON . parse ( args . visible ? ? 'true' ) === true ) ;
} catch ( ex ) {
toastr . error ( ex . message ) ;
}
2023-12-21 18:44:58 +00:00
}
addChatSet ( name , args = { } ) {
2023-12-22 12:55:25 +00:00
try {
this . api . addChatSet ( name , JSON . parse ( args . visible ? ? 'true' ) === true ) ;
} catch ( ex ) {
toastr . error ( ex . message ) ;
}
2023-12-21 18:44:58 +00:00
}
removeChatSet ( name ) {
2023-12-22 12:55:25 +00:00
try {
this . api . removeChatSet ( name ) ;
} catch ( ex ) {
toastr . error ( ex . message ) ;
}
2023-12-21 18:44:58 +00:00
}
createQuickReply ( args , message ) {
2023-12-21 20:05:42 +00:00
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 ) ;
}
2023-12-21 18:44:58 +00:00
}
updateQuickReply ( args , message ) {
2023-12-21 20:05:42 +00:00
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 ) ;
}
2023-12-21 18:44:58 +00:00
}
deleteQuickReply ( args , label ) {
2023-12-21 20:05:42 +00:00
try {
this . api . deleteQuickReply ( args . set , label ) ;
} catch ( ex ) {
toastr . error ( ex . message ) ;
}
2023-12-21 18:44:58 +00:00
}
createContextItem ( args , name ) {
2023-12-21 20:05:42 +00:00
try {
this . api . createContextItem (
args . set ,
args . label ,
name ,
JSON . parse ( args . chain ? ? 'false' ) === true ,
) ;
} catch ( ex ) {
toastr . error ( ex . message ) ;
}
2023-12-21 18:44:58 +00:00
}
deleteContextItem ( args , name ) {
2023-12-21 20:05:42 +00:00
try {
this . api . deleteContextItem ( args . set , args . label , name ) ;
} catch ( ex ) {
toastr . error ( ex . message ) ;
}
2023-12-21 18:44:58 +00:00
}
clearContextMenu ( args , label ) {
2023-12-22 12:55:25 +00:00
try {
this . api . clearContextMenu ( args . set , args . label ? ? label ) ;
} catch ( ex ) {
toastr . error ( ex . message ) ;
}
2023-12-21 18:44:58 +00:00
}
createSet ( name , args ) {
2023-12-22 12:55:25 +00:00
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 ) ;
}
2023-12-21 18:44:58 +00:00
}
updateSet ( name , args ) {
2023-12-22 12:55:25 +00:00
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 ) ;
}
}
2023-12-22 12:56:06 +00:00
deleteSet ( name ) {
try {
this . api . deleteSet ( name ? ? '' ) ;
} catch ( ex ) {
toastr . error ( ex . message ) ;
}
2023-12-21 18:44:58 +00:00
}
2023-12-23 16:36:41 +00:00
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 ) ;
}
}
2023-12-21 18:44:58 +00:00
}