首页 > 解决方案 > discord.js - 将 GuildChannel 转换或转换为 TextChannel

问题描述

我正在尝试从 ID 或提及中检索某个频道。每当我从公会的频道列表中检索频道时,它都是以 a 的形式出现的GuildChannel,它不能直接使用 .send(message) 将消息发送到。我已经通过文档简要查看了一种从Guild ChannelTextChannel | 转换为其中一个的方法。DM频道 | 新闻频道,但无济于事。

任何帮助表示赞赏,谢谢。

标签: javascriptnode.jsdiscord.js

解决方案


如果您尝试根据已发送的消息执行此操作,那么您应该可以使用。

message.guild.channels.cache.get("CHANNEL_ID").send("message");

从缓存访问频道时,最好知道您收到的频道类型是 GuildChannel,因为库不知道您正在检索哪种频道。TextChannel、VoiceChannel 和 NewsChannel 都继承自 GuildChannel,因此 intellisense 和 Typescript 将无法识别频道类型特定的方法和属性,例如send(). 如果您确实在检索 TextChannel,那么 send 方法实际上是可用的。

如果您使用的是 Typescript 并收到错误消息Property 'send' does not exist on type 'GuildChannel'.ts(2339)。您可以通过首先检查频道的类型并确保它是文本频道来解决此问题。

来源:https ://github.com/discordjs/discord.js/issues/3622#issuecomment-565550605


推荐阅读