skip to Main Content

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 😀

2

Answers


  1. Chances are the channel simply isn’t cached at the time. If the result is undefined, try to fetch the channel. If that doesn’t work, the error will be more precise about your permissions.

    
    let channel = client.channels.cache.get("id");
    
    if (!channel) {
      try {
        channel = await client.channels.fetch("id");
      } catch(e) {
        console.log(e);
      }
    }
    
    if (channel) channel.send(payload);
    

    In order for this to work, your current function must have the async modifier applied, i.e. async function(...) {} or async (...) => {}, however you wrote it.

    Login or Signup to reply.
  2. 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');
        }
      },
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search