• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

148
Views
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 3 years ago · Santiago Gelvez
1 answers
Answer question

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 3 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error