TypeError:无法读取属性';id';未定义的| Discord.js

TypeError:无法读取属性';id';未定义的| Discord.js,discord,discord.js,Discord,Discord.js,目标:禁止每次在审核日志中发现试图创建频道的用户 代码: // Channel Create client.on("channelCreate", async (channel) => { const FetchingLogs = await client.guilds.cache.get(channel.guild.id).fetchAuditLogs({ limit: 1, type: "CHANNEL_CREAT

目标:禁止每次在审核日志中发现试图创建频道的用户

代码:

// Channel Create
client.on("channelCreate", async (channel) => {

    const FetchingLogs = await client.guilds.cache.get(channel.guild.id).fetchAuditLogs({
        limit: 1,
        type: "CHANNEL_CREATE",
    })

    const ChannelLog = FetchingLogs.entries.first();

    if (!ChannelLog) {
        return console.log(red(`CHANNEL: ${channel.id} was created.`));
    }

    const { executor, target, createdAt } = ChannelLog;

    if (target.id === channel.id) {
        console.log(greenBright(`${channel.id} got created, by ${executor.tag}`));
    } else if (target.id === executor.id) {
        return
    }

    if (executor.id !== client.user.id) {
        channel.guild.member(executor.id).ban({
            reason: `Unauthorised Channel Created`
        }).then(channel.guild.owner.send(`**Unauthorised Channel Created By:** ${executor.tag} \n**Channel ID:** ${channel.id} \n**Time:** ${createdAt.toDateString()} \n**Sentence:** Ban.`)).catch();
    }
});
TypeError: Cannot read property 'id' of undefined
结果:

// Channel Create
client.on("channelCreate", async (channel) => {

    const FetchingLogs = await client.guilds.cache.get(channel.guild.id).fetchAuditLogs({
        limit: 1,
        type: "CHANNEL_CREATE",
    })

    const ChannelLog = FetchingLogs.entries.first();

    if (!ChannelLog) {
        return console.log(red(`CHANNEL: ${channel.id} was created.`));
    }

    const { executor, target, createdAt } = ChannelLog;

    if (target.id === channel.id) {
        console.log(greenBright(`${channel.id} got created, by ${executor.tag}`));
    } else if (target.id === executor.id) {
        return
    }

    if (executor.id !== client.user.id) {
        channel.guild.member(executor.id).ban({
            reason: `Unauthorised Channel Created`
        }).then(channel.guild.owner.send(`**Unauthorised Channel Created By:** ${executor.tag} \n**Channel ID:** ${channel.id} \n**Time:** ${createdAt.toDateString()} \n**Sentence:** Ban.`)).catch();
    }
});
TypeError: Cannot read property 'id' of undefined
它成功地禁止用户,但仍然抛出一个错误

错误:

// Channel Create
client.on("channelCreate", async (channel) => {

    const FetchingLogs = await client.guilds.cache.get(channel.guild.id).fetchAuditLogs({
        limit: 1,
        type: "CHANNEL_CREATE",
    })

    const ChannelLog = FetchingLogs.entries.first();

    if (!ChannelLog) {
        return console.log(red(`CHANNEL: ${channel.id} was created.`));
    }

    const { executor, target, createdAt } = ChannelLog;

    if (target.id === channel.id) {
        console.log(greenBright(`${channel.id} got created, by ${executor.tag}`));
    } else if (target.id === executor.id) {
        return
    }

    if (executor.id !== client.user.id) {
        channel.guild.member(executor.id).ban({
            reason: `Unauthorised Channel Created`
        }).then(channel.guild.owner.send(`**Unauthorised Channel Created By:** ${executor.tag} \n**Channel ID:** ${channel.id} \n**Time:** ${createdAt.toDateString()} \n**Sentence:** Ban.`)).catch();
    }
});
TypeError: Cannot read property 'id' of undefined
代码:指定错误|参考channel.guild.id

    const FetchingLogs = await client.guilds.cache.get(channel.guild.id).fetchAuditLogs({
        limit: 1,
        type: "CHANNEL_CREATE",
    })
我猜原因是参数
channel
,类型是
DMChannel
,而不是
GuildChannel


是否有办法解决此问题或将参数类型更改为
GuildChannel
???我已经检查过了,我似乎找不到任何迹象表明这是可能的。感谢您的帮助;)

您对发生此错误原因的假设是正确的。
channelCreate
事件确实处理了
GuildChannel
DMChannel
的创建,并且您的代码在禁止用户后向帮会所有者发送DM。这就是为什么禁令有效,但之后你会犯错误;因为DM与所有者创建了一个
DMChannel
,再次触发事件处理程序,但由于DM没有公会,因此
channel.guild
未定义

因此,让我再次说明这个问题。您遇到错误
无法读取未定义的
的属性'id',您已经知道这意味着
频道。帮会
未定义的
。您不希望出现错误,因此不希望
channel.guild
处于
未定义状态。但你的问题是:

有没有办法解决这个问题,或者可以将参数类型更改为GuildChannel

这不是你想采取的方法。这样做意味着用户将被禁止管理您的bot,因为它将触发您的
channelCreate
事件处理程序;此外,机器人会尝试禁止公会所有者,因为它会向其发送DM。你只想禁止用户在公会中创建频道

当我们这样解决问题时,解决方案很简单:检查频道是否为
DMChannel
,如果是,则停止。仅允许用户因创建
GuildChannel
而被禁止。那你是怎么做到的?嗯,正如您已经从错误中看到的,
channel.guild
在频道为
DMChannel
时是
未定义的。因此,只需检查此情况,如果是,则返回
。下面是一个例子:

// Channel Create
client.on("channelCreate", async (channel) => {

    if (!channel.guild) return; //<- added this

    const FetchingLogs = await client.guilds.cache.get(channel.guild.id).fetchAuditLogs({
        limit: 1,
        type: "CHANNEL_CREATE",
    })

    const ChannelLog = FetchingLogs.entries.first();

    if (!ChannelLog) {
        return console.log(red(`CHANNEL: ${channel.id} was created.`));
    }

    const { executor, target, createdAt } = ChannelLog;

    if (target.id === channel.id) {
        console.log(greenBright(`${channel.id} got created, by ${executor.tag}`));
    } else if (target.id === executor.id) {
        return
    }

    if (executor.id !== client.user.id) {
        channel.guild.member(executor.id).ban({
            reason: `Unauthorised Channel Created`
        }).then(channel.guild.owner.send(`**Unauthorised Channel Created By:** ${executor.tag} \n**Channel ID:** ${channel.id} \n**Time:** ${createdAt.toDateString()} \n**Sentence:** Ban.`)).catch();
    }
});
//频道创建
client.on(“channelCreate”,异步(通道)=>{

如果(!channel.guild)return;//文档不明确,但是,如果频道是DM,
channel
可能是
DMChannel
,反之则是
GuildChannel
。我相信DMs不是特定于公会的;你只需与某人至少在一个公会中,就可以与他们一起打开DM。因此,存在
gu是没有意义的DM对象的ild
属性。解释得很好|非常感谢。