1
+ import { fail } from "node:assert" ;
1
2
import {
2
3
type TranslationKey ,
3
4
embedColors ,
@@ -47,13 +48,13 @@ type JumbleGameContext = {
47
48
} ;
48
49
49
50
type ArtistInfo = {
50
- isGroup : boolean ;
51
- type : TranslationKey | null ;
52
- gender : TranslationKey | null ;
53
- musicGenre : string | undefined ;
54
- country : string | null ;
55
- begin : string | null ;
56
- end : string | null ;
51
+ isGroup ? : boolean ;
52
+ musicGenre ?: string ;
53
+ type ? : TranslationKey | null ;
54
+ gender ?: TranslationKey | null ;
55
+ country ? : string | null ;
56
+ begin ? : string | null ;
57
+ end ? : string | null ;
57
58
} ;
58
59
59
60
enum ButtonCustomIds {
@@ -87,18 +88,8 @@ export default class JumbleGameHandler {
87
88
name : string ,
88
89
mbid ?: string | null ,
89
90
) : Promise < JumbleGameContext | null > {
90
- const artistInfo = await this . getArtistInfo ( name , mbid ) ;
91
- if ( ! artistInfo ) return null ;
92
-
93
- const hints = this . createHints (
94
- playcount ,
95
- artistInfo . type ,
96
- artistInfo . country ,
97
- artistInfo . musicGenre ,
98
- artistInfo . gender ,
99
- artistInfo . begin ,
100
- artistInfo . end ,
101
- ) ;
91
+ const artistInfo : ArtistInfo = ( await this . getArtistInfo ( name , mbid ) ) ?? { } ;
92
+ const hints = this . createHints ( playcount , artistInfo ) ;
102
93
103
94
const gameContext : JumbleGameContext = {
104
95
hints,
@@ -203,15 +194,9 @@ export default class JumbleGameHandler {
203
194
return collector ;
204
195
}
205
196
206
- private createHints (
207
- playcount : number ,
208
- type : TranslationKey | null ,
209
- country : string | null ,
210
- musicGenre : string | undefined ,
211
- gender : TranslationKey | null ,
212
- begin : string | null ,
213
- end : string | null ,
214
- ) : string [ ] {
197
+ private createHints ( playcount : number , artistInfo : ArtistInfo ) : string [ ] {
198
+ const { type, country, musicGenre, gender, begin, end } = artistInfo ;
199
+
215
200
const hints : string [ ] = [ ] ;
216
201
const isGroup = type ? this . isGroupType ( type ) : false ;
217
202
@@ -222,8 +207,8 @@ export default class JumbleGameHandler {
222
207
? convertToDiscordTimestamp ( end , discordTimestampFormats . LONG_DATE )
223
208
: null ;
224
209
225
- const arttype = translations [ type ?? "group" ] ;
226
- const artgender = translations [ gender ?? "male" ] ;
210
+ const arttype = translations [ type ?? "group" ] ?? type ;
211
+ const artgender = translations [ gender ?? "male" ] ?? gender ;
227
212
const flag = `:flag_${ country ?. toLowerCase ( ) } :` ;
228
213
229
214
const conditionsAndHints : [ boolean , string ] [ ] = [
@@ -283,35 +268,17 @@ export default class JumbleGameHandler {
283
268
}
284
269
}
285
270
286
- private async addPoints ( userId : string , points : number ) : Promise < boolean > {
287
- try {
288
- const user = await container . db . user . findUnique ( {
289
- where : {
290
- discordId : userId ,
291
- } ,
292
- } ) ;
293
-
294
- if ( ! user ) return false ;
295
-
296
- await container . db . jumble . upsert ( {
297
- where : {
298
- userId : user . id ,
299
- } ,
300
- update : {
301
- points : {
302
- increment : points ,
303
- } ,
304
- } ,
305
- create : {
306
- userId : user . id ,
307
- points : points ,
271
+ private async addPoints ( userId : string , points : number ) {
272
+ await container . db . jumble . update ( {
273
+ where : {
274
+ userId : userId ,
275
+ } ,
276
+ data : {
277
+ points : {
278
+ increment : points ,
308
279
} ,
309
- } ) ;
310
-
311
- return true ;
312
- } catch ( error ) {
313
- return false ;
314
- }
280
+ } ,
281
+ } ) ;
315
282
}
316
283
317
284
private async setBestTime ( userId : string , time : number ) {
@@ -433,25 +400,32 @@ export default class JumbleGameHandler {
433
400
} ,
434
401
} ) ;
435
402
436
- if ( ! userData || ! userData . Jumble ) return ;
403
+ if ( ! userData || ! userData . Jumble ) {
404
+ const embed = new EmbedBuilder ( )
405
+ . setDescription (
406
+ "Use o comando /lastfm para registrar sua conta antes de jogar." ,
407
+ )
408
+ . setColor ( embedColors . default ) ;
437
409
438
- const added = await this . addPoints ( userData . id , 1 ) ;
439
- if ( added ) {
440
- const bestTime = userData . Jumble . bestTime ;
410
+ await message . channel . send ( { embeds : [ embed ] } ) ;
441
411
442
- if ( bestTime <= 0 || seconds < bestTime ) {
443
- await this . setBestTime ( userData . id , seconds ) ;
444
- }
412
+ return ;
413
+ }
414
+
415
+ await this . addPoints ( userData . id , 1 ) ;
416
+
417
+ const bestTime = userData . Jumble . bestTime ;
418
+
419
+ if ( bestTime <= 0 || seconds < bestTime ) {
420
+ await this . setBestTime ( userData . id , seconds ) ;
445
421
}
446
422
447
423
const embed = new EmbedBuilder ( )
448
424
. setDescription ( description )
449
425
. setFooter ( { text : `Respondido em ${ seconds } s` } )
450
426
. setColor ( embedColors . success ) ;
451
427
452
- message . channel . send ( {
453
- embeds : [ embed ] ,
454
- } ) ;
428
+ message . channel . send ( { embeds : [ embed ] } ) ;
455
429
}
456
430
457
431
private processInteraction (
@@ -510,6 +484,14 @@ export default class JumbleGameHandler {
510
484
if ( isWinner ) embed . spliceFields ( 1 , 1 ) ;
511
485
else embed . spliceFields ( 1 , 1 , newField ) ;
512
486
487
+ if ( ! isWinner && ! isGiveUp ) {
488
+ const failEmbed = new EmbedBuilder ( )
489
+ . setDescription ( `Ninguém adivinhou. A resposta era \`${ artistName } \`` )
490
+ . setColor ( embedColors . error ) ;
491
+
492
+ message . reply ( { embeds : [ failEmbed ] } ) ;
493
+ }
494
+
513
495
message . edit ( {
514
496
embeds : [ embed ] ,
515
497
components : [ ] ,
0 commit comments