【问题标题】:Extended Client missing functions扩展客户端缺少功能
【发布时间】:2021-12-08 05:22:30
【问题描述】:

我目前正在将我的 discord 机器人从 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] })

【问题讨论】:

    标签: typescript discord.js typescript-typings


    【解决方案1】:

    可以有多种类型的频道(文本、语音等),TypeScript 不知道你在.finding 哪种类型的频道,而且并非每种频道类型都有.send 功能(你无法在语音通道中发送消息)。

    要解决这个问题,如果您确定存储在 client.config 中的频道 ID 是文本频道 ID,则可以使用 type assertion 将该变量的类型更改为 TextChannel

    它的外观如下:

    const channel = client.channels.cache.find((channel) => channel.id == client.config.ids.channelIDs.dev.botTestLobby) as TextChannel
    channel.send({ embeds: [loginMessage] })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 2018-09-09
      • 2021-02-18
      • 2011-10-07
      • 2017-01-05
      • 2010-11-13
      相关资源
      最近更新 更多