Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

199
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda