如何用Discord.js v14判断消息频道类型并提取论坛频道ID
Discord.js v14 判断消息频道类型并获取论坛父频道ID
判断频道类型
在Discord.js v14中,可通过频道的type属性区分普通文本频道和论坛帖子频道:
- 普通文本频道类型为
ChannelType.GuildText - 论坛频道下的帖子(线程)类型为
ChannelType.GuildForumThread
代码示例:
import { ChannelType } from 'discord.js'; // 假设message为接收的消息对象 if (message.channel.type === ChannelType.GuildText) { // 消息来自普通文本频道 } else if (message.channel.type === ChannelType.GuildForumThread) { // 消息来自论坛频道的帖子线程 }
获取父论坛频道ID
论坛帖子本质是线程,其对应的父论坛频道ID可通过message.channel.parentId直接获取:
import { ChannelType } from 'discord.js'; if (message.channel.type === ChannelType.GuildForumThread) { const forumChannelId = message.channel.parentId; // forumChannelId即为目标父论坛频道ID }
若需要父频道的完整对象(如频道名称等信息),可使用message.channel.parent,注意添加非空判断:
import { ChannelType } from 'discord.js'; if (message.channel.type === ChannelType.GuildForumThread) { const forumChannel = message.channel.parent; if (forumChannel) { console.log(`父论坛频道名称:${forumChannel.name},ID:${forumChannel.id}`); } }
内容的提问来源于stack exchange,提问作者Blockchain Kid
相关产品推荐
相关产品推荐

