无法读取对象中未定义的属性(读取“设置”)。<anonymous>

问题描述 投票:0回答:1

这是我的代码,我不知道为什么它不起作用,它用于discord.js

// Load command files
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}

不知道该尝试什么我搜索了所有内容,但没有看到问题,我在终端中收到此消息:

TypeError: Cannot read properties of undefined (reading 'set')
    at Object.<anonymous>
javascript discord.js
1个回答
0
投票

类“client”缺少公共属性“commands”。该属性需要定义为一个新的map(key, value)

解决方法如下。

client.commands = new Discord.Collection(); // or new Map()

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}
© www.soinside.com 2019 - 2024. All rights reserved.