简体   繁体   English

类型错误:无法读取未定义 Discord.js 的属性“id”

[英]TypeError: Cannot read property 'id' of undefined Discord.js

I am trying to make a ticket system that creates a channel then sends an embed in that channel.我正在尝试制作一个票务系统,该系统创建一个频道,然后在该频道中发送一个嵌入。

But I get the TypeError Cannot read property 'id' of undefined .但是我得到了TypeError Cannot read property 'id' of undefined This is my code snippet:这是我的代码片段:

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

This is really easy to do.这真的很容易做到。 All you need is a .then() to move on.您只需要一个.then()即可继续前进。 So in your case that would be:所以在你的情况下,这将是:

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

You had the right solution with your reaction already.你的反应已经有了正确的解决方案。

EDIT:编辑:

Your original code comes up empty because you very likely have some uppercase letters in your name however all discord text channels are all lowercase and spaces are replaces with dashes.您的原始代码为空,因为您的名字中很可能有一些大写字母,但是所有 discord 文本通道都是小写字母,并且空格替换为破折号。 So when you look for a channel you need to factor all that into your search.因此,当您寻找频道时,您需要将所有这些因素纳入您的搜索。 So your openedTicket constant should look like this所以你的openedTicket常量应该是这样的

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

Either the channel doesnt exist, or you need to use this:要么频道不存在,要么你需要使用这个:

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

The id at the end means you dont have to have openedTicket.id and makes error trapping much easier.最后的id意味着您不必openedTicket.id并使错误捕获更容易。

Also, you need to add a cache to your client.channels.get(openedTicket)此外,您需要向您的client.channels.get(openedTicket)添加cache

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

You should also fetch all channels to ensure that your bot searches them all:您还应该获取所有频道以确保您的机器人搜索所有频道:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “TypeError:无法读取未定义的属性‘id’”Discord.js 错误 - "TypeError: Cannot read property 'id' of undefined" Discord.js error TypeError:无法读取 Discord.js 中未定义的属性“id” - TypeError: Cannot read property 'id' of undefined in Discord.js Discord.js TypeError:无法读取未定义的属性“id”-channel.id - Discord.js TypeError: Cannot read property 'id' of undefined - channel.id discord.js错误TypeError:无法读取未定义的属性“ voiceChannel” - discord.js Error TypeError: Cannot read property 'voiceChannel' of undefined Discord.JS:TypeError:无法读取未定义的属性“角色” - Discord.JS:TypeError: Cannot read property 'roles' of undefined discord.js TypeError:无法读取未定义的属性“名称” - discord.js TypeError: Cannot read property 'name' of undefined “ TypeError:无法读取未定义的属性'push'” discord.js - “TypeError: Cannot read property 'push' of undefined” discord.js 类型错误:无法使用 Discord.js 读取未定义的属性“集” - TypeError: Cannot read property 'set' of undefined with Discord.js 类型错误:无法读取未定义的 Discord.js 错误的属性“get” - TypeError: Cannot read property 'get' of undefined Discord.js Error 类型错误:无法读取未定义的属性“成员” - Discord.JS - TypeError: Cannot read property 'members' of undefined - Discord.JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM