Discord.js V14斜杠命令失效:无报错仅显示应用未响应
解决Discord.js V14斜杠命令无响应问题
1. 修复部署脚本的API版本不兼容
你的deploy.js使用了discord-api-types/v9和REST版本9,但Discord.js V14要求匹配API版本10,版本不兼容会导致命令交互逻辑失效:
- 替换
discord-api-types/v9为discord-api-types/v10 - 将REST实例的version参数设为
'10'
修改后的核心代码片段:
const { Routes } = require('discord-api-types/v10'); // ... const rest = new REST({ version: '10' }).setToken(token);
2. 确保客户端正确监听交互事件
斜杠命令的响应依赖客户端对interactionCreate事件的处理,V14需明确启用必要意图并注册事件监听:
const { Client, GatewayIntentBits } = require('discord.js'); const client = new Client({ intents: [GatewayIntentBits.Guilds] // 斜杠命令必须的基础意图 }); // 处理交互事件 client.on('interactionCreate', async interaction => { if (!interaction.isChatInputCommand()) return; const command = client.commands.get(interaction.commandName); if (!command) return; try { await command.execute(interaction); } catch (error) { console.error(error); await interaction.reply({ content: '命令执行出错', ephemeral: true }); } }); client.login(token);
3. 完善命令加载与存储逻辑
客户端需要将命令存入Collection集合,才能在交互事件中匹配调用:
const { Collection } = require('discord.js'); client.commands = new Collection(); // 遍历加载命令文件(可复用deploy.js的目录遍历逻辑) getDirectories(__dirname + '/slash').forEach(category => { const commandFiles = fs.readdirSync(category).filter(file => file.endsWith('.js')); for(const file of commandFiles) { const command = require(`${category}/${file}`); client.commands.set(command.data.name, command); } });
4. 检查命令文件的兼容性细节
你的示例命令中导入的MessageActionRow和MessageButton在V14中已被废弃,若后续用到按钮组件需替换为Builder类:
// 替换旧组件导入 const { ActionRowBuilder, ButtonBuilder, ButtonStyle } = require('discord.js');
5. 验证基础配置正确性
- 确认
deploy.js中的服务器ID是实际目标服务器的有效ID,而非占位符"serverid" - 确保机器人账号在目标服务器拥有
应用程序命令相关权限 - 若需全局命令,将部署路径改为
Routes.applicationCommands(clientId)而非服务器级路径
完成以上修改后,重新运行deploy.js部署命令,再重启机器人即可恢复斜杠命令响应。
内容的提问来源于stack exchange,提问作者Bruh Gpt
相关产品推荐
相关产品推荐

