我目前正在作業,將我的不和諧機器人從 js 重寫為 ts。為了使用打字,我擴展了 discord.js 客戶端,我無法獲取頻道并在那里發送訊息。除了使用 type:any 之外,我該如何解決這個問題?
我的擴展客戶:
export class DiscordClient extends Client {
commands
config
}
這在Client型別設定為 時不再起作用DiscordClient。如果我將它設定為任何,它就可以正常作業
client.channels.cache
.find((channel) => channel.id == client.config.ids.channelIDs.dev.botTestLobby)
.send({ embeds: [loginMessage] })
uj5u.com熱心網友回復:
通道的型別可以有很多種(文本、語音等),TypeScript 不知道你在.find輸入哪種型別的通道,也不是每個通道型別都有這個.send功能(你不能在語音通道中發送訊息) .
要解決這個問題,如果您確定存盤在您的頻道 ID 中的client.config是文本頻道 ID,則可以使用型別斷言將該變數的型別更改為TextChannel.
這是它的外觀:
const channel = client.channels.cache.find((channel) => channel.id == client.config.ids.channelIDs.dev.botTestLobby) as TextChannel
channel.send({ embeds: [loginMessage] })
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/330852.html
上一篇:比較打字稿中的顏色字串
下一篇:如何“模擬”一個Ref?
