簡體   English   中英

Discord.js:無法讀取未定義的屬性“緩存”

[英]Discord.js: Cannot read property “cache” of undefined

我目前正在使用 discord.js 編寫 discord 機器人。 我正在嘗試通過 id 從特定公會獲取特定頻道,然后將消息寫入其中。 我在 grepper 上找到了這個解決方案,但是在運行時出現錯誤“無法讀取未定義的屬性‘緩存’”但是為什么通道未定義? discord.js 文檔將頻道列為公會的屬性: https://discord.js.org/#/docs/main/stable/class/Guild 有誰知道為什么會這樣?

client.guilds.fetch(guildID).channels.cache.get(channelID)

client.guilds.fetch(guildID)是 promise,您需要的是:

client.guilds.fetch(guildID).then(guild => guild.channels.cache.get(channelID).send('HI!'))

如果你想要 go 100% 安全

client.guilds.fetch(guildID)
    .then(guild => 
        guild.channels.fetch(channelID)
            .then(channel => 
                 channel.send('HI!')
));

最簡單快捷的方法是

const channel = client.channels.cache.get("CHANNEL-ID")
channel.send("Hello")

暫無
暫無

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

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