Upgrade discord-interactions and use verifyKeyMiddleware (#57)

This commit is contained in:
Justin Beckwith
2024-07-26 13:48:46 -04:00
committed by GitHub
parent 0e233d10a7
commit 62f3c90c6d
9 changed files with 35 additions and 47 deletions
+12 -13
View File
@@ -3,29 +3,22 @@ import express from 'express';
import {
InteractionType,
InteractionResponseType,
InteractionResponseFlags,
MessageComponentTypes,
ButtonStyleTypes,
verifyKeyMiddleware,
} from 'discord-interactions';
import { VerifyDiscordRequest, getRandomEmoji, DiscordRequest } from './utils.js';
import { getShuffledOptions, getResult } from './game.js';
import { getRandomEmoji } from './utils.js';
// Create an express app
const app = express();
// Get port, or default to 3000
const PORT = process.env.PORT || 3000;
// Parse request body and verifies incoming requests using discord-interactions package
app.use(express.json({ verify: VerifyDiscordRequest(process.env.PUBLIC_KEY) }));
// Store for in-progress games. In production, you'd want to use a DB
const activeGames = {};
/**
* Interactions endpoint URL where Discord will send HTTP requests
* Parse request body and verifies incoming requests using discord-interactions package
*/
app.post('/interactions', async function (req, res) {
app.post('/interactions', verifyKeyMiddleware(process.env.PUBLIC_KEY), async function (req, res) {
// Interaction type and data
const { type, id, data } = req.body;
const { type, data } = req.body;
/**
* Handle verification requests
@@ -48,11 +41,17 @@ app.post('/interactions', async function (req, res) {
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
// Fetches a random emoji to send from a helper function
content: 'hello world ' + getRandomEmoji(),
content: `hello world ${getRandomEmoji()}`,
},
});
}
console.error(`unknown command: ${name}`);
return res.status(400).json({ error: 'unknown command' });
}
console.error('unknown interaction type', type);
return res.status(400).json({ error: 'unknown interaction type' });
});
app.listen(PORT, () => {
+1 -1
View File
@@ -42,4 +42,4 @@ const CHALLENGE_COMMAND = {
const ALL_COMMANDS = [TEST_COMMAND, CHALLENGE_COMMAND];
InstallGlobalCommands(process.env.APP_ID, ALL_COMMANDS);
InstallGlobalCommands(process.env.APP_ID, ALL_COMMANDS);
+12 -5
View File
@@ -6,24 +6,24 @@ import {
InteractionResponseFlags,
MessageComponentTypes,
ButtonStyleTypes,
verifyKeyMiddleware,
} from 'discord-interactions';
import { VerifyDiscordRequest, getRandomEmoji, DiscordRequest } from './utils.js';
import { getRandomEmoji, DiscordRequest } from './utils.js';
import { getShuffledOptions, getResult } from './game.js';
// Create an express app
const app = express();
// Get port, or default to 3000
const PORT = process.env.PORT || 3000;
// Parse request body and verifies incoming requests using discord-interactions package
app.use(express.json({ verify: VerifyDiscordRequest(process.env.PUBLIC_KEY) }));
// Store for in-progress games. In production, you'd want to use a DB
const activeGames = {};
/**
* Interactions endpoint URL where Discord will send HTTP requests
* Parse request body and verifies incoming requests using discord-interactions package
*/
app.post('/interactions', async function (req, res) {
app.post('/interactions', verifyKeyMiddleware(process.env.PUBLIC_KEY), async function (req, res) {
// Interaction type and data
const { type, id, data } = req.body;
@@ -48,10 +48,11 @@ app.post('/interactions', async function (req, res) {
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
// Fetches a random emoji to send from a helper function
content: 'hello world ' + getRandomEmoji(),
content: `hello world ${getRandomEmoji()}`,
},
});
}
// "challenge" command
if (name === 'challenge' && id) {
const userId = req.body.member.user.id;
@@ -86,6 +87,9 @@ app.post('/interactions', async function (req, res) {
},
});
}
console.error(`unknown command: ${name}`);
return res.status(400).json({ error: 'unknown command' });
}
/**
@@ -167,6 +171,9 @@ app.post('/interactions', async function (req, res) {
}
}
}
console.error('unknown interaction type', type);
return res.status(400).json({ error: 'unknown interaction type' });
});
app.listen(PORT, () => {
+2 -3
View File
@@ -5,14 +5,13 @@ import {
InteractionResponseType,
MessageComponentTypes,
ButtonStyleTypes,
verifyKeyMiddleware,
} from 'discord-interactions';
import { VerifyDiscordRequest } from '../utils.js';
// Create and configure express app
const app = express();
app.use(express.json({ verify: VerifyDiscordRequest(process.env.PUBLIC_KEY) }));
app.post('/interactions', function (req, res) {
app.post('/interactions', verifyKeyMiddleware(process.env.PUBLIC_KEY), function (req, res) {
// Interaction type and data
const { type, data } = req.body;
/**
+3 -4
View File
@@ -1,13 +1,12 @@
import 'dotenv/config';
import express from 'express';
import { InteractionType, InteractionResponseType } from 'discord-interactions';
import { VerifyDiscordRequest, DiscordRequest } from '../utils.js';
import { InteractionType, InteractionResponseType, verifyKeyMiddleware } from 'discord-interactions';
import { DiscordRequest } from '../utils.js';
// Create and configure express app
const app = express();
app.use(express.json({ verify: VerifyDiscordRequest(process.env.PUBLIC_KEY) }));
app.post('/interactions', function (req, res) {
app.post('/interactions', verifyKeyMiddleware(process.env.PUBLIC_KEY), function (req, res) {
// Interaction type and data
const { type, data } = req.body;
/**
+2 -3
View File
@@ -4,14 +4,13 @@ import {
InteractionType,
InteractionResponseType,
MessageComponentTypes,
verifyKeyMiddleware,
} from 'discord-interactions';
import { VerifyDiscordRequest } from '../utils.js';
// Create and configure express app
const app = express();
app.use(express.json({ verify: VerifyDiscordRequest(process.env.PUBLIC_KEY) }));
app.post('/interactions', function (req, res) {
app.post('/interactions', verifyKeyMiddleware(process.env.PUBLIC_KEY), function (req, res) {
// Interaction type and data
const { type, data } = req.body;
/**
+2 -3
View File
@@ -4,14 +4,13 @@ import {
InteractionType,
InteractionResponseType,
MessageComponentTypes,
verifyKeyMiddleware,
} from 'discord-interactions';
import { VerifyDiscordRequest } from '../utils.js';
// Create and configure express app
const app = express();
app.use(express.json({ verify: VerifyDiscordRequest(process.env.PUBLIC_KEY) }));
app.post('/interactions', function (req, res) {
app.post('/interactions', verifyKeyMiddleware(process.env.PUBLIC_KEY), function (req, res) {
// Interaction type and data
const { type, data } = req.body;
/**
+1 -1
View File
@@ -16,7 +16,7 @@
"author": "Shay DeWael",
"license": "MIT",
"dependencies": {
"discord-interactions": "^3.2.0",
"discord-interactions": "^4.0.0",
"dotenv": "^16.0.3",
"express": "^4.18.2"
},
-14
View File
@@ -1,18 +1,4 @@
import 'dotenv/config';
import { verifyKey } from 'discord-interactions';
export function VerifyDiscordRequest(clientKey) {
return function (req, res, buf, encoding) {
const signature = req.get('X-Signature-Ed25519');
const timestamp = req.get('X-Signature-Timestamp');
const isValidRequest = verifyKey(buf, signature, timestamp, clientKey);
if (!isValidRequest) {
res.status(401).send('Bad request signature');
throw new Error('Bad request signature');
}
};
}
export async function DiscordRequest(endpoint, options) {
// append endpoint to root API URL