簡體   English   中英

discord.js:無法讀取未定義的屬性(讀取“toJSON”)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM