Skip to content

Commit 1b98f16

Browse files
Update getting started sample with cv2 (discord#7564)
1 parent 1242cd6 commit 1b98f16

File tree

1 file changed

+71
-45
lines changed

1 file changed

+71
-45
lines changed

docs/quick-start/getting-started.mdx

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -301,14 +301,20 @@ With the endpoint verified, navigate to your project's `app.js` file and find th
301301
```javascript
302302
// "test" command
303303
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({
306306
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
307307
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+
]
310316
},
311-
});
317+
});
312318
}
313319
```
314320

@@ -345,39 +351,44 @@ To handle the `/challenge` command, add the following code after the `if name ==
345351
```javascript
346352
// "challenge" command
347353
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({
362368
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
363369
data: {
364-
content: `Rock papers scissors challenge from <@${userId}>`,
365-
components: [
370+
flags: InteractionResponseFlags.IS_COMPONENTS_V2,
371+
components: [
366372
{
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: [
369380
{
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,
375386
},
376-
],
387+
],
377388
},
378-
],
389+
],
379390
},
380-
});
391+
});
381392
}
382393
```
383394

@@ -424,11 +435,13 @@ if (type === InteractionType.MESSAGE_COMPONENT) {
424435
await res.send({
425436
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
426437
data: {
427-
// Fetches a random emoji to send from a helper function
428-
content: 'What is your object of choice?',
429438
// Indicates it'll be an ephemeral message
430-
flags: InteractionResponseFlags.EPHEMERAL,
439+
flags: InteractionResponseFlags.EPHEMERAL | InteractionResponseFlags.IS_COMPONENTS_V2,
431440
components: [
441+
{
442+
type: MessageComponentTypes.TEXT_DISPLAY,
443+
content: 'What is your object of choice?',
444+
},
432445
{
433446
type: MessageComponentTypes.ACTION_ROW,
434447
components: [
@@ -472,8 +485,8 @@ Modify the code above to handle the select menu:
472485

473486
```javascript
474487
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;
477490

478491
if (componentId.startsWith('accept_button_')) {
479492
// get the associated game ID
@@ -484,10 +497,13 @@ const componentId = data.custom_id;
484497
await res.send({
485498
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
486499
data: {
487-
content: 'What is your object of choice?',
488500
// Indicates it'll be an ephemeral message
489-
flags: InteractionResponseFlags.EPHEMERAL,
501+
flags: InteractionResponseFlags.EPHEMERAL | InteractionResponseFlags.IS_COMPONENTS_V2,
490502
components: [
503+
{
504+
type: MessageComponentTypes.TEXT_DISPLAY,
505+
content: 'What is your object of choice?',
506+
},
491507
{
492508
type: MessageComponentTypes.ACTION_ROW,
493509
components: [
@@ -517,10 +533,7 @@ const componentId = data.custom_id;
517533
// Get user ID and object choice for responding user
518534
// User ID is in user field for (G)DMs, and member for servers
519535
const userId = context === 0 ? req.body.member.user.id : req.body.user.id;
520-
521-
// User's object choice
522536
const objectName = data.values[0];
523-
524537
// Calculate result from helper function
525538
const resultStr = getResult(activeGames[gameId], {
526539
id: userId,
@@ -536,21 +549,34 @@ const componentId = data.custom_id;
536549
// Send results
537550
await res.send({
538551
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+
},
540561
});
541562
// Update ephemeral message
542563
await DiscordRequest(endpoint, {
543564
method: 'PATCH',
544565
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+
},
548573
});
549574
} catch (err) {
550575
console.error('Error sending message:', err);
551576
}
552577
}
553578
}
579+
554580
return;
555581
}
556582
```

0 commit comments

Comments
 (0)