首页 > 解决方案 > 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);

标签: javascriptnode.jsdiscorddiscord.js

解决方案


这真的很容易做到。你所需要的只是.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());

推荐阅读