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

176
Vistas
Searching for a message without knowing exact channel [discord.js]

I'm trying to get specific message content on the server by its ID, but without knowing exact channel. This is the code I'm using:

message.guild.channels.cache.filter(c => c.type === 'GUILD_TEXT').each((c) => {
  console.log(c.messages.cache.get("ID").content)
})

but the only thing I get in the end is:

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

I'd really appreciate if someone helps me to find more optimal way to do it or to fix mine!

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

This is what I found to work...

message.guild.channels.cache.filter(c => c.type === 'GUILD_TEXT').each(async c => {
  let getMsg = async () => { 
    let msg = await c.messages.fetch(/* ID HERE */).catch(err => err)
    return msg
  }
  getMsg().then(msg => {
    if(msg.content !== undefined){
      console.log(msg.content)
      message.reply(`
        Content: **${msg.content}**\n\n
        Author: **<@${msg.author.id}>**\n\n
        Channel: **<#${msg.channel.id}>**
      `)
    }
  })
})

How this works...

The Problem...

You were trying to parse large amounts of data in a very quick time, much faster than discord's api will allow you to. So I made a async function to return the message at a pace that discord allows you to without error.

The getMsg() Function...

Inside of the getMsg() function, I awaited a fetch to all the channels. the .catch() function at the end is crucial otherwise this will end your process with an error code.

Calling the Function...

I then called the function and awaited it to finish then checked if the message.content it got was undefined, because for some odd reason if I don't it will throw an error, I believe it is because it is trying to log every channels return on the message and not every channel was able to fetch it which causes the error.

Extra...

I also added it to reply to you with the message content, the author, and the channel as well just to give you an idea of what you can access with the returned message, but that is easily removable if you don't want that.


not amazingly optimized but it does what you want it to :)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Handle the possibility of .get() returning undefined, either with a conditional or optional chaining.

message.guild.channels.cache.filter(c => c.type === 'GUILD_TEXT').each((c) => {
  console.log(c.messages.cache.get("ID")?.content)

// Or

   if (c.messages.cache.get("ID")) {     
     console.log(c.messages.cache.get("ID").content);
   }
})

I'd recommend a different approach. For more readable code use some variables, then use the find() method.

const textChannel = message.guild.channels.cache
   .filter(c => c.type === 'GUILD_TEXT')
   .find(c => c.messages.cache.has('ID'));
if (!textChannel) return console.log("No result");

const msg = textChannel.messages.cache.get('ID');

console.log(msg.content);
about 4 years ago · Juan Pablo Isaza 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