Discord.js中Embed函数调用API时间歇性出现Unknown interaction错误求助
Discord.js 中使用 Embed 时间歇性出现
DiscordAPIError[10062]: Unknown interaction 错误的解决方法 问题描述
在使用 discord.js 的 Embed 功能时,间歇性触发以下错误:
DiscordAPIError[10062]: Unknown interaction
已精简代码,移除第三方 API 调用后功能完全正常,推测问题源于 API 响应速度过慢导致交互超时。简化后的触发代码如下:
client.on('interactionCreate', async (interaction) => { if (!interaction.isCommand()) return; console.log(interaction.commandName) const embed = new EmbedBuilder(); embed.setColor('#0099ff') embed.setURL('https://top.gg/bot/951349733211930654') embed.setAuthor({ name: 'WINGMAN | Apex Legends', iconURL: 'https://apex-resources.s3.eu-west-1.amazonaws.com/apex-logo.png', url: 'https://top.gg/bot/951349733211930654' }) id = interaction.id if (interaction.commandName === 'stats') { https.get(apilink, (resp) => { let data = ''; resp.on('data', (chunk) => { data += chunk; }); resp.on('end', () => { j_data = JSON.parse(data) console.log(j_data) embed.setTitle("Header") embed.setDescription("**Status: ** ") interaction.reply({ embeds: [embed] }); }); }).on("error", (err) => { console.log("Error: " + err.message); }); } }
解决建议
- 优先发送交互延迟确认,避免超时
Discord 要求所有交互必须在3秒内完成回复,当第三方 API 响应耗时超过3秒时,就会触发「Unknown interaction」错误。正确的做法是先调用interaction.deferReply()告知 Discord 正在处理请求,等数据返回后再编辑回复内容:
if (interaction.commandName === 'stats') { // 先发送延迟回复,维持交互有效性 await interaction.deferReply(); https.get(apilink, (resp) => { let data = ''; resp.on('data', (chunk) => { data += chunk; }); resp.on('end', async () => { const j_data = JSON.parse(data); console.log(j_data); embed.setTitle("Header") embed.setDescription("**Status: ** ") // 编辑延迟回复,展示最终内容 await interaction.editReply({ embeds: [embed] }); }); }).on("error", async (err) => { console.log("Error: " + err.message); // 出错时也要编辑回复告知用户 await interaction.editReply('获取数据失败,请稍后重试'); }); }
注意必须给异步操作加上 async/await,确保流程顺序正确。
- 改用 Promise 风格的 HTTP 请求库
原生https.get的回调写法不利于异步流程控制,建议使用node-fetch、axios这类支持 Promise 的库,配合async/await让代码更简洁易维护:
// 先安装依赖:npm install node-fetch const fetch = require('node-fetch'); client.on('interactionCreate', async (interaction) => { if (!interaction.isCommand()) return; console.log(interaction.commandName) const embed = new EmbedBuilder(); embed.setColor('#0099ff') embed.setURL('https://top.gg/bot/951349733211930654') embed.setAuthor({ name: 'WINGMAN | Apex Legends', iconURL: 'https://apex-resources.s3.eu-west-1.amazonaws.com/apex-logo.png', url: 'https://top.gg/bot/951349733211930654' }) if (interaction.commandName === 'stats') { await interaction.deferReply(); try { const response = await fetch(apilink); const j_data = await response.json(); console.log(j_data); embed.setTitle("Header") embed.setDescription("**Status: ** ") await interaction.editReply({ embeds: [embed] }); } catch (err) { console.log("Error: " + err.message); await interaction.editReply('获取数据失败,请稍后重试'); } } });
- 添加 API 请求超时限制
如果第三方 API 响应不稳定,可给请求设置超时时间,避免长时间等待导致后续问题:
// 先安装依赖:npm install axios const axios = require('axios'); // ...其他代码 try { // 设置10秒超时,超时直接触发错误捕获 const response = await axios.get(apilink, { timeout: 10000 }); const j_data = response.data; // ...后续编辑回复逻辑 } catch (err) { console.log("请求超时或出错: " + err.message); await interaction.editReply('请求超时,请稍后重试'); }
- 修复变量作用域问题
原代码中id = interaction.id和j_data = JSON.parse(data)未使用let/const声明,属于全局变量,高并发场景下会出现变量污染,建议修正为:
const id = interaction.id; // ... const j_data = JSON.parse(data);
内容的提问来源于stack exchange,提问作者C-Bertram
相关产品推荐
相关产品推荐

