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

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

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 Relatório

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 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