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

执行deploy-commands.js时Discord.js Bot意外启动问题

问题解决方案

1. 修复deploy-commands.js意外启动Bot的问题

你的deploy-commands.js肯定不小心引入了Bot启动逻辑,比如直接写了client.login()或者require了bot.js文件。命令注册脚本不需要启动客户端,只需要通过REST API提交命令即可:

  • 打开deploy-commands.js,删除所有和Bot登录、客户端初始化相关的代码(比如const client = new Client(...)、client.login(...)或者require('./bot.js'))
  • 确保脚本结构只保留命令读取和REST注册逻辑,示例如下:
const { REST, Routes } = require('discord.js');
const { clientId, guildId, token } = require('./config.json');
const fs = require('node:fs');
const path = require('node:path');

const commands = [];
const commandsPath = path.join(__dirname, 'commands');
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);
	commands.push(command.data.toJSON());
}

const rest = new REST({ version: '10' }).setToken(token);

(async () => {
	try {
		console.log(`开始刷新 ${commands.length} 条斜杠命令`);
		const data = await rest.put(
			Routes.applicationGuildCommands(clientId, guildId),
			{ body: commands },
		);
		console.log(`成功重载 ${data.length} 条斜杠命令`);
	} catch (error) {
		console.error(error);
	}
})();

运行node deploy-commands.js就只会完成命令注册,不会启动Bot。

2. 修复addform.js提示缺少data/execute属性的问题

检查addform.js的导出逻辑,必须满足以下要求:

  • 使用module.exports导出包含data和execute的对象,不能用exports = {...}或者其他导出方式
  • data必须是SlashCommandBuilder(或其子类)的实例,不能是普通对象
  • execute必须是一个函数(建议用异步函数)

正确示例:

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

module.exports = {
	data: new SlashCommandBuilder()
		.setName('addform')
		.setDescription('添加表单的命令'),
	async execute(interaction) {
		await interaction.reply('表单已添加');
	},
};

另外还要检查:

  • 文件名是否正确,有没有大小写错误(比如AddForm.js和你读取命令时的路径是否匹配)
  • 命令文件是否放在正确的目录下,确保deploy-commands.js和bot.js读取的是同一个命令目录

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 10:57:07