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

Discord.js v14交互失败求助:仅输出登录日志无其他反馈

Discord.js v14 交互失败问题解决

问题现象

执行/hugging-face命令时显示“this interaction failed”,控制台仅输出“Logged in as...”启动日志,无交互相关的测试日志(如test1/test3)输出。已确认机器人令牌正确,且分配了管理员、Bot、application.commands权限。

相关代码文件

index.js

const { Client, GatewayIntentBits, Events } = require('discord.js')
const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.GuildPresences,
        GatewayIntentBits.GuildMessageReactions,
        GatewayIntentBits.DirectMessages,
        GatewayIntentBits.MessageContent
    ]
})


client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
});

const options = [
    '🐭',
    'https://media.giphy.com/media/wJZTbXayokOgbCfyQe/giphy.gif',
    'https://media.giphy.com/media/QXh9XnIJetPi0/giphy.gif',
    '🐁',
];

client.on(Events.MessageCreate, message => {
    console.log(message);
});

client.on(Events.InteractionCreate, interaction => {
    console.log(`test1`);
    if (!interaction.isCommand()) return;
        console.log(`test3`);
    if (interaction.commandName === 'hugging-face') {
        console.log(`test3`);
         interaction.reply(
             options[Math.floor(Math.random() * options.length)]
        );
    }
});

client.login(process.env.TOKEN);

package.json

{
  "name": "discord-slash-bot",
  "version": "1.0.0",
  "description": "package install",
  "main": "index.js",
  "scripts": {
    "register": "node -r dotenv/config discord-slash-bot/register.js",
    "start": "node -r dotenv/config index.js"
  },
  "keywords": [
    "discord.js",
    "@discordjs/rest",
    "discord-api-types",
    "dotenv"
  ],
  "author": "conner",
  "license": "ISC",
  "dependencies": {
    "@discordjs/rest": "^1.5.0",
    "axios": "^1.2.3",
    "discord-api-types": "^0.37.28",
    "discord.js": "^14.7.1",
    "dotenv": "^16.0.3"
  }
}

register.js

const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const commands = [
    {
        name: 'hugging-face',
        description: 'input images and prompt',
    },
];

const rest = new REST({ version: '10' }).setToken(process.env.TOKEN);

(async () => {
    try {
        console.log('Started refreshing application (/) commands.');

        await rest.put(Routes.applicationCommands(process.env.APP_ID), {
            body: commands,
        });

        console.log('Successfully reloaded application (/) commands.');
    } catch (error) {
        console.error(error);
    }
})();

修复步骤

1. 统一API版本(核心问题)

register.js中REST实例使用v10版本,但Routes导入自v9,版本不匹配会导致命令注册后机器人无法识别交互请求。修改register.js:

// 替换原有Routes导入
const { Routes } = require('discord-api-types/v10');

2. 添加交互错误捕获

交互回复失败时无错误日志,无法定位问题。修改index.js中的InteractionCreate事件处理:

client.on(Events.InteractionCreate, async interaction => {
    console.log(`test1`);
    if (!interaction.isCommand()) return;
    console.log(`test2`);
    if (interaction.commandName === 'hugging-face') {
        console.log(`test3`);
        try {
            await interaction.reply(
                options[Math.floor(Math.random() * options.length)]
            );
        } catch (error) {
            console.error('交互回复出错:', error);
            // 处理已超时的情况
            if (interaction.replied || interaction.deferred) {
                await interaction.followUp({ content: '命令执行失败', ephemeral: true });
            } else {
                await interaction.reply({ content: '命令执行失败', ephemeral: true });
            }
        }
    }
});

3. 优化命令注册范围(可选,加速测试)

全局命令需1小时生效,若要快速验证,可改为注册到指定服务器:

  1. 在.env中添加GUILD_ID(你的服务器ID)
  2. 修改register.js中的路由:
await rest.put(Routes.applicationGuildCommands(process.env.APP_ID, process.env.GUILD_ID), {
    body: commands,
});

4. 精简Intents

仅处理slash命令的机器人不需要多余的intents,精简后避免不必要的权限请求:

const client = new Client({
    intents: [GatewayIntentBits.Guilds]
});

完成以上修改后,重新执行npm run register注册命令,再启动机器人,即可正常响应/hugging-face命令。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 20:20:25