B
B
BruhName2020-07-21 14:48:40
Node.js
BruhName, 2020-07-21 14:48:40

TypeError: Cannot read property 'name' of undefined How to solve this error?

Hi all! I made a bot, it worked about 5 minutes ago, and now, suddenly, the terminal points to this line of code
bot.commands.set(props.help.name,props);
and here is the code itself

const Discord = require('discord.js');
const bot = new Discord.Client();
bot.commands = new Discord.Collection();
const fs = require("fs");
let config =  require("./botconfig.json");
let token = config.token;
let prefix = config.prefix;
let profile = require('./profile.json');

fs.readdir('./cmds/',(err,files)=>{
  if(err) console.log(err);
  let jsfiles = files.filter(f => f.split(".").pop() === "js")
  if(jsfiles.length <=0) console.log("нет комманд для загрузки");
  console.log(`загружено ${jsfiles.length} комманд`)
  jsfiles.forEach((f,i)=>{
    let props = require(`./cmds/${f}`)
    console.log(`${i+1}.${f} Загружено!!!`)
    bot.commands.set(props.help.name,props);
    
  })
});

bot.on('ready', () => {
  console.log(`получил пинок под жепу ${bot.user.username}`)
  bot.generateInvite(["ADMINISTRATOR"]).then(link => {console.log(link)} );
});

bot.on('message', async message => {
  if(message.author.bot) return;
  if(message.channel.type == "dm") return;
  let uid = message.author.id;
  if(!profile[uid]){
    profile[uid] ={
      coins:10,
      warns:0,
      xp:0,
      lvl:0,
      ban:0,
      c:0,

    };
  };
  let u = profile[uid];
  u.coins++;
  u.xp++;
  if(u.xp>= (u.lvl * 5)){
    u.xp = 0;
    u.lvl+=1;
    
    
    
  }

  fs.writeFile('./profile.json',JSON.stringify(profile),(err)=>{
    if(err) console.log(err);
  }); //4:00 #3
  bot.send = function (msg){
      message.channel.send(msg);
  };
  let messageArray = message.content.split(" ");
  let command = messageArray[0].toLowerCase();
  let args = messageArray.slice(1);
  if(!message.content.startsWith(prefix)) return;
  let cmd = bot.commands.get(command.slice(prefix.length));
  if(cmd) cmd.run(bot,message,args);
  
});

bot.login(token);

do not scold for govnokod, I tried. Thanks in advance! once again I remind the error TypeError: Cannot read property 'name' of undefined

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vyacheslav, 2020-07-23
@s0ftik3

The problem is most likely that you either don't have the commands in the folder

./cmds
or some of the commands are empty. For example, there is no name value in module.exports.help , or your command simply does nothing. Look for the error in this folder.
udp: was able to artificially get your error. Check all the files in /cmds, you definitely have one of them empty.

A
Alexander, 2020-07-21
@Alexandre888

"I did my best" ?
since when is copying code from a little-known "teacher" an effort?
the code is written in a hurry, a similar inscription appears in the terminal if there are errors in the code.
I categorically do not recommend that you use a similar code scheme, but there are options that are much more compact and practical:

const Discord = require('discord.js');
const client = new Discord.Client();

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

client.on('message', msg => {
  if (msg.content === 'ping') {
    msg.reply('Pong!');
  }
});

client.login('token');

* code from https://discord.js.org/#/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question