首页 > 解决方案 > 不和谐.js | TypeError:无法读取 null 的属性“禁令”

问题描述

目的:在创建频道时删除频道,然后禁止创建频道的成员。

代码 :

bot.on('channelCreate', async (channel, member) => {
        if (!channel.guild)
            return;
        const audit = (await channel.guild.fetchAuditLogs()).entries.first();
        if (audit.action === 'CHANNEL_CREATE')
            if (audit.executor.id === '833382653779509288')
                return;
            channel.delete();
            channel.guild.member(executor).ban({reason: 'aaaaaa'})
    })`

结果:频道被删除,但用户未被禁止。

这是错误:

(node:8388) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'ban' of null
    at Client.<anonymous> (C:\Users\Utilisateur\Desktop\discordbot4\main.js:30:49)
    at processTicksAndRejections (internal/process/task_queues.js:82:5)
(node:8388) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by 
rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:8388) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 

有人可以帮我解决这个错误吗?

标签: javascriptdiscorddiscord.js

解决方案


一些事情:

  1. 您正在删除频道,然后尝试访问其guild.member. 以相反的顺序进行。
  2. User.ban()返回一个Promise对象,因此您应该await得到它的结果。(文档
  3. bot.on('channelCreate', 没有任何member参数,只有channel( docs )
  4. fetchAuditLogs()您可以使用,options例如options.limit设置为 1。这样,您将不需要该.first()方法(并且应该快一点)。(文档

推荐阅读