【问题标题】:TypeError: Cannot read property 'id' of undefined Discord.jsTypeError:无法读取未定义 Discord.js 的属性“id”
【发布时间】:2021-05-29 04:50:12
【问题描述】:

我正在尝试制作一个票务系统,该系统创建一个频道,然后在该频道中发送一个嵌入。

但我得到了TypeError Cannot read property 'id' of undefined。这是我的代码 sn-p:

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);

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      要么频道不存在,要么你需要使用这个:

      const openedTicket = message.guild.channels.cache.find(r => r.name === `${message.author.username}s-ticket`).id;
      

      最后的id 意味着您不必拥有openedTicket.id 并且使错误捕获更容易。

      另外,您需要将cache 添加到您的client.channels.get(openedTicket)

      => client.channels.cache.get(openedTicket)

      您还应该获取所有频道以确保您的机器人搜索所有频道:

      await message.guild.channels.fetch(); //using await, to return a promise
      client.channels.cache.get(openedTicket);
      

      【讨论】:

      • 嗯,它肯定是用这个确切的名字在这里创建的: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 }cache.find(r => r.name === ${message.author.username}s -ticket).id 你建议的也不起作用:/
      • 我仍然无法读取未定义的属性 'id'
      • 你用的是上面的代码,还是你原来的代码?
      • 上面的代码。我的完整代码是here
      • 我去看看
      猜你喜欢
      • 1970-01-01
      • 2020-04-08
      • 2020-10-01
      • 1970-01-01
      • 2023-01-14
      • 2020-07-14
      • 2019-07-11
      • 2020-08-17
      • 1970-01-01
      相关资源
      最近更新 更多