discord.js v12:如何等待DM通道中的消息?

时间:2020-06-28 00:22:46

标签: javascript node.js discord discord.js chatbot

这是我尝试过的代码:

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

但是它返回此错误消息:

TypeError: Cannot read property 'awaitMessages' of null

更新的代码:

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

2 个答案:

答案 0 :(得分:1)

您没有正确使用awaitMessages(),需要通过filter

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

答案 1 :(得分:0)

您应该首先尝试创建DM频道:

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

请注意,createDM()返回一个Promise,这将要求您将命令切换到异步函数(如果还没有的话)