Revenge
Revenge

Reputation: 5

TypeError: Cannot read property 'execute' of undefined

Before I start, I am really sorry about making a new post but I feel like the old one was messy and didn't give proper information that is probably needed to solve this sorry. So, I've started making my own discord bot and it ran just fine but after like a day I got an error message every time I try to use a command ill use my version command as an example (the problem is with every command) the bot starts up i get a starting message but each time I try to use command the console responds with

TypeError: Cannot read property 'execute' of undefined
    at Client.<anonymous> (C:\Users\Reven\Desktop\DiscordBot\index.js:42:39)
    at Client.emit (events.js:310:20)
    at MessageCreateAction.handle (C:\Users\Reven\Desktop\DiscordBot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\Reven\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\Reven\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\Reven\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\Reven\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\Reven\Desktop\DiscordBot\node_modules\ws\lib\event-target.js:125:16)
    at WebSocket.emit (events.js:310:20)
    at Receiver.receiverOnMessage (C:\Users\Reven\Desktop\DiscordBot\node_modules\ws\lib\websocket.js:797:20)

so here is the code that i currently have

const Discord = require('discord.js');

const client = new Discord.Client();

const prefix = '-'

const fs = require('fs');

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));

const welcome = require('./welcome')

client.once('ready', () =>{
    console.log('Aiko is working!');
    client.user.setActivity(' you!', { type: "LISTENING" });
});

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

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

if (command === 'version'){
        client.commands.get('version').execute(message, args);

client.login('MyToken');

And in the version.js file, I have

module.exports = {
    name: 'version',
    description: "current version",
    execute(message, args){
        message.channel.send('Version 1.0.0 ALPHA build');
    }
}```

and the bot doesn't send welcome messages anymore this is the code that i use for them ```module.exports = (client) => {
    const channelId = '757493821251649608' // welcome channel
    const targetChannelId = '757521186929246219' // rules and info
  
    client.on('guildMemberAdd', (member) => {
      const message = `Hi, hope you enjoy your stay <@${
        member.id
      }> , Oh i almost forgot to tell you, check out! ${member.guild.channels.cache
        .get(targetChannelId)
        .toString()}`
  
      const channel = member.guild.channels.cache.get(channelId)
      channel.send(message)
    })
  }```

Upvotes: 0

Views: 785

Answers (1)

Elitezen
Elitezen

Reputation: 6710

You forgot to actually set the commands. This block will only work for commands without subfolders.

// Define each file inside /commands/ folder
for(const file of commandFiles){
    // Set the command equal to the file
    const command = require(`./commands/${file}`);

    // Add the command to the collection
    bot.commands.set(command.name, command);
}

Upvotes: 1

Related Questions