4

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))
Johonny
  • 151
  • 1
  • 4
  • 17
  • If it's saying can't read property of null, then that means your dmChannel doesn't exist. If you look in the docs, `author.dmChannel` has a null propagation operator, which means that it can be null **or** a DMChannel. You probably need to create one first. Although, it would seem that you've already done that with `message.author.send()`. Interesting. Are you sure your `author.send()` line is working properly and that the author receives the message? – Max Jun 28 '20 at 04:38

2 Answers2

2

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))
Syntle
  • 5,168
  • 3
  • 13
  • 34
  • I've done this: `const filter = m => m.author.id === message.author.id message.author.dmChannel.awaitMessages(filter) .then((collected) => console.log(collected.first().content))` But it still returns the same error message – Johonny Jun 28 '20 at 01:08
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)

Tenclea
  • 1,444
  • 2
  • 10
  • 23
  • Correct me if I'm wrong but before the bot is "awaitingMessages" it is sending a message, doesn't that automatically create the DM channel? – Syntle Jun 29 '20 at 00:01
  • @Syntle Maybe.. I guess the best way to find out is to try – Tenclea Jun 29 '20 at 10:14