Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
TypeError: Cannot read properties of undefined (reading 'fetch') Discord.js v13

Trying to get 10 messages from every channel in a guild and read the contents then see if they contain a certain string but getting the error below

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

This is my code

interaction.guild.channels.cache.forEach(c => {
   c.messages.fetch({limit: 10}).then(msgs => {
      msgs.forEach(m => {
         if (m.content.includes(interaction.options.getString('text',true))) {
            // will do stuff here
         }
      }).catch(err => console.error(err))
   })
})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

interaction.guild.channels.cache is a collection of GuildChannel.

GuildChannels combines all theses categories of channels.

channels

Unfortunately, only Text Channels and News Channels have a messages property.

Maybe you need to check this condition before fetch the messages.

interaction.guild.channels.cache.forEach(c => {
   if(c.type == 'GUILD_TEXT'){
      c.messages.fetch({limit: 10}).then(msgs => {
         msgs.forEach(m => {
            if (m.content.includes(interaction.options.getString('text',true))) {
               // will do stuff here
            }
         }).catch(err => console.error(err))
      })
   }
})

You can also filter the channels

interaction.guild.channels.cache.filter((c) => c.type == 'GUILD_TEXT').forEach(c => {
   c.messages.fetch({limit: 10}).then(msgs => {
      msgs.forEach(m => {
         if (m.content.includes(interaction.options.getString('text',true))) {
            // will do stuff here
         }
      }).catch(err => console.error(err))
   })
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!