簡體   English   中英

類型錯誤:無法讀取未定義的屬性(讀取“獲取”)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.cacheGuildChannel的集合。

GuildChannels 結合了所有這些類別的頻道。

頻道

不幸的是,只有文本頻道新聞頻道具有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))
   })
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM