Archnemix commited on
Commit
ee8c3ae
·
verified ·
1 Parent(s): bae62ce

Update src/router.js

Browse files
Files changed (1) hide show
  1. src/router.js +37 -34
src/router.js CHANGED
@@ -61,86 +61,89 @@ router.post('/generate/leave', async (req, res) => {
61
  }
62
  })
63
 
64
- // POST /interactions
65
  // POST /interactions
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
  // Basic headers check
71
  if (!signature || !timestamp) {
72
- console.warn('[interactions] Missing signature or timestamp headers');
73
- return res.status(401).end('Missing signature headers');
74
  }
75
 
76
- console.log('[interactions] Verifying signature...');
77
 
78
  const isValid = verifyKey(
79
- req.rawBody, // Must be Buffer or raw string
80
  signature,
81
  timestamp,
82
  process.env.DISCORD_PUBLIC_KEY
83
- );
84
 
85
  if (!isValid) {
86
- console.error('[interactions] ❌ Invalid signature - Verification failed');
87
- console.error('[interactions] Signature:', signature?.substring(0, 20) + '...');
88
- console.error('[interactions] Timestamp:', timestamp);
89
- return res.status(401).end('Bad request signature');
90
  }
91
 
92
- console.log('[interactions] ✅ Signature valid');
93
 
94
- let interaction;
95
  try {
96
- interaction = JSON.parse(req.rawBody);
97
  } catch (err) {
98
- console.error('[interactions] Failed to parse interaction JSON');
99
- return res.status(400).json({ error: 'Invalid JSON' });
100
  }
101
 
102
- // PING — respond as fast as possible
103
  if (interaction.type === 1) {
104
- console.log('[interactions] PING received, sending PONG');
105
- return res.status(200).json({ type: 1 });
 
 
 
 
106
  }
107
 
108
  // APPLICATION_COMMAND
109
  if (interaction.type === 2) {
110
- const commandName = interaction.data?.name;
111
- console.log(`[interactions] APPLICATION_COMMAND received: "${commandName}"`);
112
 
113
- const command = commands.get(commandName);
114
  if (!command) {
115
- console.warn(`[interactions] Unknown command: "${commandName}"`);
116
  return res.json({
117
  type: 4,
118
  data: { content: 'Unknown command.', flags: 64 },
119
- });
120
  }
121
 
122
- console.log(`[interactions] Executing command: "${commandName}"`);
123
  try {
124
- await command.execute(interaction, res);
125
- console.log(`[interactions] Command "${commandName}" completed`);
126
  } catch (err) {
127
- console.error(`[interactions] Command "${commandName}" threw:`, err.message);
128
  if (!res.headersSent) {
129
  res.json({
130
  type: 4,
131
  data: { content: 'An error occurred while running that command.', flags: 64 },
132
- });
133
  }
134
  }
135
- return;
136
  }
137
 
138
  // Unhandled interaction type
139
- console.warn('[interactions] Unhandled interaction type:', interaction.type);
140
  res.json({
141
  type: 4,
142
  data: { content: 'Unsupported interaction type.', flags: 64 }
143
- });
144
- });
145
 
146
  export default router
 
61
  }
62
  })
63
 
 
64
  // POST /interactions
65
  router.post('/interactions', async (req, res) => {
66
+ const signature = req.headers['x-signature-ed25519']
67
+ const timestamp = req.headers['x-signature-timestamp']
68
 
69
  // Basic headers check
70
  if (!signature || !timestamp) {
71
+ console.warn('[interactions] Missing signature or timestamp headers')
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,
79
  signature,
80
  timestamp,
81
  process.env.DISCORD_PUBLIC_KEY
82
+ )
83
 
84
  if (!isValid) {
85
+ console.error('[interactions] ❌ Invalid signature - Verification failed')
86
+ console.error('[interactions] Signature:', signature?.substring(0, 20) + '...')
87
+ console.error('[interactions] Timestamp:', timestamp)
88
+ return res.status(401).end('Bad request signature')
89
  }
90
 
91
+ console.log('[interactions] ✅ Signature valid')
92
 
93
+ let interaction
94
  try {
95
+ interaction = JSON.parse(req.rawBody)
96
  } catch (err) {
97
+ console.error('[interactions] Failed to parse interaction JSON')
98
+ return res.status(400).json({ error: 'Invalid JSON' })
99
  }
100
 
101
+ // PING — Important fix for Hugging Face
102
  if (interaction.type === 1) {
103
+ console.log('[interactions] PING received, sending PONG')
104
+
105
+ // Small delay helps with HF Spaces + Discord validation
106
+ await new Promise(resolve => setTimeout(resolve, 250))
107
+
108
+ return res.status(200).json({ type: 1 })
109
  }
110
 
111
  // APPLICATION_COMMAND
112
  if (interaction.type === 2) {
113
+ const commandName = interaction.data?.name
114
+ console.log(`[interactions] APPLICATION_COMMAND received: "${commandName}"`)
115
 
116
+ const command = commands.get(commandName)
117
  if (!command) {
118
+ console.warn(`[interactions] Unknown command: "${commandName}"`)
119
  return res.json({
120
  type: 4,
121
  data: { content: 'Unknown command.', flags: 64 },
122
+ })
123
  }
124
 
125
+ console.log(`[interactions] Executing command: "${commandName}"`)
126
  try {
127
+ await command.execute(interaction, res)
128
+ console.log(`[interactions] Command "${commandName}" completed`)
129
  } catch (err) {
130
+ console.error(`[interactions] Command "${commandName}" threw:`, err.message)
131
  if (!res.headersSent) {
132
  res.json({
133
  type: 4,
134
  data: { content: 'An error occurred while running that command.', flags: 64 },
135
+ })
136
  }
137
  }
138
+ return
139
  }
140
 
141
  // Unhandled interaction type
142
+ console.warn('[interactions] Unhandled interaction type:', interaction.type)
143
  res.json({
144
  type: 4,
145
  data: { content: 'Unsupported interaction type.', flags: 64 }
146
+ })
147
+ })
148
 
149
  export default router