问题
client.on('message', async (message, user) => {
if(message.content == "!createcategory"){
//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']},
]
}).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)
}).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.author.send("<@" + message.author.id + ">" + ' Jau turi sukurtą kategoriją su kanalais.').then(msg => {
msg.delete({timeout:15000})}
)
}
}
});
I get an error Cannot read property 'setParent' of undefined
I'm trying to add permissions to voice and text channels, but not on category.
Users would have permission to change text and voice channel names, move members, kick them, change their names, mute them, and deaf them.
回答1:
The error is occurring because channel
is undefined
since the function
parent => {
message.guild.channels.create('Text channel', {/* ... */}).catch(console.error)
message.guild.channels.create('Voice channel', {/* ... */}).catch(console.error)
}
returns undefined
(you have not explicitly returned anything). This means that message.guild.channels.create(message.author.username, { ... }).then( ... )
returns a promise that resolves to undefined
.
For more information see MDN's article on using promises.
It looks like you've kept some code from your old question that isn't needed anymore. You don't need to set the parent of the text or voice channel — these are set upon creation.
Your final code should look something like this:
message.guild.channels.create(message.author.username, {type: 'category', /* ... */})
.then(parent =>
Promise.all([
message.guild.channels.create(
'Text channel',
{type: 'text', parent, /* ... */}
),
message.guild.channels.create(
'Voice channel',
{type: 'voice', parent, /* ... */}
)
])
)
.then(() =>
message.author.send("<@" + message.author.id + ">" + ' Jau turi sukurtą kategoriją su kanalais.')
)
.then(msg => msg.delete({timeout:15000}))
// While you didn't handle any errors from the channel category creation in
// your original code, I assume you want to log all errors
.catch(console.error)
Also, you can use string interpolation with template literals to mention someone (see here for more information), so the code for sending the message could be changed to
message.author.send(`${message.author} Jau turi sukurtą kategoriją su kanalais.`)
来源:https://stackoverflow.com/questions/64744565/discord-js-cannot-read-property-setparent-of-undefined-category-create