无法将消息发送到刚创建的频道

时间:2020-05-24 13:06:48

标签: discord.js

我试图在创建消息后立即向其发送消息,但该消息似乎无效。我正在使用discord.js@v12

这是代码:

message.guild.channels.create(cpl, 'text').then(ma => {
  ma.setParent(cat);
  ma.lockPermissions();
}).catch(err => console.log(err))

let nChannel = bot.channels.cache.find(ch => ch.name === cpl)
console.log(nChannel)
let embed = new Discord.MessageEmbed()
  .setTitle("New Ticket")

nChannel.send(embed)

这是记录到控制台的内容:

Cannot read property 'send' of undefined

1 个答案:

答案 0 :(得分:0)

这是因为您在尝试发送消息之前没有等待创建通道。与使用.setParent().lockPermissions()之前等待其准备就绪的方式相同,应该使用.send()之前等待。
这是我的处理方式:

message.guild.channels.create(cpl, 'text').then(async (newChannel) => {
  // You're inside .then, so all of this is happening AFTER the channel is created

  // Wait for .setParent and .lockPermissions to fulfill
  await newChannel.setParent(cat)
  await newChannel.lockPermissions()

  // You can now send your message
  let embed = new Discord.MessageEmbed()
    .setTitle("New Ticket")
  newChannel.send(embed)
})