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

discord.js V13 获取Guild中分类频道数量显示为0如何修复

问题原因

  1. 频道类型判断规则不匹配你使用的discord.js版本
    • discord.js v12 及更早版本中,分类频道的type字段值为字符串category
    • discord.js v13 及以上版本对频道类型做了重构,全部改为枚举值,分类频道对应的枚举为ChannelType.GuildCategory,值为数字4,你用字符串"category"过滤自然匹配不到任何频道,因此返回结果恒为0
  2. 额外小问题:你代码中setThumbnail的参数拼写错误,dynaic应为dynamic,会导致服务器图标无法正常显示
  3. 少数情况是机器人启动时未开启Guilds网关意图,导致无法正常拉取服务器频道列表

修复方案

  1. 确认你使用的discord.js版本,调整分类频道过滤规则
  2. 检查并开启必要的网关意图
  3. 修正参数拼写错误

修复后的代码示例(适配discord.js v13+版本)

// 先从discord.js导入ChannelType枚举
const { ChannelType, MessageEmbed } = require('discord.js');

const channels = message.guild.channels.cache;

const embed = new MessageEmbed()
.setAuthor(message.guild.name, message.guild.iconURL({dynamic: true}))
// 修正拼写错误
.setThumbnail(message.guild.iconURL({dynamic: true}))
.addField(`Owner`, `${owner.user.tag}`,true)
// 改用ChannelType枚举判断分类频道
.addField(`Categorys`, `${channels.filter(channel => channel.type === ChannelType.GuildCategory).size}`,true)

不想导入枚举的简化写法

.addField(`Categorys`, `${channels.filter(channel => channel.type === 4).size}`,true)

网关意图配置检查(机器人登录逻辑处调整)

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
  intents: [
    // 必须开启Guilds意图才能获取服务器、频道相关数据
    GatewayIntentBits.Guilds,
    // 你用到的其他业务相关意图,比如消息类功能可追加GuildMessages、MessageContent等
  ]
});

内容的提问来源于stack exchange,提问作者Nicky

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 02:06:05