Search code examples
javascriptnode.jsdiscorddiscord.jsbots

Cannot read properties of undefined (reading 'send') (Discord.js V14)


Can someone please explain why its not sending the message?

Im currently testing smth so when you say ">send", My bot sends a message to a channel:

if (msg.content === prefix + "send") {
        const channel = client.channels.cache.get('1137720568850956300');
        channel.send('test succes');
    
    }

the 1137720568850956300 is my channel id, but i keep getting this error:

TypeError: Cannot read properties of undefined (reading 'send')

Trust me its the correct id (i doubled checked) and idk why its not working

what surprises me is that i copy and pasted it from the discord.js guide thinking it would work but it doesnt. if you really want it heres my full code:

const {
  Events,
  EmbedBuilder,
  Client,
  GatewayIntentBits,
} = require('discord.js');
const greeting = [
  'yo wsp',
  'hi',
  'hello muthafucka',
  'stfu and talk with other people',
];
const lolo = greeting[Math.floor(Math.random() * greeting.length)];
const prefix = '>';
const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMembers,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent,
  ],
});
const EmbedReply = {
  title: 'Do not ping this person.',
  description: ' 🙏 hes ded because of the pings already 🙏',
  color: '10038562',

  footer: {
    text: 'Current version: 0.0.1',
    icon_url:
      'https://cdn.discordapp.com/attachments/1141734104543539220/1141750630763987015/pooperson.jpg',
  },
};

module.exports = {
  name: Events.MessageCreate,
  once: false,
  execute(msg) {
    if (msg.content === '<@1140642830302335098>') {
      msg.reply(`${lolo}`);
    }

    if (msg.content === '<@1089518964360609792>') {
      msg.reply({ content: `<@${msg.author.id}>`, embeds: [EmbedReply] });
    }

    if (msg.content === prefix + 'test') {
      msg.reply('test succesful');
    }

    if (msg.content === prefix + 'send') {
      const channel = client.channels.cache.get('1137720568850956300');
      channel.send('test succes');
    }
  },
};

thanks in advance :D


Solution

  • It's because you instantiate a new client and it's not the one logged in and listening to the MessageCreate event. You will need to remove that and get the client from the msg variable:

    const { Events } = require('discord.js');
    const greeting = [
      'yo wsp',
      'hi',
      'hello muthafucka',
      'stfu and talk with other people',
    ];
    const prefix = '>';
    
    const EmbedReply = {
      title: 'Do not ping this person.',
      description: ' 🙏 hes ded because of the pings already 🙏',
      color: '10038562',
      footer: {
        text: 'Current version: 0.0.1',
        icon_url:
          'https://cdn.discordapp.com/attachments/1141734104543539220/1141750630763987015/pooperson.jpg',
      },
    };
    
    module.exports = {
      name: Events.MessageCreate,
      once: false,
      execute(msg) {
        if (msg.content === '<@1140642830302335098>') {
          // you'll also need to move this here or it will be
          // the same message every time
          const lolo = greeting[Math.floor(Math.random() * greeting.length)];
    
          msg.reply(`${lolo}`);
        }
    
        if (msg.content === '<@1089518964360609792>') {
          msg.reply({ content: `<@${msg.author.id}>`, embeds: [EmbedReply] });
        }
    
        if (msg.content === prefix + 'test') {
          msg.reply('test succesful');
        }
    
        if (msg.content === prefix + 'send') {
          // grab the client here
          const { client } = msg;
          const channel = client.channels.cache.get('1137720568850956300');
    
          if (!channel) return console.log('Channel not found');
    
          channel.send('test succes');
        }
      },
    };