Archnemix commited on
Commit
0a8169b
·
verified ·
1 Parent(s): d20acf4

Update src/router.js

Browse files
Files changed (1) hide show
  1. src/router.js +13 -13
src/router.js CHANGED
@@ -62,39 +62,39 @@ router.post('/generate/leave', async (req, res) => {
62
  }
63
  });
64
 
65
- // ==================== FIXED INTERACTIONS ENDPOINT (HF Spaces) ====================
66
  router.post('/interactions', async (req, res) => {
67
  const signature = req.headers['x-signature-ed25519'];
68
  const timestamp = req.headers['x-signature-timestamp'];
69
 
70
- // Quick reject if headers missing
71
  if (!signature || !timestamp) {
72
  return res.status(401).end('Missing headers');
73
  }
74
 
 
 
 
 
 
 
75
  const isValid = verifyKey(
76
- req.rawBody,
77
  signature,
78
  timestamp,
79
  process.env.DISCORD_PUBLIC_KEY
80
  );
81
 
82
  if (!isValid) {
83
- console.error('[interactions] ❌ Invalid signature (this is expected sometimes from Discord)');
84
  return res.status(401).end('Bad request signature');
85
  }
86
 
87
- // Parse interaction
88
- let interaction;
89
- try {
90
- interaction = JSON.parse(req.rawBody.toString('utf8'));
91
- } catch {
92
- return res.status(400).end('Bad JSON');
93
- }
94
 
95
  // === PING - Respond as fast as possible ===
96
  if (interaction.type === 1) {
97
- // IMPORTANT: Send clean response with proper Content-Type
98
  res.setHeader('Content-Type', 'application/json');
99
  return res.status(200).json({ type: 1 });
100
  }
@@ -132,4 +132,4 @@ router.post('/interactions', async (req, res) => {
132
  res.json({ type: 4, data: { content: 'Unsupported.', flags: 64 } });
133
  });
134
 
135
- export default router;
 
62
  }
63
  });
64
 
65
+ // ==================== FIXED INTERACTIONS ENDPOINT ====================
66
  router.post('/interactions', async (req, res) => {
67
  const signature = req.headers['x-signature-ed25519'];
68
  const timestamp = req.headers['x-signature-timestamp'];
69
 
70
+ // Quick reject if signature headers are completely missing
71
  if (!signature || !timestamp) {
72
  return res.status(401).end('Missing headers');
73
  }
74
 
75
+ // Ensure req.rawBody exists before converting to string
76
+ if (!req.rawBody) {
77
+ return res.status(400).end('Missing request body raw data');
78
+ }
79
+
80
+ // FIXED: verifyKey works best when given a UTF-8 string representation of the raw body
81
  const isValid = verifyKey(
82
+ req.rawBody.toString('utf8'),
83
  signature,
84
  timestamp,
85
  process.env.DISCORD_PUBLIC_KEY
86
  );
87
 
88
  if (!isValid) {
89
+ console.error('[interactions] ❌ Invalid signature received.');
90
  return res.status(401).end('Bad request signature');
91
  }
92
 
93
+ // FIXED: No manual try/catch JSON parsing needed now since express.json() populates req.body
94
+ const interaction = req.body;
 
 
 
 
 
95
 
96
  // === PING - Respond as fast as possible ===
97
  if (interaction.type === 1) {
 
98
  res.setHeader('Content-Type', 'application/json');
99
  return res.status(200).json({ type: 1 });
100
  }
 
132
  res.json({ type: 4, data: { content: 'Unsupported.', flags: 64 } });
133
  });
134
 
135
+ export default router;