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))
})
})
interaction.guild.channels.cache is a collection of GuildChannel.
GuildChannels combines all theses categories of 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))
})
})