0
votes

I'm trying to make my bot send a message to a specific channel on Discord. However, when I use the command it results in an error: TypeError: Cannot read property 'cache' of undefined .

Here's my code:

module.exports = {
    name: 'send',
    description: 'sends a message testing',
    execute(client) {
        const channel01 = client.channels.cache.find(channel => channel.id === "768667222527705148");
        channel01.send('Hi');
    },
};

In my index.js file, I have the following in the beginning, so I don't think the client usage in front of .channels is the problem.

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

const client = new Discord.Client();
client.commands = new Discord.Collection();

Here's my Index.js file.

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

const client = new Discord.Client();
client.commands = new Discord.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.name, command);
}

const cooldowns = new Discord.Collection();

client.once('ready', () => {
    console.log('Ready!');
    client.user.setActivity("KevinYT", {
        type: "WATCHING",
        url: "https://www.youtube.com/channel/UCUr50quaTjBKWL1fLuWRvCg?view_as=subscriber"
      });
});

client.on('message', message => {

    if (!message.content.startsWith(prefix) || message.author.bot) return;

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

    const command = client.commands.get(commandName)
        || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));

    if (!command) return;

    if (command.guildOnly && message.channel.type === 'dm') {
        return message.reply('I can\'t execute that command inside DMs!');
    }

    if (command.args && !args.length) {
        let reply = `You didn't provide any arguments, ${message.author}!`;

        if (command.usage) {
            reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
        }

        return message.channel.send(reply);
    }

    if (!cooldowns.has(command.name)) {
        cooldowns.set(command.name, new Discord.Collection());
    }

    const now = Date.now();
    const timestamps = cooldowns.get(command.name);
    const cooldownAmount = (command.cooldown || 3) * 1000;

    if (timestamps.has(message.author.id)) {
        const expirationTime = timestamps.get(message.author.id) + cooldownAmount;

        if (now < expirationTime) {
            const timeLeft = (expirationTime - now) / 1000;
            return message.reply(`please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${command.name}\` command.`);
        }
    }

    timestamps.set(message.author.id, now);
    setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);

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

client.login(token);

What am I missing? Thanks!

1
I suppose your client is not an instance of client itself.. Can you send the command handler of getting this send file?Radnerus
I added my index.js file to the initial question. I believe the command handler starts at line 8 or 27.kevinw1015
Like Arun Kumar Mohan said, you are passing message and args as parameters but in this file you are passing client. The parameters have to be the same and also in same order.Radnerus

1 Answers

0
votes
command.execute(message, args)

You're passing the execute function message (a Message object) as the first argument but you're trying to use it as a Client object. The code fails since message.channels is undefined (the confusion arises from the argument name used in the execute function).

You can use message.client to get access to the Client object.

module.exports = {
  // ...
  execute (message) {
    const channel01 = message.client.channels.cache.find(
      (channel) => channel.id === '768667222527705148'
    )
    // ...
  },
}