简体   繁体   中英

AwaitMessages in DM is not working Discord.js

I am trying to run a AwaitMessage function inside the user's DM's, but its not working. Can anyone help?

Error: TypeError: Cannot read property 'awaitMessages' of undefined

const discord = require('discord.js')

module.exports = {
  name: "team",
  description: "You can create a new team",
  usage: "team",
  category: "info",
  run: (client, message) => {

      const member = message.member; 
      const user = message.author;

      member.send("Do you want continue ? (Yes/No)");
      message.dmChannel.awaitMessages(user == message.author, {max: 1 ,  time: 40000})
      .then (collect => {
        if (collected.first().content.toLowerCase() == 'yes') {
       member.send("Test") 
       } else {
       member.send("Test")
       }
       });
  }
}```

Firstly, I'd recommend using async/await for more than one awaitMessages, as the code can get very nest-y and hard to look at.

Instead of

message.dmChannel.awaitMessages(user == message.author, { max: 1 ,  time: 40000 })

Use

let collected = await message.dmChannel.awaitMessages(filter, { max: 1, time: 400000 });

If you're trying to only allow the user who triggered the command to pass the filter, just match IDs like this:

let filter = (m) => { m.author.id == message.author.id; }

And lastly, in case a user fails to reply, simply use a try/catch block, like this

let filter = (m) => {
  m.author.id == message.author.id;
};
try {
  let collected = await message.dmChannel.awaitMessages(filter, {
    max: 1,
    time: 400000,
  });
  let response = collected.first().content.toLowerCase();
  if (response == "yes") {
    member.send("Test");
  } else {
    member.send("Test");
  }
} catch (e) {
  return message.reply("Looks like you took too long to reply!");
}

I hope this can serve you.

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