Spaces:
Sleeping
Sleeping
Update src/router.js
Browse files- src/router.js +14 -7
src/router.js
CHANGED
|
@@ -2,7 +2,6 @@ import { Router } from 'express'
|
|
| 2 |
import { verifyKey } from 'discord-interactions'
|
| 3 |
import { generateWelcomeCard } from './canvas/welcomeCard.js'
|
| 4 |
import { generateLeaveCard } from './canvas/leaveCard.js'
|
| 5 |
-
|
| 6 |
import * as setupCmd from './commands/setup.js'
|
| 7 |
import * as welcomeMessageCmd from './commands/welcome-message.js'
|
| 8 |
import * as leaveMessageCmd from './commands/leave-message.js'
|
|
@@ -37,7 +36,6 @@ router.post('/generate/welcome', async (req, res) => {
|
|
| 37 |
if (req.headers['x-api-secret'] !== process.env.API_SECRET) {
|
| 38 |
return res.status(401).json({ error: 'unauthorized' })
|
| 39 |
}
|
| 40 |
-
|
| 41 |
try {
|
| 42 |
const buffer = await generateWelcomeCard(req.body)
|
| 43 |
res.set('Content-Type', 'image/png')
|
|
@@ -53,7 +51,6 @@ router.post('/generate/leave', async (req, res) => {
|
|
| 53 |
if (req.headers['x-api-secret'] !== process.env.API_SECRET) {
|
| 54 |
return res.status(401).json({ error: 'unauthorized' })
|
| 55 |
}
|
| 56 |
-
|
| 57 |
try {
|
| 58 |
const buffer = await generateLeaveCard(req.body)
|
| 59 |
res.set('Content-Type', 'image/png')
|
|
@@ -69,15 +66,26 @@ router.post('/interactions', async (req, res) => {
|
|
| 69 |
const signature = req.headers['x-signature-ed25519']
|
| 70 |
const timestamp = req.headers['x-signature-timestamp']
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
if (!isValid) {
|
| 74 |
-
|
|
|
|
| 75 |
}
|
| 76 |
|
|
|
|
| 77 |
const interaction = JSON.parse(req.body.toString())
|
| 78 |
|
| 79 |
// PING
|
| 80 |
if (interaction.type === 1) {
|
|
|
|
| 81 |
return res.json({ type: 1 })
|
| 82 |
}
|
| 83 |
|
|
@@ -97,7 +105,6 @@ router.post('/interactions', async (req, res) => {
|
|
| 97 |
await command.execute(interaction, res)
|
| 98 |
} catch (err) {
|
| 99 |
console.error(`[router] Command "${commandName}" threw:`, err.message)
|
| 100 |
-
// Only send error response if headers haven't been sent yet
|
| 101 |
if (!res.headersSent) {
|
| 102 |
res.json({
|
| 103 |
type: 4,
|
|
@@ -112,4 +119,4 @@ router.post('/interactions', async (req, res) => {
|
|
| 112 |
res.json({ type: 4, data: { content: 'Unsupported interaction type.', flags: 64 } })
|
| 113 |
})
|
| 114 |
|
| 115 |
-
export default router
|
|
|
|
| 2 |
import { verifyKey } from 'discord-interactions'
|
| 3 |
import { generateWelcomeCard } from './canvas/welcomeCard.js'
|
| 4 |
import { generateLeaveCard } from './canvas/leaveCard.js'
|
|
|
|
| 5 |
import * as setupCmd from './commands/setup.js'
|
| 6 |
import * as welcomeMessageCmd from './commands/welcome-message.js'
|
| 7 |
import * as leaveMessageCmd from './commands/leave-message.js'
|
|
|
|
| 36 |
if (req.headers['x-api-secret'] !== process.env.API_SECRET) {
|
| 37 |
return res.status(401).json({ error: 'unauthorized' })
|
| 38 |
}
|
|
|
|
| 39 |
try {
|
| 40 |
const buffer = await generateWelcomeCard(req.body)
|
| 41 |
res.set('Content-Type', 'image/png')
|
|
|
|
| 51 |
if (req.headers['x-api-secret'] !== process.env.API_SECRET) {
|
| 52 |
return res.status(401).json({ error: 'unauthorized' })
|
| 53 |
}
|
|
|
|
| 54 |
try {
|
| 55 |
const buffer = await generateLeaveCard(req.body)
|
| 56 |
res.set('Content-Type', 'image/png')
|
|
|
|
| 66 |
const signature = req.headers['x-signature-ed25519']
|
| 67 |
const timestamp = req.headers['x-signature-timestamp']
|
| 68 |
|
| 69 |
+
// Log before verification — req.body is a raw Buffer here
|
| 70 |
+
console.log('[interactions] Received interaction type:', req.body ? JSON.parse(req.body.toString())?.type : 'unknown')
|
| 71 |
+
|
| 72 |
+
const isValid = verifyKey(
|
| 73 |
+
req.body,
|
| 74 |
+
signature,
|
| 75 |
+
timestamp,
|
| 76 |
+
process.env.DISCORD_PUBLIC_KEY
|
| 77 |
+
)
|
| 78 |
if (!isValid) {
|
| 79 |
+
console.warn('[interactions] Invalid signature')
|
| 80 |
+
return res.status(401).send('Invalid signature')
|
| 81 |
}
|
| 82 |
|
| 83 |
+
// Safe to parse now that signature is verified
|
| 84 |
const interaction = JSON.parse(req.body.toString())
|
| 85 |
|
| 86 |
// PING
|
| 87 |
if (interaction.type === 1) {
|
| 88 |
+
console.log('[interactions] PING received, sending PONG')
|
| 89 |
return res.json({ type: 1 })
|
| 90 |
}
|
| 91 |
|
|
|
|
| 105 |
await command.execute(interaction, res)
|
| 106 |
} catch (err) {
|
| 107 |
console.error(`[router] Command "${commandName}" threw:`, err.message)
|
|
|
|
| 108 |
if (!res.headersSent) {
|
| 109 |
res.json({
|
| 110 |
type: 4,
|
|
|
|
| 119 |
res.json({ type: 4, data: { content: 'Unsupported interaction type.', flags: 64 } })
|
| 120 |
})
|
| 121 |
|
| 122 |
+
export default router
|