类型错误:无法读取 null 的属性“ban” | Discord.js

时间:2021-01-08 21:52:27

标签: discord discord.js

目的:禁止未经授权的用户将会员踢出我的服务器。

代码:

client.on("guildMemberRemove", async member => {

    const FetchingLogs = await member.guild.fetchAuditLogs({
        limit: 1,
        type: "MEMBER_KICK",
    });

    const kickLog = FetchingLogs.entries.first();

    if (!kickLog) {
        return console.log(red(`${member.user.tag} was kicked in ${member.guild.name} but nothing was registered in the audit log...`));
    }

    const { executor, target, createdAt } = kickLog

    if (target.id === member.id) {
        console.log(greenBright(`${member.user.tag} got kicked in ${member.guild.name}, by ${executor.tag}`));
    } else if (target.id === executor.id) {
        return
    }

    if (executor.id !== client.user.id) {
        member.guild.member(executor).ban({
            reason: `Unauthorised Kick`
        }).then(member.guild.owner.send(`**Unauthorised Kick By:** ${executor.tag} \n**Victim:** ${target.tag} \n**Time:** ${createdAt.toDateString()} \n**Sentence:** Ban.`)).catch();
    }

})

结果: 它禁止了执行程序,但它仍然抛出这个错误:

(node:10272) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'ban' of null

能否请您告诉我为什么会发生这种情况以及我可以如何消除此错误。感谢所有帮助;)

2 个答案:

答案 0 :(得分:0)

这是违规行:

result = list()
for row in df.iterrows():
    cur_data = row[1]
    result.append((((cur_data['Submit_Date'] < df['Submit_Date']) & (df['Submit_Date']< cur_data['Resolved_Date']))
                  | ((cur_data['Submit_Date'] < df['Resolved_Date']) & (df['Resolved_Date'] < cur_data['Resolved_Date']))).sum())
df['count'] = result


         Submit_Date       Resolved_Date    count
1   2016-10-01 23:41:00 2016-10-02 02:27:00 2
2   2016-10-01 23:50:00 2017-03-09 19:39:00 3
3   2016-10-02 00:05:00 2016-11-15 12:46:00 2
4   2016-10-03 05:17:00 2016-11-14 17:37:00 0

您为 member.guild.member 提供了一个执行程序对象,它返回 null,然后它尝试对 null 对象调用函数 ban 并且您得到错误。

也许尝试将 executor.id 发送给它,如下所示:

member.guild.member(executor).ban(....

答案 1 :(得分:0)

如果你想禁止某人加入公会,你可以写:

member.guild.members.ban(executor.id, { reason: "/* Your reason */" })...

来源: