You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行Discord模态框代码触发DiscordAPIError[40060]错误求助

解决DiscordAPIError[40060]: Interaction has already been acknowledged错误

错误原因分析

该错误意味着你对同一个交互执行了多次确认操作(比如重复调用reply/deferReply/showModal等),或是代码存在语法异常导致交互处理逻辑混乱。结合你的代码,主要存在两处问题:

解决方案

1. 修复模态处理代码的语法错误

你的Modal Handler代码末尾缺失两个闭合大括号,导致代码结构不完整,运行时会触发异常。补充后的完整代码如下:

// Modal Handler
client.on(Events.InteractionCreate, async interaction => {

  if (!interaction.isModalSubmit()) return;

  if (interaction.customId === 'warnmodal') {
    const reason = interaction.fields.getTextInputValue('reason');

    const dmEmbed = new EmbedBuilder()
    .setColor("Blue")
    .setDescription(`<:success_checkmark:1092216874768547922> You have been *warned* in ***${interaction.guild.name}***. | ${reason}`)

    const embed = new EmbedBuilder()
    .setColor("Blue")
    .setDescription(`<:success_checkmark:1092216874768547922> ${interaction.user.tag} has been ***warned***. | ${reason}`)

    await interaction.reply({ embeds: [embed] });

    await interaction.user.send({embeds: [dmEmbed] }).catch(err => {
        return;
    })
  } // 闭合warnmodal判断代码块
}) // 闭合InteractionCreate事件监听代码块

2. 为上下文菜单中的showModal添加await

interaction.showModal()是异步方法,必须用await确保交互确认操作完成,避免后续出现重复确认问题。修改后的上下文菜单代码如下:

const { ContextMenuCommandBuilder, ApplicationCommandType, EmbedBuilder, ModalBuilder, ActionRowBuilder, TextInputBuilder, TextInputStyle } = require(`discord.js`);
const { QuickDB } = require(`quick.db`);
const db = new QuickDB();

module.exports = {
    data: new ContextMenuCommandBuilder()
    .setName('Warn User')
    .setType(ApplicationCommandType.User),
    async execute (interaction) {

        const modal = new ModalBuilder()
        .setTitle("Warn Menu")
        .setCustomId('warnmodal')

        const reason = new TextInputBuilder()
        .setCustomId('reason')
        .setRequired(true)
        .setLabel("Reason for the warn")
        .setStyle(TextInputStyle.Short);

        const firstActionRow = new ActionRowBuilder().addComponents(reason)

        modal.addComponents(firstActionRow)
        await interaction.showModal(modal) // 添加await关键字
    }
}

额外检查项

  • 确保代码中没有对同一个交互对象重复调用reply、deferReply等确认方法
  • 确认Discord.js版本为v14及以上(你的代码使用了EmbedBuilder、ModalBuilder等v14专属API,版本不匹配也可能引发异常)

内容的提问来源于stack exchange,提问作者tristymp4

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 08:45:03