MegaMix_Craft
MegaMix_Craft

Reputation: 2210

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!

Upvotes: 0

Views: 1013

Answers (2)

Elitezen
Elitezen

Reputation: 6710

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

Upvotes: 0

gavin
gavin

Reputation: 1354

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

Upvotes: 1

Related Questions