@@ -10,6 +10,7 @@ import {
10
10
import { Command } from "../../shared/command/command" ;
11
11
import { Subcommand } from "../../shared/command/subcommand" ;
12
12
import { getTextChannel , prismaClient } from "../.." ;
13
+ import { Prisma } from "@prisma/client"
13
14
import {
14
15
batphoneChannelId ,
15
16
raiderRoleId ,
@@ -154,7 +155,7 @@ class setBp extends Subcommand {
154
155
key = message . split ( " " ) [ 0 ] . toLowerCase ( ) ;
155
156
}
156
157
const location = this . getOption ( "location" , interaction )
157
- ?. value as string ;
158
+ ?. value as string || "" ;
158
159
key = truncate ( String ( key ) , { length : 100 } ) ; // max option length = 100
159
160
await prismaClient . batphone . create ( {
160
161
data : {
@@ -174,7 +175,15 @@ class setBp extends Subcommand {
174
175
}
175
176
} catch ( err ) {
176
177
console . error ( err ) ;
177
- interaction . editReply ( "Failed save batphone message: " + err ) ;
178
+ let errMsg = err ;
179
+ if ( err instanceof Prisma . PrismaClientKnownRequestError ) {
180
+ if ( err . code === 'P2002' ) {
181
+ errMsg = "Key already exists" ;
182
+ } else {
183
+ errMsg = err . code ;
184
+ }
185
+ }
186
+ interaction . editReply ( "Failed save batphone message: " + errMsg ) ;
178
187
}
179
188
}
180
189
public async getOptionAutocomplete (
@@ -283,7 +292,11 @@ class getBp extends Subcommand {
283
292
key : val ,
284
293
} ,
285
294
} ) ;
286
- const message = savedMsg ?. message || val ;
295
+ const message = savedMsg ?. message ;
296
+ if ( ! message ) {
297
+ throw new Error ( `No batphone with key ${ val } ` ) ;
298
+ }
299
+
287
300
if ( typeof message === "string" ) {
288
301
const formattedMessage = message . replace (
289
302
/ \\ n / g,
@@ -297,7 +310,7 @@ ${formattedMessage}
297
310
298
311
--------
299
312
Location: ${ savedMsg ?. location || "NO LOCATION SET" }
300
- To change this message, use \`/bp unset ${ key } \` and then \`/bp set \` to set a new message.
313
+ To change this message, use \`/bp update \` to set a new message.
301
314
` ;
302
315
savedMsg
303
316
? console . log (
@@ -344,6 +357,110 @@ To change this message, use \`/bp unset ${key}\` and then \`/bp set\` to set a n
344
357
}
345
358
}
346
359
360
+ class updateBp extends Subcommand {
361
+ public async execute ( interaction : CommandInteraction < CacheType > ) {
362
+ // authorize
363
+ authorizeByMemberRoles (
364
+ [ officerRoleId , modRoleId , knightRoleId ] ,
365
+ interaction
366
+ ) ;
367
+
368
+ const message = this . getOption ( "message" , interaction ) ?. value ;
369
+ try {
370
+ if ( typeof message === "string" ) {
371
+ if ( message . length > 2000 ) {
372
+ // max message length is 2000 chars
373
+ throw new Error ( "Message is too long." ) ;
374
+ }
375
+ let key = this . getOption ( "key" , interaction ) ?. value ;
376
+ if ( ! key ) {
377
+ key = message . split ( " " ) [ 0 ] . toLowerCase ( ) ;
378
+ }
379
+ const location = this . getOption ( "location" , interaction )
380
+ ?. value as string || "" ;
381
+ key = truncate ( String ( key ) , { length : 100 } ) ; // max option length = 100
382
+
383
+ let updateNoLocation = {
384
+ message : message
385
+ } ;
386
+
387
+ let updateWithLocation = {
388
+ message : message ,
389
+ location : location
390
+ } ;
391
+
392
+ let update = location ? updateWithLocation : updateNoLocation ;
393
+
394
+ await prismaClient . batphone . update ( {
395
+ where : {
396
+ key : key
397
+ } ,
398
+ data : update
399
+ } ) ;
400
+
401
+ console . log (
402
+ `Updated batphone - key: ${ key } , location: ${
403
+ location || "unset"
404
+ } , message: ${ message } `
405
+ ) ;
406
+ interaction . editReply ( "Updated Batphone: " + key ) ;
407
+ } else {
408
+ throw error ;
409
+ }
410
+ } catch ( err ) {
411
+ console . error ( err ) ;
412
+ let errMsg = err ;
413
+ if ( err instanceof Prisma . PrismaClientKnownRequestError ) {
414
+ if ( err . code === 'P2025' ) {
415
+ errMsg = "No batphone found with the provided key" ;
416
+ } else {
417
+ errMsg = err . code ;
418
+ }
419
+ }
420
+ interaction . editReply ( "Failed update batphone message: " + errMsg ) ;
421
+ }
422
+ }
423
+
424
+ public async getOptionAutocomplete (
425
+ option : string ,
426
+ interaction : AutocompleteInteraction < CacheType >
427
+ ) : Promise <
428
+ ApplicationCommandOptionChoiceData < string | number > [ ] | undefined
429
+ > {
430
+ switch ( option ) {
431
+ case "location" :
432
+ return LocationService . getInstance ( ) . getLocationOptions ( ) ;
433
+ default :
434
+ return [ ] ;
435
+ }
436
+ }
437
+
438
+ public get command ( ) {
439
+ return super . command
440
+ . addStringOption ( ( o ) =>
441
+ o
442
+ . setName ( "message" )
443
+ . setDescription ( "BP Message" )
444
+ . setRequired ( true )
445
+ . setAutocomplete ( false )
446
+ )
447
+ . addStringOption ( ( o ) =>
448
+ o
449
+ . setName ( "key" )
450
+ . setDescription ( "Key (optional" )
451
+ . setRequired ( false )
452
+ . setAutocomplete ( false )
453
+ )
454
+ . addStringOption ( ( o ) =>
455
+ o
456
+ . setName ( "location" )
457
+ . setDescription ( "Location of the batphone" )
458
+ . setRequired ( false )
459
+ . setAutocomplete ( true )
460
+ ) ;
461
+ }
462
+ }
463
+
347
464
export const batphoneCommand = new Command (
348
465
"bp" ,
349
466
"set and send batphone messages" ,
@@ -352,5 +469,6 @@ export const batphoneCommand = new Command(
352
469
new setBp ( "set" , "save a BP preset" ) ,
353
470
new unsetBp ( "unset" , "remove BP preset" ) ,
354
471
new getBp ( "get" , "show BP message in this channel" ) ,
472
+ new updateBp ( "update" , "update a BP" )
355
473
]
356
474
) ;
0 commit comments