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

将Discord服务器命令改为全局命令后SlashCommands重复

Discord 全局斜杠命令重复问题解决

把路由从Routes.applicationGuildCommands(clientId, guildId)改为Routes.applicationCommands(clientId)后,所有斜杠命令出现重复。核心原因是原服务器级命令未删除,同时新的全局命令又被注册,导致同一命令存在两份。结合你提供的两个文件,以下是具体修复方案:

1. 修复commandHandler.js

当前代码仅加载本地命令,但未向Discord API提交全局命令,也未清理旧缓存,导致命令重复堆积。修复后的代码如下:

async function loadCommands(client) {
    const fs = require('fs');
    const ascii = require('ascii-table');
    const { REST } = require('@discordjs/rest');
    const { Routes } = require('discord.js');
    const table = new ascii().setHeading('Commands', 'Status');

    // 清除本地旧命令缓存
    await client.commands.clear();

    let commandsArray = [];

    const commandsFolder = fs.readdirSync('/Users/roopa/Desktop/projects/LLbot/src/Commands');

    for (const folder of commandsFolder) {
        const commandFiles = fs.readdirSync(`/Users/roopa/Desktop/projects/LLbot/src/Commands/${folder}`).filter((file) => file.endsWith('.js'));

        for (const file of commandFiles) {
            const commandFile = require(`/Users/roopa/Desktop/projects/LLbot/src/Commands/${folder}/${file}`);

            client.commands.set(commandFile.data.name, commandFile);
            commandsArray.push(commandFile.data.toJSON());
            table.addRow(file, '🟩');
        }
    }

    // 向Discord API提交全局命令(覆盖现有命令,避免重复)
    const rest = new REST({ version: '10' }).setToken(client.token);
    try {
        await rest.put(
            Routes.applicationCommands(client.user.id),
            { body: commandsArray }
        );
        console.log('全局命令已成功注册');
    } catch (error) {
        console.error('注册全局命令失败:', error);
    }

    console.log(table.toString(), '\nCommands Loaded');
}
module.exports = { loadCommands };

关键修复点:

  • 取消注释并执行client.commands.clear(),清理本地命令缓存
  • 添加@discordjs/rest依赖(需先安装:npm install @discordjs/rest),用于向Discord API提交命令
  • 新增全局命令注册逻辑,用rest.put覆盖Discord端的命令列表,彻底避免重复
  • 将函数改为async,适配异步操作

2. 优化interactionCreate.js

当前代码在找不到命令时未终止执行,会触发后续报错,优化如下:

const { CommandInteraction } = require('discord.js');

module.exports = {
  name: 'interactionCreate',

  execute (interaction, client) {
    if (!interaction.isChatInputCommand()) return;

    const command = client.commands.get(interaction.commandName);
    if (!command) {
      return interaction.reply({content: '过时的命令', ephemeral: true});
    }
    command.execute(interaction, client);
  }
};

关键优化点:

  • 在!command的判断分支添加return,避免后续代码执行报错

额外步骤:清理旧服务器命令

如果之前的服务器级命令仍存在,需手动清空,可临时执行以下代码(运行一次即可):

const { REST } = require('@discordjs/rest');
const { Routes } = require('discord.js');
const rest = new REST({ version: '10' }).setToken('你的Bot令牌');

// 清空指定服务器的所有旧命令
rest.put(Routes.applicationGuildCommands('你的ClientID', '你的服务器ID'), { body: [] })
  .then(() => console.log('服务器旧命令已清空'))
  .catch(console.error);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 18:43:14