按名称将频道添加到类别

2023-11-27

    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");

我正在尝试使文本通道“常规”进入“文本通道”类别

所有解决方案我已经发现依赖于你知道类别 id。我想知道如果有办法我可以获得类别 id,或者仅通过其名称将常规移动到“文本通道”。

注意::目前我正在考虑按照以下方式获取类别 id:

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

然后使用

server.channels.find("name","general").setParent(categoryID);

您可以使用GuildChannel.setParent()。请记住,Discord 会将类别视为频道:CategoryChannel延伸GuildChannel,这样你就可以检查类型GuildChannel.type

要分配现有通道:

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}`);

创建新频道:

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);

编辑:discord.js@v12
唯一改变的是你必须使用GuildChannelManager对于一切。

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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

按名称将频道添加到类别 的相关文章

随机推荐