无法将文件附加到消息回复交互时嵌入



我一直在尝试回复discord.js中的一个带有缩略图的嵌入式消息的交互

这是我一直在使用的代码:

const Discord = require('discord.js');
const fs = require('fs');
const path = require('path');
const p = path.join(__dirname, '..', 'static', 'SDGs.json');
module.exports = (msg, int, text, sDGNo) => {
fs.readFile(p, (err, data) => {
if (err) {
console.log(err.message);
}
const db = JSON.parse(data);
if (Number(sDGNo) === 0) {
const imgFilePath = path.join(__dirname, '..', 'static', 'SDGIcons', 'SDG_Wheel_Transparent_WEB.png');
const imgAttachment = new Discord.MessageAttachment(imgFilePath, 'SDG_Wheel_Transparent_WEB.png');
const goals = [];
for (let i = 1; i < 18; i++) {
const sdg = db[i];
goals.push({ name: `${i} ${sdg.shortForm}`, value: `${sdg.shortFormJap}n${sdg.shortFormNep}`, inline: true });
}
const embed = new Discord.MessageEmbed()
.setTitle('Sustainable Development Goals')
.setColor('#ffffff')
.setThumbnail('attachment://SDG_Wheel_Transparent_WEB.png')
.addFields(goals)
.setFooter('To find out more about each goal, simply type "!sdg" and the goal number (e.g., "!sdg7").');
msg ? msg.channel.send({ embeds: [embed], files: [imgAttachment] }) : int ? int.reply({ embeds: [embed], files: [imgAttachment] }).catch(console.error) : null;
return;
}
})
}

该函数在switch语句中的interactionCreate事件的处理程序中执行:

const { nE, tJ, say, whatis, spellcheck, joke, syn, ant, tN, sdg } = require('../helpers/commands');
module.exports = (client, interaction) => {
switch (interaction.commandName) {
/* ... */
case 'sdg':
sdg(null, interaction, null, interaction.options.data[0] ? interaction.options.data[0].value : '0');
break;
}
};

当它被用来发送由messagecrecreate事件生成的消息的嵌入式消息时,它工作得很好,但是与由interactionCreate事件创建的交互,除非我在回复选项中排除文件属性(在这种情况下,它发布嵌入消息而没有缩略图),我得到以下错误:

DiscordAPIError: Unknown interaction
at RequestHandler.execute (D:DiscordTranslationBotnode_modulesdiscord.jssrcrestRequestHandler.js:298:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (D:DiscordTranslationBotnode_modulesdiscord.jssrcrestRequestHandler.js:50:14)
at async CommandInteraction.reply (D:DiscordTranslationBotnode_modulesdiscord.jssrcstructuresinterfacesInteractionResponses.js:97:5) {
method: 'post',
path:'/interactions/895529028163878952/aW50ZXJhY3Rpb246ODk1NTI5MDI4MTYzODc4OTUyOlJ5YXRuSXZDekk3MFdyUzg3Z2RNZTB3U0ZpaVl4dlJkZjBySHRaYk05RDNwRE5pM1dTUG52eTR2aG9TUGl1QkVMOEt4aEhzRjNTTXdDS3dWZjNOaWtrMFZVQUlHWWkxOEJOUkY3eExFWk9xM3htWlFad2UzcWU5cnJnODVwZzc2/callback',
code: 10062,
httpStatus: 404,
requestData: { json: { type: 4, data: [Object] }, files: [ [Object] ] }
}

任何帮助都将非常感激!

看来你回复互动太晚了。
冷却时间为3秒,如果您在交互创建3秒后没有回复,您将得到此错误。
省略files属性不会造成问题。
尝试先延迟回复,然后再编辑回复。

int.deferReply();
// ...
int.editReply({ embeds: [embed] });

最新更新