类型错误:无法读取未定义的 Discord.js 的属性“set”

问题描述

我正在使用 discord.js,这是我的代码

const client = new discord.Client();

const fs = require('fs');
const { get } = require('http');

client.commands = new discord.Collection();

const commandFiles = fs.readdirsync('./Commands/').filter(file => file.endsWith('.js'));

const set = new Set();

for(const file of commandFiles){
  const command = require(`./Commands/${file}`);

  client.command.set(command.name,command);
}

const config = require('./config.json');

client.on('ready',() => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message',msg => {
  if (!msg.content.startsWith(prefix) || msg.author.bot) return;

  const args = msg.content.slice(prefix.length).trim().split('/ +/ ');
  const command = args.shift().toLowerCase();
  // ...

  if (!client.commands.has(commandName)) return;

  const Command = client.commands.get(commandName);

  try {
     Command.execute(message,args);
  } catch (error) {
  // ...
  }

  try {
    client.commands.get(command).execute(message,args);
  } catch (error) {
    console.error(error);
    message.reply('there was an error trying to execute that command!');
  }

});
client.on('message',msg => {
  if (msg.content === 'ping') {
    client.commands.get('ping').execute(message,args);
  }else if (msg.content === 'pong') {
    msg.reply('Ping!')
  }else if (msg.content === `${prefix}beep`) {
    msg.channel.send('Boop!');
  }else if (msg.content === `${prefix}boop`) {
    msg.channel.send('Beep!');
  }else if (msg.content === `${prefix}server`) {
    msg.channel.send(`This server's name is: ${msg.guild.name}\nTotal members: 
    ${msg.guild.memberCount}`);
  }else if (msg.content === `${prefix}user-info`) {
    msg.channel.send(`Your username: ${msg.author.username}\nYour ID:    ${msg.author.id}`);
  }

});

const { prefix,token } = require('./config.json');
//...

client.login(config.token);

她是我的错误,在你问之前:是的,我有一个 config.json。

错误

client.command.set(command.name,command);

TypeError: Cannot read property 'set' of undefined
at Object.<anonymous> (C:\Users\Scare\Desktop\spagehtt\index.js:17:18)
?[90m    at Module._compile (internal/modules/cjs/loader.js:1133:30)?[39m
?[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)?[39m
?[90m    at Module.load (internal/modules/cjs/loader.js:977:32)?[39m
?[90m    at Function.Module._load (internal/modules/cjs/loader.js:877:14)?[39m
?[90m    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)?[39m
?[90m    at internal/main/run_main_module.js:18:47?[39m
PS C:\Users\Scare\Desktop\spagehtt>

这是我的问题,使我无法打开我的机器人并运行我正在测试的命令,所以我真的需要帮助。

解决方法

如您所见,client.command 没有 .set,但 client.commands

解决方案:

client.commands.set(command.name,command);