@@ -98,7 +98,8 @@ router.post('/send', async (request, env) => {
9898 }
9999} ) ;
100100
101- router . post ( '/' , async ( request , env ) => {
101+
102+ router . post ( '/' , async ( request , env , ctx ) => {
102103 const { isValid, interaction } = await verifyDiscordRequest ( request , env ) ;
103104 if ( ! isValid || ! interaction ) {
104105 return new Response ( 'Bad request signature.' , { status : 401 } ) ;
@@ -110,7 +111,7 @@ router.post('/', async (request, env) => {
110111 } ) ;
111112 }
112113
113- if ( interaction . type === InteractionType . MESSAGE_COMPONENT ) {
114+ if ( interaction . type === InteractionType . MESSAGE_COMPONENT ) {
114115 let toSend = '' ;
115116 let components = undefined ;
116117
@@ -270,16 +271,14 @@ router.post('/', async (request, env) => {
270271 } ) ;
271272 }
272273
273- // Send a deferred response immediately to avoid timeout.
274+ // Immediately send a deferred response
274275 const deferredResponse = new JsonResponse ( {
275- type : InteractionResponseType . DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE ,
276- data : { content : "Adding..." }
276+ type : InteractionResponseType . DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE
277277 } ) ;
278-
279- // Process the role assignment asynchronously.
280- // If your environment supports it (e.g. Cloudflare Workers), consider using event.waitUntil()
281- processRoleAssignment ( interaction , env , roleId , users , issuerId , guildId ) ;
282-
278+
279+ // Use ctx.waitUntil to ensure background processing continues
280+ ctx . waitUntil ( processRoleAssignment ( interaction , env , roleId , users , issuerId , guildId ) ) ;
281+
283282 return deferredResponse ;
284283 }
285284 }
@@ -422,14 +421,19 @@ async function processRoleAssignment(
422421}
423422
424423async function updateOriginalMessage ( interaction : any , env : any , content : string ) {
425- await fetch (
426- `https://discord.com/api/v9/webhooks/${ env . DISCORD_APPLICATION_ID } /${ interaction . token } /messages/@original` ,
427- {
428- method : 'PATCH' ,
429- headers : { 'Content-Type' : 'application/json' } ,
430- body : JSON . stringify ( { content } )
431- }
432- ) ;
424+ try {
425+ const response = await fetch (
426+ `https://discord.com/api/v9/webhooks/${ env . DISCORD_APPLICATION_ID } /${ interaction . token } /messages/@original` ,
427+ {
428+ method : 'PATCH' ,
429+ headers : { 'Content-Type' : 'application/json' } ,
430+ body : JSON . stringify ( { content } )
431+ }
432+ ) ;
433+ console . log ( 'Update original message status:' , response . status ) ;
434+ } catch ( err ) {
435+ console . error ( 'Error updating original message:' , err ) ;
436+ }
433437}
434438
435439const server = {
0 commit comments