javascript - TypeError:无法读取未定义 Discord.js 的属性“id”
问题描述
我正在尝试制作一个票务系统,该系统创建一个频道,然后在该频道中发送一个嵌入。
但我得到TypeError Cannot read property 'id' of undefined
. 这是我的代码片段:
const openedTicket = message.guild.channels.cache.find((r) => r.name === `${message.author.username}s-ticket`);
const openedEmbed = new Discord.MessageEmbed().setDescription("Support will be with you shortly." + "To close this ticket react with :lock:");
setTimeout(function () {
client.channels
.get(openedTicket.id)
.send(openedEmbed)
.then((msg) => {
msg.react("");
});
}, 1000);
解决方案
这真的很容易做到。你所需要的只是.then()
继续前进。所以在你的情况下,这将是:
message.guild.channels.create(`${message.author.username}s-ticket`, {
type: 'text',
permissionOverwrites: [
{
allow: 'VIEW_CHANNEL',
id: message.author.id
},
{
deny: 'VIEW_CHANNEL',
id: message.guild.id
}
]
}).then(channel => {
const openedEmbed = new Discord.MessageEmbed().setDescription("Support will be with you shortly." + "To close this ticket react with :lock:");
setTimeout(function () {
channel.send(openedEmbed)
.then((msg) => {
msg.react("");
});
}, 1000);
})
你的反应已经有了正确的解决方案。
编辑:
您的原始代码为空,因为您的名字中很可能有一些大写字母,但是所有不和谐的文本通道都是小写的,并且空格替换为破折号。因此,当您寻找频道时,您需要将所有这些因素纳入您的搜索。所以你的openedTicket
常数应该是这样的
const openedTicket = message.guild.channels.cache.find(r => r.name === `${message.author.username}s-ticket`.replace(" ", "-").toLowerCase());
推荐阅读
- c# - 在 UWP 中替换 System.Diagnostics.StackFrame.GetMethod
- visual-studio-code - 如何为 Visual Studio 代码扩展创建文件?
- python - 在混合整数线性规划中结合简单的 if 语句
- javascript - 使用请求承诺移动到新页面的最佳方法?
- graphviz - 如何设置图形填充区域?
- mysql - 使用kafka插入表后读取数据
- excel - Powershell在excel中将选定范围保存为html
- oracle - 如何在过程中调用函数?
- dom - Xamarin.Android 如何使用 Dcsoup Nuget 插件获取 Google Play 商店应用程序版本号?
- angular - 带有 mat-autocomplete 的 cdk-virtual-scroll 工作不规则