【问题标题】:ID Members UndefinedID 成员未定义
【发布时间】:2021-03-10 11:50:11
【问题描述】:

好的,我知道我之前在这里,但现在我搞砸了一些东西,试图添加另一个东西。基本上,我在控制台中收到一个未定义的错误,因为我不确定如何在 Discord.JS 的代码中编写它

代码: const id = message.id.members.first();

控制台错误:

(node:16412) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'first' of undefined
    at Object.execute (C:\Users\Saman\Desktop\gban-bot\commands\test.js:12:39)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:16412) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:16412) [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.```

【问题讨论】:

    标签: discord.js


    【解决方案1】:

    您似乎对自己想要做什么有点困惑。 message 中有两件事包含某种形式的成员。

    发送消息的member 始终存在。你可以访问它,以及对应的ID,像这样:

    let memberID = message.member.id;
    

    然后是在消息中被标记的member。显然,它并不总是存在,因此您可能需要为此添加额外的检查。你可以这样访问它:

    let memberID = message.mentions.members.first().id;
    

    【讨论】:

    • 好吧,我正在寻找一种通过 ID 禁止的方法,我知道这是一个简单的命令,但由于某种原因,我无法弄清楚如何通过 ID 禁止它而不是提及.
    • 你需要先通过ID找到成员对象,因为你只能禁止一个成员。所以:ID -> 会员 -> 禁止。请参阅 here 了解如何禁止某人。
    【解决方案2】:

    message.id.members.first() 不是一个东西。 message.id 返回雪花。即已发送消息的 ID。 message.member.id 还返回雪花/消息成员的 id。在您的情况下,如果您想从消息中的提及中获取成员对象本身,那么您正在寻找mentions。所以它是message.mentions.members.first() 并且会返回GuildMember 对象本身。使用它,您可以访问 GuildMember 的属性。

    编辑

    好吧,我正在寻找一种通过 ID 禁止的方法,我知道这是一个简单的命令,但由于某种原因,我无法弄清楚如何通过 ID 禁止它而不是提及。

    为了通过 id 禁止他们,首先你需要找到 GuildMember。您可以通过 3 种方式找到会员。 find()get() 使用 GuildMemberManager 的集合。或者您可以直接使用GuildMemberManager 使用fetch()(如果该成员不在集合中)。通过这样做,您将获得GuildMember 对象。你可以通过 <GuildMember>.ban()

    禁止他

    get()fetch() 需要雪花。即 ID 本身作为字符串。而您需要在find() 中提供一个函数。如果您仍有任何疑问,我会将您链接到these methods 的 djs 文档。

    【讨论】:

    • 好吧,我正在寻找一种通过 ID 禁止的方法,我知道这是一个简单的命令,但由于某种原因,我无法弄清楚如何通过 ID 禁止它而不是提及.
    • 我只是对答案做了一些调整。请检查。
    猜你喜欢
    • 2020-09-18
    • 2021-03-04
    • 2022-01-08
    • 1970-01-01
    • 2020-09-19
    • 2021-11-26
    • 2020-09-08
    • 2021-05-24
    • 2018-03-04
    相关资源
    最近更新 更多