Discord机器人迁移命令至外部文件触发BASE_TYPE_REQUIRED错误求助
问题:Discord机器人迁移命令至JSON文件后出现"0.name[BASE_TYPE_REQUIRED]"错误
开发Discord机器人时,为精简主代码文件、方便后续修改维护,将原本写在主文件中的url命令迁移至外部JSON文件,但运行代码时出现0.name[BASE_TYPE_REQUIRED]: This field is required错误,若将commands数组设为空则可正常运行,请问该如何解决?
主机器人文件
const { Client, GatewayIntentBits, Routes, EmbedBuilder, ActivityType, } = require('discord.js'); const { token, clientId, guildId } = require('../config.json'); const { REST } = require('@discordjs/rest'); const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.DirectMessages, GatewayIntentBits.GuildBans, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, ], }); const { UrlCommand } = require('../Commands/UrlCommand.json'); client.on('ready', (client) => { console.log(`${client.user.username}#${client.user.discriminator} running`); client.user.setPresence({ activities: [{ name: 'connections', type: ActivityType.Watching }], }); }); client.on('interactionCreate', async (interaction) => { if (!interaction.isChatInputCommand()) return; if (interaction.commandName === 'url') { console.log(`called ${interaction.commandName} command`); const urlEmbed = new EmbedBuilder() .setColor('0x0099FF') .setTitle('LEPSES') .setURL('https://lepses.com') .setAuthor({ name: 'LEPSES Bot', // TODO: replace icon iconURL: 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/89/Latin_capital_letter_L_with_descender.svg/497px-Latin_capital_letter_L_with_descender.svg.png', }) .setDescription('You choose, we connect!') .addFields( // TODO : add more data in the fields { name: 'Regular field title', value: 'Some value here' }, { name: '\u200B', value: '\u200B' }, { name: 'Inline field title', value: 'Some value here', inline: true, }, { name: 'Inline field title', value: 'Some value here', inline: true, } ); await interaction.reply({ embeds: [urlEmbed] }); } if (interaction.commandName === 'clear') { if (interaction.options.get('amount') >= 5000) { interaction.reply('You cannot delete more than 5000 messages'); } else { try { const amount = interaction.options.getInteger('amount'); const Channel = interaction.channel; if (amount) { Channel.bulkDelete(amount, true); await interaction.reply(`${amount} messages purged`); } else { Channel.bulkDelete(50, true); await interaction.reply(`50 messages purged`); } } catch (err) { console.log(err.message); } } } }); const rest = new REST({ version: '10' }).setToken(token); async function main() { // const commands = []; const commands = [UrlCommand]; try { console.log( `Started refresing application ${commands.length} commands` ); await rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands, }); client.login(token); } catch (error) { console.log('ERROR================================================='); console.log(error.message); console.log('ERROR================================================='); } } main();
URL命令JSON文件
{ "name": "url", "description": "Visit us now!!" }
解决方案
核心问题
你导入JSON文件时用了解构赋值const { UrlCommand } = require(...),但你的JSON文件直接导出的是一个命令对象,并没有名为UrlCommand的键,导致UrlCommand变量的值是undefined,Discord API接收不到有效的命令结构,所以抛出name字段缺失的错误。
修复步骤
- 修正导入语句
把导入JSON的代码改成直接赋值,不需要解构:
const UrlCommand = require('../Commands/UrlCommand.json');
- 验证命令结构(可选)
确认JSON文件中的命令符合Discord API要求:
name只能包含小写字母、数字、-或_,长度1-32字符description长度1-100字符
你的JSON文件结构是合规的,修正导入后即可正常运行。
优化建议(多场景)
如果后续要添加多个命令,可以把JSON文件改成数组格式,方便批量导入:
[ { "name": "url", "description": "Visit us now!!" }, { "name": "clear", "description": "Clear messages in the channel" } ]
然后在主文件中直接导入整个数组:
const commands = require('../Commands/commands.json');
这样不需要手动构建命令数组,更便于维护。
内容的提问来源于stack exchange,提问作者Axis
相关产品推荐
相关产品推荐

