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

Discord机器人注册命令时遇TypeError: Do not know how to serialize a BigInt

解决Discord机器人注册命令时的TypeError: Do not know how to serialize a BigInt错误

问题背景

我用JavaScript开发Discord机器人,需要在index.js中注册命令才能部署。项目结构如下:

  • index.js
  • node_modules
  • 其他配置文件...
  • commands
    • command_folder1
      • command1.js
      • command2.js
    • command-folder2
      • command3.js
      • command4.js
    • 其他命令文件夹...

注册命令过程中遇到错误:TypeError: Do not know how to serialize a BigInt

相关代码

原index.js关键代码

const fs = require('fs');
const path = require('path');
const { REST, Routes } = require('discord.js');
const { clientId, guildId, token } = require('./config.json');

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] The command at ${filePath} is missing a required "data" or "execute" property.`);
		}
	}
}

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

(async () => {
	try {
		console.log(`Started refreshing ${commands.length} application (/) commands.`);

		const data = await rest.put(
			Routes.applicationGuildCommands(clientId, guildId),
			{ body: commands },
		);

		console.log(`Successfully reloaded ${data.length} application (/) commands.`);
	} catch (error) {
		console.error(error);
	}
})();

引发问题的createcharacter.js关键代码

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

module.exports = {
	data: new SlashCommandBuilder()
		.setName('createcharacter')
		.setDescription('创建你的角色')
		.addIntegerOption(option =>
			option.setName('id')
				.setDescription('角色ID')
				.setRequired(true)
				.setMinValue(BigInt(1))), // 这里误用了BigInt
	async execute(interaction) {
		// 命令执行逻辑
	},
};

修改后的index.js(未解决的尝试)

// 错误的序列化处理方式
commands.push(JSON.stringify(command.data.toJSON(), (key, value) => 
	typeof value === 'bigint' ? value.toString() : value
));

错误输出

TypeError: Do not know how to serialize a BigInt
    at JSON.stringify (<anonymous>)
    at ... // 堆栈跟踪信息

解决方案

这个错误核心是Discord.js命令数据中包含BigInt类型,但JSON序列化不支持BigInt,分两种场景解决:

场景1:命令参数误用BigInt

Discord的Slash命令Integer参数只支持32位常规整数,不需要用BigInt。直接把createcharacter.js里的BigInt转为普通数字即可:

// 修改createcharacter.js的参数设置
.addIntegerOption(option =>
	option.setName('id')
		.setDescription('角色ID')
		.setRequired(true)
		.setMinValue(1) // 用普通数字代替BigInt(1)
)

场景2:命令数据需保留BigInt(自定义字段)

如果命令data里有自定义BigInt字段,要在序列化时手动转换为字符串,再转回对象(确保Discord API能识别):

// 修改index.js中添加命令的代码
commands.push(JSON.parse(JSON.stringify(command.data.toJSON(), (key, value) => 
	typeof value === 'bigint' ? value.toString() : value
)));

额外注意

  • Discord的Snowflake ID(用户/服务器ID等),Discord.js会自动处理为Snowflake对象,无需手动用BigInt,直接用字符串或让库内部处理即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 11:23:18