使用Discord.js Slash Command Builder注册命令失败,命令未显示
Discord Slash Command Builder 命令不显示问题排查方案
问题背景
- 非Slash Command Builder方式注册的斜杠命令可正常使用,已确认机器人授权范围无异常
- 使用Slash Command Builder编写的
/ticket-setup命令,代码无报错,但在Discord客户端中无法找到该命令
相关代码
index.js
const { Client, IntentsBitField, PermissionsBitField, Embed, ButtonBuilder, ButtonStyle} = require('discord.js'); const { EmbedBuilder } = require('discord.js'); const { ActionRowBuilder, Events, Collection, ModalBuilder, TextInputBuilder, TextInputStyle } = require('discord.js'); const fs = require('node:fs'); const path = require('node:path'); const client = new Client({ intents: [ IntentsBitField.Flags.Guilds, IntentsBitField.Flags.GuildMembers, IntentsBitField.Flags.GuildMessages, IntentsBitField.Flags.MessageContent, ], }); client.commands = new Collection(); const foldersPath = path.join(__dirname, 'commands'); const commandFolders = fs.readdirSync(foldersPath); for (const folder of commandFolders) { const commandsPath = path.join(foldersPath, folder); const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); for (const file of commandFiles) { const filePath = path.join(commandsPath, file); const command = require(filePath); if ('data' in command && 'execute' in command) { client.commands.set(command.data.name, command); } else { console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); } } } client.on(Events.InteractionCreate, interaction => { console.log(interaction); }); client.on(Events.InteractionCreate, interaction => { if (!interaction.isChatInputCommand()) return; console.log(interaction); }); client.on(Events.InteractionCreate, async interaction => { if (!interaction.isChatInputCommand()) return; const command = interaction.client.commands.get(interaction.commandName); if (!command) { console.error(`No command matching ${interaction.commandName} was found.`); return; } try { await command.execute(interaction); } catch (error) { console.error(error); if (interaction.replied || interaction.deferred) { await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true }); } else { await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); } } }); //Code cut off
ticket-setup.js(Slash Command Builder配置)
const { SlashCommandBuilder, EmbedBuilder, ButtonBuilder, ButtonStyle, ActionRowBuilder, PermissionsBitField, PermissionFlagsBits} = require('discord.js'); module.exports = { data: new SlashCommandBuilder() .setName('ticket-setup') .setDescription('Open a Ticket') .setDMPermission(false), async execute(interaction) { const embed = new EmbedBuilder() .setTitle('Contact Moderation') .setDescription('Create a ticket and contact the moderators with the button below.') .setColor('Blue'); const ticketBtn = new ButtonBuilder() .setCustomId('ticketBtn') .setLabel('Open Ticket') .setEmoji('📤') .setStyle(ButtonStyle.Secondary); const row = new ActionRowBuilder() .addComponents(ticketBtn) await interaction.reply({ embeds: [embed], components: [row] }); } }
解决步骤
执行命令注册脚本
你的index.js只是把命令加载到本地Collection,但没把命令数据提交给Discord API。非Builder方式你肯定有单独的注册脚本,Builder方式也需要这一步。新建deploy-commands.js文件,内容如下:const { REST, Routes } = require('discord.js'); const { clientId, guildId, token } = require('./config.json'); // 自行填写机器人的clientId、目标服务器ID和token const fs = require('node:fs'); const path = require('node:path'); const commands = []; const foldersPath = path.join(__dirname, 'commands'); const commandFolders = fs.readdirSync(foldersPath); for (const folder of commandFolders) { const commandsPath = path.join(foldersPath, folder); const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); for (const file of commandFiles) { const filePath = path.join(commandsPath, file); const command = require(filePath); if ('data' in command && 'execute' in command) { commands.push(command.data.toJSON()); } else { console.log(`[WARNING] 命令文件 ${filePath} 缺少必填的"data"或"execute"属性`); } } } const rest = new REST().setToken(token); (async () => { try { console.log(`开始刷新 ${commands.length} 个应用命令`); // 服务器专属命令,如需全局命令替换为Routes.applicationCommands(clientId) const data = await rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands }); console.log(`成功刷新 ${data.length} 个应用命令`); } catch (error) { console.error(error); } })();配置好
config.json后,运行node deploy-commands.js完成命令注册。验证命令是否被正确加载
在index.js的命令加载循环中添加日志,确认ticket-setup命令被正常加载:if ('data' in command && 'execute' in command) { client.commands.set(command.data.name, command); console.log(`已加载命令: ${command.data.name}`); // 新增日志 }启动机器人后查看控制台,如果未输出这条日志,检查commands目录下的子文件夹是否被正确遍历(确保路径配置无问题)。
清理Discord客户端缓存
有时Discord会缓存旧命令列表,可尝试:- 退出并重新登录Discord客户端
- 输入
/后点击右上角的刷新按钮 - 服务器专属命令等待几分钟同步,全局命令可能需要1小时左右生效
再次确认权限配置
确保机器人的OAuth2授权链接勾选了applications.commands权限,且在目标服务器中拥有管理员权限,或至少拥有使用应用命令权限。
内容的提问来源于stack exchange,提问作者aarush v
相关产品推荐
相关产品推荐

