77 simpleEscapeForDiscord
88} = require ( './strings' ) ;
99
10+ function attachHelp ( cmdFunc , commandName , additionalCommands , { overrideHelpFields = { } } = { } ) {
11+ const addlCommandsHelp = Object . entries ( additionalCommands ?? { } )
12+ . filter ( ( [ k ] ) => k . indexOf ( '_' ) !== 0 )
13+ . reduce ( ( a , [ k , v ] ) => ( {
14+ [ k ] : {
15+ text : k
16+ } ,
17+ ...a
18+ } ) , { } ) ;
19+
20+ const defaultHelp = {
21+ title : `Add or remove strings to the \`${ commandName } \` list.` ,
22+ usage : 'network subcommand [string]' ,
23+ subcommands : {
24+ add : {
25+ header : 'Notes' ,
26+ text : `Adds \`string\` to the \`${ commandName } \` list.`
27+ } ,
28+ remove : {
29+ header : 'Notes' ,
30+ text : `Removes \`string\` from the \`${ commandName } \` list.`
31+ } ,
32+ clear : {
33+ header : 'Notes' ,
34+ text : `Removes all strings from the \`${ commandName } \` list.`
35+ } ,
36+ ...addlCommandsHelp
37+ }
38+ } ;
39+
40+ cmdFunc . __drcHelp = ( ) => {
41+ return {
42+ ...defaultHelp ,
43+ ...overrideHelpFields
44+ } ;
45+ } ;
46+ }
47+
1048// keySubstitue only applies (if set) to additionalCommands!
11- function generateListManagementUCExport ( commandName , additionalCommands , disallowClear = false , keySubstitute = null ) {
49+ function generateListManagementUCExport ( commandName ,
50+ additionalCommands ,
51+ disallowClear = false ,
52+ keySubstitute = null ,
53+ callOnChange = null ,
54+ overrideHelpFields = { }
55+ ) {
1256 const f = async function ( context , ...a ) {
1357 const [ netStub , cmd ] = a ;
1458
@@ -35,10 +79,21 @@ function generateListManagementUCExport (commandName, additionalCommands, disall
3579 return convertDiscordChannelsToIRCInString ( a . slice ( 2 ) . join ( ' ' ) , context ) ;
3680 } ;
3781
82+ const addRmArgs = async ( ) => [ key , await argStr ( ) ] ;
83+
84+ let onChangeWrapper = async ( a ) => a ;
85+ if ( callOnChange ) {
86+ onChangeWrapper = async ( changed ) => {
87+ if ( changed ) {
88+ await callOnChange ( context , ...await addRmArgs ( ) ) ;
89+ }
90+ } ;
91+ }
92+
3893 return scopedRedisClient ( async ( redis ) => {
3994 switch ( cmd ) {
4095 case 'add' :
41- await redis . sadd ( key , await argStr ( ) ) ;
96+ await onChangeWrapper ( await redis . sadd ( ... await addRmArgs ( ) ) ) ;
4297 break ;
4398 case 'clear' :
4499 // this really should be a button for confirmation instead of hardcoded!
@@ -47,7 +102,7 @@ function generateListManagementUCExport (commandName, additionalCommands, disall
47102 }
48103 break ;
49104 case 'remove' :
50- await redis . srem ( key , await argStr ( ) ) ;
105+ await onChangeWrapper ( await redis . srem ( ... await addRmArgs ( ) ) ) ;
51106 break ;
52107 }
53108
@@ -68,42 +123,22 @@ function generateListManagementUCExport (commandName, additionalCommands, disall
68123 } ) ;
69124 } ;
70125
71- const addlCommandsHelp = Object . entries ( additionalCommands ?? { } )
72- . filter ( ( [ k ] ) => k . indexOf ( '_' ) !== 0 )
73- . reduce ( ( a , [ k , v ] ) => ( {
74- [ k ] : {
75- text : k
76- } ,
77- ...a
78- } ) , { } ) ;
79-
80- f . __drcHelp = ( ) => {
81- return {
82- title : `Add or remove strings to the \`${ commandName } \` list.` ,
83- usage : 'network subcommand [string]' ,
84- subcommands : {
85- add : {
86- header : 'Notes' ,
87- text : `Adds \`string\` to the \`${ commandName } \` list.`
88- } ,
89- remove : {
90- header : 'Notes' ,
91- text : `Removes \`string\` from the \`${ commandName } \` list.`
92- } ,
93- clear : {
94- header : 'Notes' ,
95- text : `Removes all strings from the \`${ commandName } \` list.`
96- } ,
97- ...addlCommandsHelp
98- }
99- } ;
100- } ;
101-
126+ attachHelp ( f , commandName , additionalCommands , { overrideHelpFields } ) ;
102127 return f ;
103128}
104129
105- function generatePerChanListManagementUCExport ( commandName , additionalCommands , enforceChannelSpec = true ) {
106- return function ( context , ...a ) {
130+ function generateListManagementUCExportOpts ( commandName , {
131+ additionalCommands,
132+ disallowClear = false ,
133+ keySubstitute = null ,
134+ callOnChange = null ,
135+ overrideHelpFields = { }
136+ } = { } ) {
137+ return generateListManagementUCExport ( commandName , additionalCommands , disallowClear , keySubstitute , callOnChange , overrideHelpFields ) ;
138+ }
139+
140+ function generatePerChanListManagementUCExport ( commandName , additionalCommands , enforceChannelSpec = true , options = { } ) {
141+ const cmdFunc = function ( context , ...a ) {
107142 const [ netStub , channelIdSpec ] = context . options . _ ;
108143 const { network } = matchNetwork ( netStub ) ;
109144 let channel = channelIdSpec ;
@@ -122,17 +157,24 @@ function generatePerChanListManagementUCExport (commandName, additionalCommands,
122157 return addlCmd ( { key, network, ...context } , ...a ) ;
123158 }
124159
125- const cmdFunctor = generateListManagementUCExport ( `${ commandName } _${ key } ` , additionalCommands ) ;
160+ const cmdFunctor = generateListManagementUCExportOpts ( `${ commandName } _${ key } ` , {
161+ ...options ,
162+ additionalCommands
163+ } ) ;
126164
127165 context . options . _ [ 1 ] = context . options . _ [ 0 ] ;
128166 a [ 1 ] = a [ 0 ] ;
129167 context . options . _ . shift ( ) ;
130168 a . shift ( ) ;
131169 return cmdFunctor ( context , ...a ) ;
132170 } ;
171+
172+ attachHelp ( cmdFunc , commandName , additionalCommands , options ) ;
173+ return cmdFunc ;
133174}
134175
135176module . exports = {
136177 generateListManagementUCExport,
178+ generateListManagementUCExportOpts,
137179 generatePerChanListManagementUCExport
138180} ;
0 commit comments