Archnemix commited on
Commit
5aeb8a2
·
verified ·
1 Parent(s): a0a51d9

Update src/router.js

Browse files
Files changed (1) hide show
  1. src/router.js +8 -16
src/router.js CHANGED
@@ -63,32 +63,27 @@ router.post('/generate/leave', async (req, res) => {
63
 
64
  // POST /interactions
65
  router.post('/interactions', async (req, res) => {
66
- console.log('[interactions] Incoming request, headers:', JSON.stringify(req.headers))
67
-
68
  const signature = req.headers['x-signature-ed25519']
69
  const timestamp = req.headers['x-signature-timestamp']
70
-
71
- console.log('[interactions] Verifying signature...')
72
  const isValid = verifyKey(req.body, signature, timestamp, process.env.DISCORD_PUBLIC_KEY)
73
  if (!isValid) {
74
  console.warn('[interactions] Invalid signature, rejecting request')
75
  return res.status(401).end('Invalid request signature')
76
  }
77
- console.log('[interactions] Signature valid')
78
 
 
79
  const interaction = JSON.parse(req.body.toString())
80
- console.log('[interactions] Received interaction type:', interaction.type)
81
 
82
- // PING
83
  if (interaction.type === 1) {
84
- console.log('[interactions] PING received, sending PONG')
85
- res.setHeader('Content-Type', 'application/json')
86
- console.log('[interactions] Content-Type header set to application/json')
87
- const pong = { type: 1 }
88
- console.log('[interactions] Sending PONG response:', JSON.stringify(pong))
89
- return res.status(200).json(pong)
90
  }
91
 
 
 
 
 
 
92
  // APPLICATION_COMMAND
93
  if (interaction.type === 2) {
94
  const commandName = interaction.data?.name
@@ -97,7 +92,6 @@ router.post('/interactions', async (req, res) => {
97
  const command = commands.get(commandName)
98
  if (!command) {
99
  console.warn(`[interactions] Unknown command: "${commandName}"`)
100
- res.setHeader('Content-Type', 'application/json')
101
  return res.json({
102
  type: 4,
103
  data: { content: 'Unknown command.', flags: 64 },
@@ -112,7 +106,6 @@ router.post('/interactions', async (req, res) => {
112
  console.error(`[router] Command "${commandName}" threw:`, err.message)
113
  if (!res.headersSent) {
114
  console.log(`[interactions] Sending error fallback response for "${commandName}"`)
115
- res.setHeader('Content-Type', 'application/json')
116
  res.json({
117
  type: 4,
118
  data: { content: 'An error occurred while running that command.', flags: 64 },
@@ -126,7 +119,6 @@ router.post('/interactions', async (req, res) => {
126
 
127
  // Unhandled interaction type
128
  console.warn('[interactions] Unhandled interaction type:', interaction.type)
129
- res.setHeader('Content-Type', 'application/json')
130
  res.json({ type: 4, data: { content: 'Unsupported interaction type.', flags: 64 } })
131
  })
132
 
 
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
  const isValid = verifyKey(req.body, signature, timestamp, process.env.DISCORD_PUBLIC_KEY)
69
  if (!isValid) {
70
  console.warn('[interactions] Invalid signature, rejecting request')
71
  return res.status(401).end('Invalid request signature')
72
  }
 
73
 
74
+ // Parse immediately so PING can be handled before anything else
75
  const interaction = JSON.parse(req.body.toString())
 
76
 
77
+ // PING — respond as fast as possible, no logging before this
78
  if (interaction.type === 1) {
79
+ return res.status(200).json({ type: 1 })
 
 
 
 
 
80
  }
81
 
82
+ // All logging happens after PING check so it never delays PONG
83
+ console.log('[interactions] Incoming request, headers:', JSON.stringify(req.headers))
84
+ console.log('[interactions] Signature valid')
85
+ console.log('[interactions] Received interaction type:', interaction.type)
86
+
87
  // APPLICATION_COMMAND
88
  if (interaction.type === 2) {
89
  const commandName = interaction.data?.name
 
92
  const command = commands.get(commandName)
93
  if (!command) {
94
  console.warn(`[interactions] Unknown command: "${commandName}"`)
 
95
  return res.json({
96
  type: 4,
97
  data: { content: 'Unknown command.', flags: 64 },
 
106
  console.error(`[router] Command "${commandName}" threw:`, err.message)
107
  if (!res.headersSent) {
108
  console.log(`[interactions] Sending error fallback response for "${commandName}"`)
 
109
  res.json({
110
  type: 4,
111
  data: { content: 'An error occurred while running that command.', flags: 64 },
 
119
 
120
  // Unhandled interaction type
121
  console.warn('[interactions] Unhandled interaction type:', interaction.type)
 
122
  res.json({ type: 4, data: { content: 'Unsupported interaction type.', flags: 64 } })
123
  })
124