Skip to content

Commit cefe50f

Browse files
committed
Fix async
1 parent e9fa2f4 commit cefe50f

File tree

1 file changed

+75
-22
lines changed

1 file changed

+75
-22
lines changed

src/index.ts

Lines changed: 75 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,18 +1831,46 @@ async function performListDomainsAsync(interaction: DiscordInteraction, env: Env
18311831
}
18321832

18331833
async function handleListDomains(interaction: DiscordInteraction, env: Env): Promise<DiscordInteractionResponse> {
1834-
// Send deferred response (shows "Bot is thinking...")
1835-
const deferredResponse = {
1834+
// Defer the response since this might take time with many domains
1835+
const deferResponse = {
18361836
type: 5, // DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE
18371837
data: {}
18381838
};
18391839

1840-
// Start the async listing process (fire and forget)
1841-
performListDomainsAsync(interaction, env).catch(error => {
1842-
console.error("Async list failed:", error);
1843-
});
1844-
1845-
return deferredResponse;
1840+
try {
1841+
// Do the actual work and send followup BEFORE returning
1842+
await performListDomainsAsync(interaction, env);
1843+
return deferResponse;
1844+
1845+
} catch (error) {
1846+
console.error("Error listing domains:", error);
1847+
1848+
// Try to send error as followup
1849+
const followupUrl = `https://discord.com/api/v10/webhooks/${interaction.application_id}/${interaction.token}`;
1850+
1851+
try {
1852+
await fetch(followupUrl, {
1853+
method: 'POST',
1854+
headers: {
1855+
'Content-Type': 'application/json'
1856+
},
1857+
body: JSON.stringify({
1858+
content: `❌ Failed to list domains: ${error instanceof Error ? error.message : String(error)}`
1859+
})
1860+
});
1861+
1862+
return deferResponse;
1863+
} catch (followupError) {
1864+
console.error("Failed to send followup error:", followupError);
1865+
return {
1866+
type: 4,
1867+
data: {
1868+
content: `❌ Failed to list domains: ${error instanceof Error ? error.message : String(error)}`,
1869+
flags: 64
1870+
}
1871+
};
1872+
}
1873+
}
18461874
}
18471875

18481876
async function handleDomainStatus(interaction: DiscordInteraction, env: Env): Promise<DiscordInteractionResponse> {
@@ -2135,22 +2163,47 @@ async function handleAddWithSubdomains(interaction: DiscordInteraction, env: Env
21352163
};
21362164
}
21372165

2138-
// Send immediate response so user knows command was received
2139-
const immediateResponse = {
2140-
type: 4,
2141-
data: {
2142-
content: `🔍 **Subdomain discovery started for \`${domain}\`**\n\n⏳ Scanning Certificate Transparency logs and DNS records...\n💡 Results will appear in a new message when complete (usually 3-8 seconds)`,
2143-
flags: 0 // Make it visible to everyone
2144-
}
2166+
// Defer the response since this might take longer than 3 seconds
2167+
const deferResponse = {
2168+
type: 5, // DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE
2169+
data: {}
21452170
};
21462171

2147-
// Start the async discovery process (fire and forget)
2148-
// Note: We don't await this to avoid Discord timeout
2149-
performSubdomainDiscoveryAsync(interaction, env, domain, verifyAll).catch(error => {
2150-
console.error("Async discovery failed:", error);
2151-
});
2152-
2153-
return immediateResponse;
2172+
try {
2173+
// Do the actual work and send followup BEFORE returning
2174+
await performSubdomainDiscoveryAsync(interaction, env, domain, verifyAll);
2175+
return deferResponse;
2176+
2177+
} catch (error) {
2178+
console.error("Error in subdomain discovery:", error);
2179+
2180+
// Try to send error as followup
2181+
const followupUrl = `https://discord.com/api/v10/webhooks/${interaction.application_id}/${interaction.token}`;
2182+
2183+
try {
2184+
await fetch(followupUrl, {
2185+
method: 'POST',
2186+
headers: {
2187+
'Content-Type': 'application/json'
2188+
},
2189+
body: JSON.stringify({
2190+
content: `❌ Failed to discover subdomains: ${error instanceof Error ? error.message : String(error)}`
2191+
})
2192+
});
2193+
2194+
return deferResponse;
2195+
} catch (followupError) {
2196+
// If we can't send followup, return error response directly
2197+
console.error("Failed to send followup error:", followupError);
2198+
return {
2199+
type: 4,
2200+
data: {
2201+
content: `❌ Failed to discover subdomains: ${error instanceof Error ? error.message : String(error)}`,
2202+
flags: 64
2203+
}
2204+
};
2205+
}
2206+
}
21542207
}
21552208

21562209
async function handleDampening(interaction: DiscordInteraction, env: Env): Promise<DiscordInteractionResponse> {

0 commit comments

Comments
 (0)