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

运行node index.js load时遇DiscordAPIError[50035]重复命令名错误求助

问题:Discord.js 加载斜杠命令时触发重复名称错误

代码与错误信息

我的index.js代码:

const LOAD_SLASH = process.argv[2] == "load"

const CLIENT_ID = "981858607362629663"
const GUILD_ID = "970702726348546078"

const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent,
        GatewayIntentBits.GuildMembers,
        GatewayIntentBits.GuildVoiceStates
    ]
})

client.slashcommands = new Discord.Collection()
client.player = new Player(client, {
    ytdlOptions: {
        quality: "highestaudio",
        highWaterMark: 1 << 25
    }
})

let commands = []

const slashFiles = fs.readdirSync("./slash").filter(file => file.endsWith(".js"))
for (const file of slashFiles){
    const slashcmd = require(`./slash/${file}`)
    client.slashcommands.set(slashcmd.data.name, slashcmd)
    if (LOAD_SLASH) commands.push(slashcmd.data.toJSON())
}

执行node index.js load后报错:

DiscordAPIError[50035]: Invalid Form Body 5[APPLICATION_COMMANDS_DUPLICATE_NAME]: Application command names must be unique at SequentialHandler.runRequest

已尝试卸载重装discord.js,问题未解决。


解决方案
  • 排查命令名称重复:检查./slash目录下所有命令文件,确保每个命令的data.name完全唯一。注意Discord命令名称不区分大小写,Play和play会被判定为重复。
  • 添加加载时去重逻辑:修改命令加载循环,跳过重复名称的命令,避免生成重复的命令数组:
    const slashFiles = fs.readdirSync("./slash").filter(file => file.endsWith(".js"))
    const commandNames = new Set()
    let commands = []
    
    for (const file of slashFiles){
        const slashcmd = require(`./slash/${file}`)
        const cmdName = slashcmd.data.name
        
        if (commandNames.has(cmdName)) {
            console.warn(`发现重复命令:${cmdName}(文件:${file}),已跳过`)
            continue
        }
        
        commandNames.add(cmdName)
        client.slashcommands.set(cmdName, slashcmd)
        if (LOAD_SLASH) commands.push(slashcmd.data.toJSON())
    }
    
  • 清理现有命令:如果是服务器专属命令,可先清空服务器内已存在的命令,再重新提交新命令,避免与旧命令冲突:
    if (LOAD_SLASH) {
        const { REST, Routes } = require('discord.js');
        const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN);
    
        // 清空服务器现有命令
        await rest.put(
            Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID),
            { body: [] }
        );
    
        // 提交新命令
        await rest.put(
            Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID),
            { body: commands }
        );
    }
    
  • 检查重复文件:确认./slash目录下没有重复的命令文件(如play.js和play_副本.js但内部命令名称一致)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 17:25:29