javascript - 不和谐.js | 无法读取未定义的属性“setParent”| 类别创建
问题描述
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})}
)
}
}
});
我收到一个错误Cannot read property 'setParent' of undefined
,我正在尝试向语音和文本频道添加权限,但不在类别中。用户将有权更改文本和语音频道名称、移动成员、踢他们、更改他们的名字、静音和聋。
解决方案
发生错误是channel
因为undefined
该功能
parent => {
message.guild.channels.create('Text channel', {/* ... */}).catch(console.error)
message.guild.channels.create('Voice channel', {/* ... */}).catch(console.error)
}
返回undefined
(您没有明确返回任何内容)。这意味着message.guild.channels.create(message.author.username, { ... }).then( ... )
返回一个解析为undefined
.
有关更多信息,请参阅MDN 关于使用 Promise 的文章。
看起来您从旧问题中保留了一些不再需要的代码。您不需要设置文本或语音通道的父级 - 这些是在创建时设置的。
您的最终代码应如下所示:
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)
此外,您可以使用带有模板文字的字符串插值来提及某人(有关更多信息,请参见此处),因此发送消息的代码可以更改为
message.author.send(`${message.author} Jau turi sukurtą kategoriją su kanalais.`)
推荐阅读
- reactjs - firebase 通知事件在服务工作者中不起作用(notifcationclose,notificationclick)
- haskell - 使用 Haskell 对列表的元素进行分组
- python - 有没有办法使用 python 中的 schedule 模块请求输入?
- c++ - c++ make_shared<> 与二维数组
- azure - 将 Docker 容器连接到与 Azure Pipelines 中的当前容器相同的网络
- javascript - Webpack:包含 html 文件及其 js 脚本以及独立的 js 脚本
- python - 在 Pandas DataFrame 上对迭代函数进行矢量化
- android - android AR核心抛出模拟器不支持的设备
- http - MIT APP INVENTOR 中的 CURL 请求执行
- command-line - gnuplot 命令行选项