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

Discord.js:如何将文本频道关联至分类频道

Linking Text Channels to a Category in Your Discord Bot

I'm building a Discord bot to help server owners set up their servers automatically by creating channels and roles. Right now my code creates a category and a text channel separately, but I want the text channel to be nested under the category instead of being independent. Here's my current code:

if (message.content === 'tchannel') { 
  message.guild.channels.create('Important', { 
    type: 'category', 
    permissionOverwrites: [ 
      { id: message.guild.id, allow: ['VIEW_CHANNEL'] } 
    ] 
  }) 
  message.guild.channels.create('Rules', { 
    type: 'text', 
    permissionOverwrites: [ 
      { id: message.guild.id, allow: ['VIEW_CHANNEL'] } 
    ] 
  }) 
  message.channel.send("Channel Created!") 
}

Is there a way to associate the created text channel with the corresponding category?

Discord Channel Layout Example


Absolutely! The key here is that channels.create() returns a Promise, so we can wait for the category to finish being created first, then use its ID (or the category object itself) to set the parent property when making the text channel. This will nest the text channel directly under the category like you want.

Here's your modified code to make this work:

if (message.content === 'tchannel') { 
  // First create the category, then chain the text channel creation
  message.guild.channels.create('Important', { 
    type: 'category', 
    permissionOverwrites: [ 
      { id: message.guild.id, allow: ['VIEW_CHANNEL'] } 
    ] 
  })
  .then(category => {
    // Now create the text channel, linking it to the new category
    return message.guild.channels.create('Rules', { 
      type: 'text', 
      parent: category.id, // This is the line that ties the channel to the category
      permissionOverwrites: [ 
        { id: message.guild.id, allow: ['VIEW_CHANNEL'] } 
      ] 
    });
  })
  .then(() => {
    // Send confirmation once both channels are successfully created
    message.channel.send("Channel Created!");
  })
  .catch(error => {
    // Handle any errors (like missing bot permissions) gracefully
    console.error('Error creating channels:', error);
    message.channel.send("Oops, something went wrong creating the channels.");
  });
}

Quick breakdown of the changes:

  • We use .then() to wait for the category creation to complete before making the text channel – this ensures we have a valid category ID to reference.
  • The parent option accepts either the category's ID string or the full category GuildChannel object; using category.id here is simple and reliable.
  • I added a .catch() block to handle unexpected errors (like if the bot doesn't have permission to manage channels) – this keeps your bot from crashing and gives users feedback.
  • If you want the text channel to inherit permissions directly from the category, you can remove the permissionOverwrites section from the text channel creation entirely.

This should result in the "Rules" text channel appearing nested under the "Important" category exactly as you're aiming for!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 12:57:46