您可以使用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);