简体   繁体   中英

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

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))

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))

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM