yas
yas

Reputation: 17

discord.js - Issues with sending a message

I have a command handler which it's module and constructed like this.

module.exports ={
    name: "test",
execute(message, args) {
var yes = client.channels.cache.get('818107649912209438')
yes.send('some message')
}

Whenever I try to send a message to a specific channel, The bot returns an error which is;

  var yes = client.channels.cache.get("818107649912209438");
                     ^

TypeError: Cannot read property 'channels' of undefined

And I've been trying to fix this, but no luck. If anyone knows why it doesn't work and would like to help, I'd be thankful.

Upvotes: 0

Views: 179

Answers (2)

Just4you
Just4you

Reputation: 66

Your Question is not clear, So I will show you a example on how to handle, I will show code and explanation later.

Formatting:

  • Folder
  1. File

  • events
  1. message.js
// I used message as msg to shorten my code.
const all_requires = require("../settings/settings"); // Exported all requires
const { config, Discord, client } = all_requires; // Imported what I need
module.exports = (client, msg) => {

    if(msg.author.bot || !msg.content.startsWith(config.prefix)) return;
    const args = msg.content.split(" ").slice(1);
    const command = msg.content.split(' ')[0].slice(config.prefix.length);
    const cmd = client.commands.get(command); //enmap handler
    console.log(cmd);
    if(!cmd) return;

    cmd.run(msg, args);
}
  • commands
  1. ping.js
module.exports.run = (msg) => {
    msg.channel.send("Pong!");
}
  • config
  1. config.json
{
    "token": "TOKEN-HERE",
    "prefix": "+"
}
  • settings
  1. settings.js
const Discord = require("discord.js");
const fs = require("fs");
const enmap = require("enmap");
const config = require("../config/config.json");
const client = new Discord.Client();

const all_requires = { Discord, fs, enmap, config, client }
module.exports = all_requires;

index.js

const all_requires = require("./settings/settings");
const { Discord, fs, enmap, config, client } = all_requires;
client.commands = new enmap;

client.once("ready", () => {
    console.log(`${client.user.tag} is Connected.`);
})

fs.readdir("./events/", (err, files) => {
    if(err) return console.error(err);
    files.forEach(file => {
        const event = require(`./events/${file}`);
        let eventName = file.split(".")[0];
        client.on(eventName, event.bind(null, client));
    });
    console.log(`${files.length} Events Loaded.`)
});

fs.readdir("./commands/", (err, files) => {
    if(err) return console.error(err);
    files.forEach(file => {
        let props = require(`./commands/${file}`);
        let commandName = file.split(".")[0];
        client.commands.set(commandName, props);
    });
    console.log(`Loaded ${files.length} Commands.`);
});

process.on('unhandledRejection', err => console.log(err));
client.login(config.token);

Files Structure

As a handlers you need to show me the whole code to know what you're doing to handle commands, you can use either enmap link like in the example of that basic code, or use discord.js link Handlers to execute the commands, look at message.js Line:8 client.commands.get() here we defined the enmap handler, and at Line:13 for cmd.run() we run the commands, using only module.exports.run and we defined command handler in index.js Line:3 and we set the commands in Line:24 inside the fs for loop.

-Enjoy Coding.

Upvotes: 0

Radnerus
Radnerus

Reputation: 592

Your error 'of undefined' means you are access the channel property through something, but that something is not defined. In this command handler, you are not defining client. You have to pass the client object as a parameter which you have defined in the main file, like execute(message,args,client) and then access the properties or methods of the client.

Upvotes: 2

Related Questions