Home > OS >  Why can't my bot get channels? discord.js
Why can't my bot get channels? discord.js

Time:10-26

I get this error in my console: TypeError: Cannot read property 'channels' of undefined.

The code that is causing the problem:

const channel = guild.channels.cache.find(c => c.type === 'text' && channel.permissionsFor(guild.me).has('SEND_MESSAGES'))

Index.js:

client.on("guildCreate", guild => {
  client.commands.get("joinserver").execute(guild)
});

joinserver.js:

const Discord = require("discord.js")

module.exports = {
  name: 'joinserver',
  description: '',
  execute(message, args, client, guild) {
    const channel = guild.channels.cache.find(c => c.type === 'text' && channel.permissionsFor(guild.me).has('SEND_MESSAGES'))
    const embeds = new Discord.MessageEmbed()
    channel.send(embeds)
  }
}

CodePudding user response:

You passing your guild object into the execute function as first parameter (which is not guild but message parameter). So your guild is accessible inside execute function as message right now. You should pass guild into correct argument (4th argument in your example) or change the way how execute function receiving arguments.

client.on("guildCreate", guild => {
  // This should work: you need to pass null to `message`, `args` and `client`
  // because these arguments marked as required
  client.commands.get("joinserver").execute(null, null, null, guild)
});
  • Related