Archnemix commited on
Commit
bc1774c
·
verified ·
1 Parent(s): aaa53cc

Update src/router.js

Browse files
Files changed (1) hide show
  1. src/router.js +20 -21
src/router.js CHANGED
@@ -62,67 +62,65 @@ router.post('/generate/leave', async (req, res) => {
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
  if (!signature || !timestamp) {
71
- console.warn('[interactions] Missing signature or timestamp');
72
- return res.status(401).end('Missing signature headers');
73
  }
74
 
75
- console.log('[interactions] Verifying signature...');
76
-
77
  const isValid = verifyKey(
78
- req.rawBody, // Must be Buffer
79
  signature,
80
  timestamp,
81
  process.env.DISCORD_PUBLIC_KEY
82
  );
83
 
84
  if (!isValid) {
85
- console.error('[interactions] ❌ Invalid signature');
86
  return res.status(401).end('Bad request signature');
87
  }
88
 
89
- console.log('[interactions] ✅ Signature valid');
90
-
91
  let interaction;
92
  try {
93
- interaction = JSON.parse(req.rawBody.toString());
94
- } catch (e) {
95
- console.error('[interactions] Failed to parse interaction JSON');
96
  return res.status(400).end('Bad JSON');
97
  }
98
 
99
- // PING
100
  if (interaction.type === 1) {
101
- console.log('[interactions] PING received, sending PONG');
 
102
  return res.status(200).json({ type: 1 });
103
  }
104
 
105
- // APPLICATION_COMMAND
106
  if (interaction.type === 2) {
107
  const commandName = interaction.data?.name;
108
- console.log(`[interactions] Command received: "${commandName}"`);
109
-
110
  const command = commands.get(commandName);
 
111
  if (!command) {
 
112
  return res.json({
113
  type: 4,
114
- data: { content: 'Unknown command.', flags: 64 },
115
  });
116
  }
117
 
118
  try {
119
  await command.execute(interaction, res);
120
  } catch (err) {
121
- console.error(`[router] Command "${commandName}" error:`, err.message);
122
  if (!res.headersSent) {
 
123
  res.json({
124
  type: 4,
125
- data: { content: 'An error occurred while running that command.', flags: 64 },
126
  });
127
  }
128
  }
@@ -130,7 +128,8 @@ router.post('/interactions', async (req, res) => {
130
  }
131
 
132
  // Fallback
133
- res.json({ type: 4, data: { content: 'Unsupported interaction type.', flags: 64 } });
 
134
  });
135
 
136
  export default router;
 
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
  }
101
 
102
+ // === Normal Commands ===
103
  if (interaction.type === 2) {
104
  const commandName = interaction.data?.name;
 
 
105
  const command = commands.get(commandName);
106
+
107
  if (!command) {
108
+ res.setHeader('Content-Type', 'application/json');
109
  return res.json({
110
  type: 4,
111
+ data: { content: 'Unknown command.', flags: 64 }
112
  });
113
  }
114
 
115
  try {
116
  await command.execute(interaction, res);
117
  } catch (err) {
118
+ console.error(`[router] Command error:`, err.message);
119
  if (!res.headersSent) {
120
+ res.setHeader('Content-Type', 'application/json');
121
  res.json({
122
  type: 4,
123
+ data: { content: 'An error occurred.', flags: 64 }
124
  });
125
  }
126
  }
 
128
  }
129
 
130
  // Fallback
131
+ res.setHeader('Content-Type', 'application/json');
132
+ res.json({ type: 4, data: { content: 'Unsupported.', flags: 64 } });
133
  });
134
 
135
  export default router;