问题
client.on('ready', () => {
command(client, 'createcategory', (message) => {
const name = message.content.replace('!createcategory ', '')
if(message.guild.channels.cache.find(c => c.name == message.author.username && c.type == "category") === undefined){
message.guild.channels.create(message.author.username, {type: 'category', permissionOverwrites: [
{
id: message.guild.id,
deny: ['VIEW_CHANNEL'],
},
{
id: message.author.id,
allow: ['VIEW_CHANNEL'],
},
]})
message.guild.channels.create('Text channel', {type: 'text', permissionOverwrites: [
{
id: message.guild.id,
deny: ['VIEW_CHANNEL'],
},
{
id: message.author.id,
allow: ['VIEW_CHANNEL'],
},
]}).then(channel => {
let category = message.guild.channels.cache.find(c => c.name == message.author.username && c.type == "category");
if (!category) throw new Error("Category channel does not exist");
channel.setParent(category.id);
}).catch(console.error);
message.guild.channels.create('Voice channel', {type: 'voice', permissionOverwrites: [
{
id: message.guild.id,
deny: ['VIEW_CHANNEL'],
},
{
id: message.author.id,
allow: ['VIEW_CHANNEL'],
},
]}).then(channel => {
let category = message.guild.channels.cache.find(c => c.name == message.author.username && c.type == "category");
if (!category) throw new Error("Category channel does not exist");
channel.setParent(category.id);
}).catch(console.error);
} else {message.send('Jau tu turi kanala, kurviuk tu')}
});
});
The code used to work, but somehow I forgot what I did with it and didn't code for 2 months or so...
The function should be - when you write !createcategory
it should create a category with voice and text channels in it. The category should be named by your user name. There is no error in the console, please help, thanks!
回答1:
I'm not sure what's causing your issue, but try setting the parent of the text and voice channels when you create the channel:
// GuildChannelManager#create returns the channel you created
message.guild.channels.create(message.author.username, {
type: 'category',
permissionOverwrites: [
{id: message.guild.id, deny: ['VIEW_CHANNEL']},
{id: message.author.id, allow: ['VIEW_CHANNEL']},
]
}).then(parent => {
// Create the text channel
message.guild.channels.create('Text channel', {
type: 'text',
// under the parent category
parent, // shorthand for parent: parent
permissionOverwrites: [
{id: message.guild.id, deny: ['VIEW_CHANNEL']},
{id: message.author.id, allow: ['VIEW_CHANNEL']},
]
}).catch(console.error)
// Same with the voice channel
message.guild.channels.create('Voice channel', {
type: 'voice',
parent,
permissionOverwrites: [
{id: message.guild.id, deny: ['VIEW_CHANNEL']},
{id: message.author.id, allow: ['VIEW_CHANNEL']},
]
}).catch(console.error)
})
You could also use ES2017's async/await:
// Must be an async function vvvvv
command(client, 'createcategory', async (message) => {
// ...
const parent = await message.guild.channels.create(/* ... */)
try {
// Run the promises concurrently, like in your code
await Promise.all([
message.guild.channels.create('Text channel', {/* ... */})
message.guild.channels.create('Voice channel', {/* ... */)
])
} catch (error) {
console.error(error)
}
// ...
})
来源:https://stackoverflow.com/questions/64525395/createcategory-doesnt-create-category-what-to-do-discord-js