feat: Improve error handling in promptKeySelection for interaction updates and timeouts

This commit is contained in:
2025-10-05 21:46:15 -05:00
parent cdcc339693
commit 294a641e54

View File

@@ -367,22 +367,29 @@ export async function promptKeySelection<T>(
return; return;
} }
await select.update({ try {
components: [ await select.update({
{ components: [
type: 17, {
accent_color: accentColor, type: 17,
components: [ accent_color: accentColor,
{ components: [
type: 10, {
content: `⏳ Cargando **${selected.option.label}**…`, type: 10,
}, content: `⏳ Cargando **${selected.option.label}**…`,
], },
}, ],
], },
}); ],
collector.stop('selected'); });
} catch {
if (!select.deferred && !select.replied) {
try { await select.deferUpdate(); } catch {}
}
}
finish(selected.entry, 'selected'); finish(selected.entry, 'selected');
collector.stop('selected');
return; return;
} }
@@ -406,19 +413,26 @@ export async function promptKeySelection<T>(
} }
if (interaction.customId === `${config.customIdPrefix}_cancel` && interaction.isButton()) { if (interaction.customId === `${config.customIdPrefix}_cancel` && interaction.isButton()) {
await interaction.update({ try {
components: [ await interaction.update({
{ components: [
type: 17, {
accent_color: 0xFF0000, type: 17,
components: [ accent_color: 0xFF0000,
{ type: 10, content: '❌ Selección cancelada.' }, components: [
], { type: 10, content: '❌ Selección cancelada.' },
}, ],
], },
}); ],
collector.stop('cancelled'); });
} catch {
if (!interaction.deferred && !interaction.replied) {
try { await interaction.deferUpdate(); } catch {}
}
}
finish(null, 'cancelled'); finish(null, 'cancelled');
collector.stop('cancelled');
return; return;
} }
@@ -474,17 +488,24 @@ export async function promptKeySelection<T>(
collector.on('end', async (_collected, reason) => { collector.on('end', async (_collected, reason) => {
if (resolved) return; if (resolved) return;
resolved = true; resolved = true;
const expiredPanel = { if (reason !== 'selected' && reason !== 'cancelled') {
type: 17, const expiredPanel = {
accent_color: 0xFFA500, type: 17,
components: [ accent_color: 0xFFA500,
{ type: 10, content: '⏰ Selección expirada.' }, components: [
], { type: 10, content: '⏰ Selección expirada.' },
}; ],
try { };
await panelMessage.edit({ components: [expiredPanel] }); try {
} catch {} await panelMessage.edit({ components: [expiredPanel] });
const mappedReason: 'selected' | 'cancelled' | 'timeout' = reason === 'cancelled' ? 'cancelled' : 'timeout'; } catch {}
}
let mappedReason: 'selected' | 'cancelled' | 'timeout';
if (reason === 'selected') mappedReason = 'selected';
else if (reason === 'cancelled') mappedReason = 'cancelled';
else mappedReason = 'timeout';
resolve({ entry: null, panelMessage, reason: mappedReason }); resolve({ entry: null, panelMessage, reason: mappedReason });
}); });
}); });