discord.js配合WOKCommands使用斜杠命令提示Invalid interaction application command错误
「Invalid interaction application command」报错修复方案
核心问题
你配置了slash: "both"让命令同时支持前缀消息命令和斜杠命令,但当前仅实现了消息命令适配的execute回调,WOKCommands触发斜杠命令时找不到对应的交互处理逻辑,导致抛出无效命令错误。
修复代码
将execute字段替换为WOKCommands兼容双触发场景的callback字段,同时适配两种调用逻辑:
const { MessageEmbed } = require("discord.js"); // Simple command for the message module.exports = { name: "ping", slash: "both", testOnly: false, description: "Command to figure out what your current ping is. Also shows API Latency", // 替换原execute为callback兼容消息/斜杠双触发 callback: ({ client, message, interaction }) => { const newEmbed = new MessageEmbed() .setColor("#ffdbac") .setTitle("Ping") .setThumbnail(`https://cometiclachlan.github.io/Uploads/pingpong-removebg.png`) .setTimestamp() .setFooter("v1.2", `https://cometiclachlan.github.io/Uploads/Checkpoint-removebg.png`); // 消息命令触发逻辑 if (message) { newEmbed.setDescription("Pong! Latency is **" + (Date.now() - message.createdTimestamp) + "ms**. API Latency is **" + message.client.ws.ping + "ms**") return message.channel.send({ embeds: [newEmbed] }); } // 斜杠命令触发逻辑,必须调用reply响应交互 if (interaction) { newEmbed.setDescription("Pong! Latency is **" + (Date.now() - interaction.createdTimestamp) + "ms**. API Latency is **" + client.ws.ping + "ms**") return interaction.reply({ embeds: [newEmbed] }); } }, };
其他可能导致报错的原因
- 邀请机器人时未勾选
applications.commands授权 scope,缺少斜杠命令调用权限 - 全局斜杠命令同步最多需要1小时生效,测试阶段建议设置
testOnly: true指定测试服务器,服务器级命令注册即时生效 - discord.js与WOKCommands版本不匹配,WOKCommands v1.7.0+仅适配discord.js v14,使用discord.js v13需降级WOKCommands到v1.6.x版本
内容的提问来源于stack exchange,提问作者CometicLachlan
相关产品推荐
相关产品推荐

