TypeError: Cannot read properties of undefined (reading 'fetch') Discord.js v13
TypeError: Cannot read properties of undefined (reading 'fetch') Discord.js v13
试图从公会中的每个频道获取 10 条消息并读取内容然后查看它们是否包含特定字符串但出现以下错误
TypeError: Cannot read properties of undefined (reading 'fetch')
这是我的代码
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
是 GuildChannel.
的集合
GuildChannels 结合了所有这些类别的频道。
不幸的是,只有 Text Channels and News Channels 有一个 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))
})
}
})
您还可以过滤频道
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))
})
})
试图从公会中的每个频道获取 10 条消息并读取内容然后查看它们是否包含特定字符串但出现以下错误
TypeError: Cannot read properties of undefined (reading 'fetch')
这是我的代码
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
是 GuildChannel.
GuildChannels 结合了所有这些类别的频道。
不幸的是,只有 Text Channels and News Channels 有一个 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))
})
}
})
您还可以过滤频道
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))
})
})