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

Discord.js V13斜杠命令执行提示interaction failed报错问题求助

问题根因
  • 事件名拼写错误:代码中监听的事件为interactCreate,Discord.js v13 对应交互触发的正确事件名为interactionCreate,拼写错误导致交互触发后没有对应的处理逻辑返回响应,就会抛出interaction failed报错。
  • 命令名不匹配:代码中注册的斜杠命令名为pig,你实际执行的是/ping,调用未注册的命令也会触发执行失败。
  • 冗余无效代码:单独一行的commands?.create属于无效冗余代码,虽不会直接报错,但建议清理。
修复方案
  1. 修正事件监听名称,将interactCreate替换为interactionCreate
  2. 对齐命令名称:如果需要实现/ping命令,将注册命令时的name字段改为ping,同时判断逻辑里的commandName也改为ping;如果要保留/pig命令,直接执行/pig测试即可
  3. 删除冗余的commands?.create行

修复后的完整代码如下:

import DiscordJS, { Intents } from 'discord.js'
import dotenv from 'dotenv'
dotenv.config()

const client = new DiscordJS.Client({
    intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MESSAGES
    ]
})

client.on('ready', () => {
    console.log('The bot is ready')

    const guildID = '906389171806040114'
    const guild = client.guilds.cache.get(guildID)
    let commands

    if (guild) {
        commands = guild.commands
    } else {
        commands = client.application?.commands
    }

    // 注册/ping命令
    commands?.create({
        name: 'ping',
        description: 'Replies with pong',
    })
})

client.on('interactionCreate', async (interaction) => {
    if (!interaction.isCommand()) {
        return
    }

    const { commandName } = interaction

    if (commandName === 'ping') {
        interaction.reply({
            content: 'pong',
            ephemeral: true,
        })
    }
})

client.login(process.env.TOKEN)

注意:修改命令注册配置后,服务器级命令会在1分钟内同步完成,全局命令最长需要1小时同步,测试阶段建议使用服务器级命令。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 22:45:01