【问题标题】:Discord.js | TypeError: Cannot read property 'ban' of null不和谐.js | TypeError:无法读取 null 的属性“禁令”
【发布时间】:2021-04-28 23:27:19
【问题描述】:

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

代码:

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. 

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

【问题讨论】:

  • 您的channel.guild.member(executor)executor 的范围是什么?之前您使用过audit.executor。你有一些公共变量executor,它代表用户对象吗?
  • audit.executor.id 用于指定 id 被反通道保护忽略。我想我应该在 channel.guild.member(executor) 中使用 audit.executor.id。 (我对 js 很陌生,刚脱离 lua)

标签: javascript discord discord.js


【解决方案1】:

几件事:

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

【讨论】:

  • 应该是这样吗? ``` bot.on('channelCreate', async channel => { if (!channel.guild) return; const audit = (await channel.guild.fetchAuditLogs() .then( audit => options.limit(1)) ) .catch(console.error) if (audit.action === 'CHANNEL_CREATE') if (audit.executor.id === '409699691408785411') return; (await channel.guild.member(audit.executor.id) .ban({reason: 'aaaaaa'})) channel.delete(); }) ```
【解决方案2】:

您的代码,但有(大部分)来自@Gaben 的建议。

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

我从未使用过Discord.js,这就是为什么我不愿意过多地更改代码。


我刚刚注意到与您的代码相比,您的错误看起来如何,我想我知道问题所在。
错误说,Cannot read property "ban" of null, 我认为这意味着channel.guild.member(executor) 正在返回null。查看discord.js documentation,我可以看到.member(user) 函数如果找到则返回GuildMember,否则返回null
所以据我所知,channel.guild.member(executor) 是空的……看来executor 实际上并不是你想的那样。

【讨论】:

  • 首先,感谢您的回答,我尝试了代码,不幸的是,这不起作用。我将 channel.guild.member(executor).ban 更改为 channel.guild.member(audit.executor.id).ban 但这也不起作用。你对此有什么答案吗?
  • 是的,对不起,我以前从未使用过 discord.js。我基本上只是复制@Gaben 的建议并将它们放入代码形式。
  • @Mengo_ 我刚刚注意到你的错误并改变了我的答案。
猜你喜欢
  • 2021-04-14
  • 2021-01-11
  • 2021-05-18
  • 2023-03-30
  • 2021-04-29
  • 2020-11-10
  • 2020-11-11
  • 2021-04-15
  • 2021-05-10
相关资源
最近更新 更多