javascript - discord.js - 将 GuildChannel 转换或转换为 TextChannel
问题描述
我正在尝试从 ID 或提及中检索某个频道。每当我从公会的频道列表中检索频道时,它都是以 a 的形式出现的GuildChannel
,它不能直接使用 .send(message) 将消息发送到。我已经通过文档简要查看了一种从Guild Channel
TextChannel | 转换为其中一个的方法。DM频道 | 新闻频道,但无济于事。
任何帮助表示赞赏,谢谢。
解决方案
如果您尝试根据已发送的消息执行此操作,那么您应该可以使用。
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
推荐阅读
- angular7 - 无法在 Angular 7 中实现来自 FusionCharts 的甘特图
- apache-spark - 如何为对象列表(即 StructType)定义火花模式?
- google-apps-script - 我可以随机化 MultipleChoiceItem 中的项目吗?
- intellij-idea - IntelliJ Idea git 弄乱了存储库。如何避免?
- homebrew - 在每次安装中“Apple 无法检查它是否存在恶意软件”
- wordpress - 如何将组权限授予 WordPress
- html - 如何使占位符在选择中始终可见?
- autodesk-forge - 小地图不显示 2D 视图
- python - Scrapy 和网络爬虫
- css - 可滚动的 Inner Div 内部的 ion 内容