@@ -301,14 +301,20 @@ With the endpoint verified, navigate to your project's `app.js` file and find th
301
301
``` javascript
302
302
// "test" command
303
303
if (name === ' test' ) {
304
- // Send a message into the channel where command was triggered from
305
- return res .send ({
304
+ // Send a message into the channel where command was triggered from
305
+ return res .send ({
306
306
type: InteractionResponseType .CHANNEL_MESSAGE_WITH_SOURCE ,
307
307
data: {
308
- // Fetches a random emoji to send from a helper function
309
- content: ' hello world ' + getRandomEmoji (),
308
+ flags: InteractionResponseFlags .IS_COMPONENTS_V2 ,
309
+ components: [
310
+ {
311
+ type: MessageComponentTypes .TEXT_DISPLAY ,
312
+ // Fetches a random emoji to send from a helper function
313
+ content: ` hello world ${ getRandomEmoji ()} `
314
+ }
315
+ ]
310
316
},
311
- });
317
+ });
312
318
}
313
319
```
314
320
@@ -345,39 +351,44 @@ To handle the `/challenge` command, add the following code after the `if name ==
345
351
``` javascript
346
352
// "challenge" command
347
353
if (name === ' challenge' && id) {
348
- // Interaction context
349
- const context = req .body .context ;
350
- // User ID is in user field for (G)DMs, and member for servers
351
- const userId = context === 0 ? req .body .member .user .id : req .body .user .id ;
352
- // User's object choice
353
- const objectName = req .body .data .options [0 ].value ;
354
-
355
- // Create active game using message ID as the game ID
356
- activeGames[id] = {
357
- id: userId,
358
- objectName,
359
- };
360
-
361
- return res .send ({
354
+ // Interaction context
355
+ const context = req .body .context ;
356
+ // User ID is in user field for (G)DMs, and member for servers
357
+ const userId = context === 0 ? req .body .member .user .id : req .body .user .id ;
358
+ // User's object choice
359
+ const objectName = req .body .data .options [0 ].value ;
360
+
361
+ // Create active game using message ID as the game ID
362
+ activeGames[id] = {
363
+ id: userId,
364
+ objectName,
365
+ };
366
+
367
+ return res .send ({
362
368
type: InteractionResponseType .CHANNEL_MESSAGE_WITH_SOURCE ,
363
369
data: {
364
- content : ` Rock papers scissors challenge from <@ ${ userId } > ` ,
365
- components: [
370
+ flags : InteractionResponseFlags . IS_COMPONENTS_V2 ,
371
+ components: [
366
372
{
367
- type: MessageComponentTypes .ACTION_ROW ,
368
- components: [
373
+ type: MessageComponentTypes .TEXT_DISPLAY ,
374
+ // Fetches a random emoji to send from a helper function
375
+ content: ` Rock papers scissors challenge from <@${ userId} >` ,
376
+ },
377
+ {
378
+ type: MessageComponentTypes .ACTION_ROW ,
379
+ components: [
369
380
{
370
- type: MessageComponentTypes .BUTTON ,
371
- // Append the game ID to use later on
372
- custom_id: ` accept_button_${ req .body .id } ` ,
373
- label: ' Accept' ,
374
- style: ButtonStyleTypes .PRIMARY ,
381
+ type: MessageComponentTypes .BUTTON ,
382
+ // Append the game ID to use later on
383
+ custom_id: ` accept_button_${ req .body .id } ` ,
384
+ label: ' Accept' ,
385
+ style: ButtonStyleTypes .PRIMARY ,
375
386
},
376
- ],
387
+ ],
377
388
},
378
- ],
389
+ ],
379
390
},
380
- });
391
+ });
381
392
}
382
393
```
383
394
@@ -424,11 +435,13 @@ if (type === InteractionType.MESSAGE_COMPONENT) {
424
435
await res .send ({
425
436
type: InteractionResponseType .CHANNEL_MESSAGE_WITH_SOURCE ,
426
437
data: {
427
- // Fetches a random emoji to send from a helper function
428
- content: ' What is your object of choice?' ,
429
438
// Indicates it'll be an ephemeral message
430
- flags: InteractionResponseFlags .EPHEMERAL ,
439
+ flags: InteractionResponseFlags .EPHEMERAL | InteractionResponseFlags . IS_COMPONENTS_V2 ,
431
440
components: [
441
+ {
442
+ type: MessageComponentTypes .TEXT_DISPLAY ,
443
+ content: ' What is your object of choice?' ,
444
+ },
432
445
{
433
446
type: MessageComponentTypes .ACTION_ROW ,
434
447
components: [
@@ -472,8 +485,8 @@ Modify the code above to handle the select menu:
472
485
473
486
``` javascript
474
487
if (type === InteractionType .MESSAGE_COMPONENT ) {
475
- // custom_id set in payload when sending message component
476
- const componentId = data .custom_id ;
488
+ // custom_id set in payload when sending message component
489
+ const componentId = data .custom_id ;
477
490
478
491
if (componentId .startsWith (' accept_button_' )) {
479
492
// get the associated game ID
@@ -484,10 +497,13 @@ const componentId = data.custom_id;
484
497
await res .send ({
485
498
type: InteractionResponseType .CHANNEL_MESSAGE_WITH_SOURCE ,
486
499
data: {
487
- content: ' What is your object of choice?' ,
488
500
// Indicates it'll be an ephemeral message
489
- flags: InteractionResponseFlags .EPHEMERAL ,
501
+ flags: InteractionResponseFlags .EPHEMERAL | InteractionResponseFlags . IS_COMPONENTS_V2 ,
490
502
components: [
503
+ {
504
+ type: MessageComponentTypes .TEXT_DISPLAY ,
505
+ content: ' What is your object of choice?' ,
506
+ },
491
507
{
492
508
type: MessageComponentTypes .ACTION_ROW ,
493
509
components: [
@@ -517,10 +533,7 @@ const componentId = data.custom_id;
517
533
// Get user ID and object choice for responding user
518
534
// User ID is in user field for (G)DMs, and member for servers
519
535
const userId = context === 0 ? req .body .member .user .id : req .body .user .id ;
520
-
521
- // User's object choice
522
536
const objectName = data .values [0 ];
523
-
524
537
// Calculate result from helper function
525
538
const resultStr = getResult (activeGames[gameId], {
526
539
id: userId,
@@ -536,21 +549,34 @@ const componentId = data.custom_id;
536
549
// Send results
537
550
await res .send ({
538
551
type: InteractionResponseType .CHANNEL_MESSAGE_WITH_SOURCE ,
539
- data: { content: resultStr },
552
+ data: {
553
+ flags: InteractionResponseFlags .IS_COMPONENTS_V2 ,
554
+ components: [
555
+ {
556
+ type: MessageComponentTypes .TEXT_DISPLAY ,
557
+ content: resultStr
558
+ }
559
+ ]
560
+ },
540
561
});
541
562
// Update ephemeral message
542
563
await DiscordRequest (endpoint, {
543
564
method: ' PATCH' ,
544
565
body: {
545
- content: ' Nice choice ' + getRandomEmoji (),
546
- components: []
547
- }
566
+ components: [
567
+ {
568
+ type: MessageComponentTypes .TEXT_DISPLAY ,
569
+ content: ' Nice choice ' + getRandomEmoji ()
570
+ }
571
+ ],
572
+ },
548
573
});
549
574
} catch (err) {
550
575
console .error (' Error sending message:' , err);
551
576
}
552
577
}
553
578
}
579
+
554
580
return ;
555
581
}
556
582
```
0 commit comments