javascript - TypeError:无法读取未定义的属性(读取“toJSON”)和另外一个
问题描述
const fs = require('fs');
const { Client, Collection, Intents } = require('discord.js');
const { token } = require('./config.json');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.data.name, command);
}
client.once('ready', () => {
console.log('Ready!');
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
return interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
client.login(token);
that's the index.js file
the error= TypeError: Cannot read properties of undefined (reading 'name')
at Object.<anonymous> (C:\Users\shado\OneDrive\Pulpit\Command\index.js:12:35)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47
there is also a second issue
TypeError: Cannot read properties of undefined (reading 'toJSON')
at Object.<anonymous> (C:\Users\shado\OneDrive\Pulpit\Command\deploy-commands.js:11:29)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47
const fs = require('fs');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { clientId, guildId, token } = require('./config.json');
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());
}
const rest = new REST({ version: '9' }).setToken(token);
rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
.then(() => console.log('Successfully registered application commands.'))
.catch(console.error);
^ deploy-commands.js 第二个问题涉及到
我不知道发生了什么,它昨天工作,一切都崩溃了,请帮忙
自从上次在我的电脑上运行以来,没有发生重大变化。这两个文件本身的代码完全没有,只是添加了额外的命令
解决方案
我遇到过同样的问题。尝试打印 command 和 command.data 并查看类型。对我来说,当我执行 typeof(command.data) 时,我看到我的一个对象是未定义的。这是由于命令文件夹中的 user.js 文件中的拼写错误。我写的不是数据,而是数据。
推荐阅读
- python-3.x - (Python)我想将以下带有 return 语句的 for 循环放在一行上:
- python - discord.ext.commands.errors.CommandInvokeError:命令引发异常:TypeError:'list'对象不可调用
- c# - 如果用户在hangman c#中正确猜测,我如何显示用户的输入
- docker - 评估 Dockerfile 中的 bash 表达式
- http - 增加 K8S 入口的 URL 长度限制
- jquery - 选择 2 复选框并能够在 thingspeak 中下载文件?
- c++ - Qt5 - QML:如何正确连接 ProgressBar 和 Button 以进行长时间运行的循环计算
- c++ - 确定用户字符输入c ++的结束
- laravel - 如何将选定的父类别放入编辑表单?
- flutter - Flutter 中的“Image”和“ImageProvider”有什么区别?