discord.js v12: How do I await for messages in a DM channel?

江枫思渺然 提交于 2021-02-09 09:13:14

问题


This is the code that I've tried:

message.author.dmChannel.awaitMessages(msg => {
    console.log(msg.content)
});

But it returns this error message:

TypeError: Cannot read property 'awaitMessages' of null

Updated Code:

message.author.send("What is your name?")

const filter = m => m.author.id === message.author.id

message.author.dmChannel.awaitMessages(filter)
     .then((collected) => console.log(collected.first().content))

回答1:


You're not using awaitMessages() properly, you need to pass a filter

const filter = (m) => m.author.id === message.author.id
message.author.dmChannel.awaitMessages(filter)
  .then((collected) => console.log(collected.first().content))



回答2:


You should try to create a DM channel first :

let channel = message.author.dmChannel;
if (!channel) channel = await message.author.createDM();

Please note that createDM() returns a Promise, which will require you to switch your command to an async function instead (if it already was not)



来源:https://stackoverflow.com/questions/62616893/discord-js-v12-how-do-i-await-for-messages-in-a-dm-channel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!