From 3a2892f2e23c22d3a7a9fae322aa6a77693ff953 Mon Sep 17 00:00:00 2001 From: shay Date: Mon, 29 Jul 2024 11:38:57 -0400 Subject: [PATCH] Update app for user-installable apps (#52) * update code for user-install * readme --- README.md | 3 --- commands.js | 4 ++++ examples/app.js | 10 ++++++++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d327572..16ebc68 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,6 @@ This project contains a basic rock-paper-scissors-style Discord app written in J ![Demo of app](https://github.com/discord/discord-example-app/raw/main/assets/getting-started-demo.gif?raw=true) -> ✨ A version of this code is also hosted **[on Glitch 🎏](https://glitch.com/~getting-started-discord)** and **[on Replit 🌀](https://replit.com/github/discord/discord-example-app)** - ## Project structure Below is a basic overview of the project structure: @@ -91,7 +89,6 @@ You should see your connection open: Tunnel Status online Version 2.0/2.0 Web Interface http://127.0.0.1:4040 -Forwarding http://1234-someurl.ngrok.io -> localhost:3000 Forwarding https://1234-someurl.ngrok.io -> localhost:3000 Connections ttl opn rt1 rt5 p50 p90 diff --git a/commands.js b/commands.js index d282e56..4102e40 100644 --- a/commands.js +++ b/commands.js @@ -22,6 +22,8 @@ const TEST_COMMAND = { name: 'test', description: 'Basic command', type: 1, + integration_types: [0, 1], + contexts: [0, 1, 2], }; // Command containing options @@ -38,6 +40,8 @@ const CHALLENGE_COMMAND = { }, ], type: 1, + integration_types: [0, 1], + contexts: [0, 2], }; const ALL_COMMANDS = [TEST_COMMAND, CHALLENGE_COMMAND]; diff --git a/examples/app.js b/examples/app.js index 9b8ac13..8a84dd7 100644 --- a/examples/app.js +++ b/examples/app.js @@ -55,7 +55,10 @@ app.post('/interactions', verifyKeyMiddleware(process.env.PUBLIC_KEY), async fun // "challenge" command if (name === 'challenge' && id) { - const userId = req.body.member.user.id; + // Interaction context + const context = req.body.context; + // User ID is in user field for (G)DMs, and member for servers + const userId = context === 0 ? req.body.member.user.id : req.body.user.id; // User's object choice const objectName = req.body.data.options[0].value; @@ -137,8 +140,11 @@ app.post('/interactions', verifyKeyMiddleware(process.env.PUBLIC_KEY), async fun const gameId = componentId.replace('select_choice_', ''); if (activeGames[gameId]) { + // Interaction context + const context = req.body.context; // Get user ID and object choice for responding user - const userId = req.body.member.user.id; + // User ID is in user field for (G)DMs, and member for servers + const userId = context === 0 ? req.body.member.user.id : req.body.user.id; const objectName = data.values[0]; // Calculate result from helper function const resultStr = getResult(activeGames[gameId], {