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

serverinfo斜杠命令统计频道数包含分类导致结果错误如何修复

问题原因

interaction.guild.channels.cache默认包含服务器内所有类型的频道对象,除了普通的文本、语音频道外,也会包含频道分类、帖子线程、公告频道等类型,你直接取size计数会把分类也统计进去,所以和实际感知到的频道数量不符。

解决方案

统计前对频道列表做过滤,排除类型为分类的项即可,你也可以根据自己的统计需求调整过滤规则。

修改后代码

if (commandName === 'serverinfo') {
  // 过滤掉频道分类,仅统计普通频道
  const countChannels = interaction.guild.channels.cache.filter(channel => channel.type !== 'GUILD_CATEGORY').size;
  let serverinfoembed = new MessageEmbed()
  .setAuthor(`Info for ${interaction.guild.name}`)
  .setColor('#4269f5')
  .addField('Owner', `<@${interaction.guild.ownerId}>`, true)
  .addField('Channels', `${countChannels}`, true)
  return interaction.reply({ embeds: [serverinfoembed] });
};

可选调整规则

  • 仅统计文本频道:修改过滤条件为channel.type === 'GUILD_TEXT'
  • 同时统计文本+语音频道:修改过滤条件为['GUILD_TEXT', 'GUILD_VOICE'].includes(channel.type)
  • 若使用discord.js v14版本,需要引入ChannelType枚举,将过滤条件改为channel.type !== ChannelType.GuildCategory

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 10:36:06