最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Add channel to category by name - Stack Overflow

programmeradmin3浏览0评论
    var server = message.guild;
    for (var i = 0; i < server.channels.array().length; i++) {
        server.channels.array()[i].delete();
    }

    server.createChannel("Text Channels", "category");
    server.createChannel('general', "text");

I am trying to make the text channel 'general` go into the category 'Text Channels'

All the solutions I have found rely on you knowing the categories id. I was wondering if there is a way I could get the category id, or else move general into "Text Channels" simply by its name.

NOTE:: Currently I am thinking of something along these lines to get the category id:

var categoryID = server.categories.find("name","Text Channels");

Then to use

server.channels.find("name","general").setParent(categoryID);
    var server = message.guild;
    for (var i = 0; i < server.channels.array().length; i++) {
        server.channels.array()[i].delete();
    }

    server.createChannel("Text Channels", "category");
    server.createChannel('general', "text");

I am trying to make the text channel 'general` go into the category 'Text Channels'

All the solutions I have found rely on you knowing the categories id. I was wondering if there is a way I could get the category id, or else move general into "Text Channels" simply by its name.

NOTE:: Currently I am thinking of something along these lines to get the category id:

var categoryID = server.categories.find("name","Text Channels");

Then to use

server.channels.find("name","general").setParent(categoryID);
Share Improve this question edited Nov 26, 2018 at 15:34 Federico Grandi 6,8065 gold badges33 silver badges51 bronze badges asked Nov 26, 2018 at 10:51 6IU6IU 1352 gold badges2 silver badges7 bronze badges 6
  • And why isn't the mentioned code working? Please give us an proper example of the things that you've tried so I can help you with this. We don't offer a code-writing-service. – Koen Hollander Commented Nov 26, 2018 at 11:11
  • @KoenHollander The code is working, I am merely asking how to move general into text channels. I haven't been able to find anywhere that works. If this is considered spam, are you aware of any sites where I can ask? – 6IU Commented Nov 26, 2018 at 11:14
  • I see, I'm sorry. I've found something that will help, you can set a channel parent: github./discordjs/discord.js/issues/2644 – Koen Hollander Commented Nov 26, 2018 at 11:17
  • @KoenHollander That seems to be working fine, except it only works for channel id's. If you know of any way to get a category id that would be great. I know how to get channel id's, just not categories. Sorry if I am wasting your time – 6IU Commented Nov 26, 2018 at 11:29
  • I don't see a problem, the code at the bottom works perfectly fine for me. – Ethan Mitchell Commented Nov 26, 2018 at 14:37
 |  Show 1 more ment

2 Answers 2

Reset to default 9

You can use GuildChannel.setParent(). Please keep in mind that categories are considered as channels by Discord: CategoryChannel extends GuildChannel, so you can check the type with GuildChannel.type

To assign an existing channel:

let category = server.channels.find(c => c.name == "Text Channels" && c.type == "category"),
  channel = server.channels.find(c => c.name == "general" && c.type == "text");

if (category && channel) channel.setParent(category.id);
else console.error(`One of the channels is missing:\nCategory: ${!!category}\nChannel: ${!!channel}`);

To create a new channel:

server.createChannel("general", "text")
  .then(channel => {
    let category = server.channels.find(c => c.name == "Text Channels" && c.type == "category");

    if (!category) throw new Error("Category channel does not exist");
    channel.setParent(category.id);
  }).catch(console.error);

Edit: discord.js@v12
The only thing that changes is that you have to use the GuildChannelManager for everything.

let category = server.channels.cache.find(c => c.name == "Text Channels" && c.type == "category"),
  channel = server.channels.cache.find(c => c.name == "general" && c.type == "text");

if (category && channel) channel.setParent(category.id);
else console.error(`One of the channels is missing:\nCategory: ${!!category}\nChannel: ${!!channel}`);
server.channels.create("general")
  .then(channel => {
    let category = server.channels.cache.find(c => c.name == "Text Channels" && c.type == "category");

    if (!category) throw new Error("Category channel does not exist");
    channel.setParent(category.id);
  }).catch(console.error);

I know it's too late, but here's the way to get the channel_id :

  • right click on the channel to open the menu
  • click on Copy id (in the bottom of the menu)
发布评论

评论列表(0)

  1. 暂无评论