Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
When running getting: TypeError: Cannot read properties of undefined (reading 'send')

I get this error when running the code, my research says its something about send not being available on it or not defined, I don't fully understand it or how to fix it though.

The code:

const { SlashCommandBuilder } = require('@discordjs/builders');
const { Client, Collection, Intents, MessageEmbed } = require('discord.js');

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS] })

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

const channel = client.channels.cache.get('997960087249371136');

do {
  sleep(1000)
  let number = Math.floor(Math.random() * 5);
  
  if(number = 1) {

    channel.send("Carrier under attack!")
    
  } else if(number = 2) {

    channel.send("Carrier leak detected in lower quaters!")
    
  } else if(number = 3) {

    channel.send("All is quiet, for now.")
    
  } else if(number = 4) {

    channel.send("Unidentified aircraft entering our airspace!")
    
  } else {
    console.log("The impossible is possible!")
  } 
}
while(true);

I know the channels do exist.

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

you just created the client, you haven't logged in with your token yet, this is why "client.channels.cache" is empty and contains no channels yet. To fix this you have to put everything on the "ready" event

Also there's some other problems on your code:

  1. You have to async/await the "sleep" function or it's useless
  2. On the if conditions, you have to check with === or ==, not = see here why
  3. 1000 is way too short (1000 millisecondes is 1 second) posting 1 message/s is api request spam. Try using 4 or 5 seconds instead.
const { SlashCommandBuilder } = require('@discordjs/builders');
const { Client, Collection, Intents, MessageEmbed } = require('discord.js');

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS] })

client.on('ready', async () =>{
  const channel = client.channels.cache.get('997960087249371136');

  do {
    await sleep(1000)
    let number = Math.floor(Math.random() * 5);
  
    if(number == 1) return channel.send("Carrier under attack!")
    else if(number == 2) return channel.send("Carrier leak detected in lower quaters!")
    else if(number == 3) return channel.send("All is quiet, for now.")
    else if(number == 4) return channel.send("Unidentified aircraft entering our airspace!")
    else return channel.send("The impossible is possible!")
  } while(true);
})

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

client.login("YOUR TOKEN")
about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda