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
解决方案
这是因为您在尝试发送消息之前没有等待创建通道。.setParent()
与在使用and之前等待它准备好一样.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)
})
推荐阅读
- python - 如何在 Python 中将 CTRL-P 发送到 Web 浏览器
- c# - 如何使用 iText 7 设置卢布符号?
- jenkins - 从上游 Jenkins 管道作业调用多个下游作业
- javascript - 用jquery对html表进行排序
- gis - 如何从geoserver请求WFS服务并将geojson数据加载为铯中的一层?
- c# - 如何使用 Automapper 展平 C# 中重复使用的子对象
- python - 是否可以将双矩阵列转换为列表或配对?
- apt - 源编译或使用apt有什么区别?
- mysql - 获取从现在起 3 天后到期的行
- php - MYSQL 只选择列的一部分