![](/img/trans.png)
[英]TypeError: Cannot read properties of undefined (reading 'toJSON') - Discord.js slash commands
[英]discord.js: Cannot read properties of undefined (reading 'toJSON')
我試圖用 discord.js 制作一個機器人,並按照https://discordjs.guide上的教程進行操作。
顯然 data.toJSON() 沒有被識別(即使安裝了構建器)
部署命令.js:
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { token, clientId, guildId } = require('./config.json');
const fs = require('node:fs');
const commands = [];
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
commands.push(command.data.toJSON()); // error here
}
const rest = new REST({ version: '9' }).setToken(token);
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands },
);
console.log('Successfully reloaded application (/) commands.');
}
catch (error) {
console.error(error);
}
})();
[ ]
我的命令文件夾中有一些空文件,這些文件在沒有數據屬性的情況下被讀取。 我刪除它們以解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.