You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 18:01:16