Referencing a DM channel

时间:2018-12-03 13:05:55

标签: javascript node.js discord discord.js

I'm trying to create a command that allows users to create their password following prompts through DM. I'm able to send a message to the user, but not able to read a message sent back with a MessageCollector because I cannot find a way to reference the DM channel.

I have tried using another instance of bot.on("message", message) here, but that creates a leak in the system causing the second instance to never disappear.

I also can't let users use a command say !CreatePassword *** because this function is linked with many others in a strict order.

Maybe I'm doing something fundamentally wrong, or approaching the problem in a bad way, but I need a way to reference a DM channel.

This is the best iteration of my code so far.

function createAccount(receivedMessage, embedMessage)
{
    const chan = new Discord.DMChannel(bot, receivedMessage.author);

    const msgCollector = new Discord.MessageCollector(chan , m => m.author.id == receivedMessage.author.id);
    msgCollector.on("collect", (message) => 
    {
        // Other Code
        msgCollector.stop();
        // Removing an embed message on the server, which doesn't have a problem.
        embedMessage.delete();
    })
}

I can show the rest of my code if necessary.

Thank you for your time. I've lost an entire night of sleep over this.

1 个答案:

答案 0 :(得分:0)

我会这样做(我假设receivedMessage是触发命令的消息,如果我错了,请纠正我)

async function createAccount(receivedMessage, embedMessage) {
  // first send a message to the user, so that you're sure that the DM channel exists.
  let firstMsg = await receivedMessage.author.send("Type your password here");

  let filter = () => true; // you don't need it, since it's a DM.
  let collected = await firstMsg.channel.awaitMessages(filter, {
      maxMatches: 1, // you only need one message
      time: 60000 // the time you want it to run for
    }).catch(console.log);

  if (collected && collected.size > 0) {
    let password = collected.first().content.split(' ')[0]; // grab the password
    collected.forEach(msg => msg.delete()); // delete every collected message (and so the password)
    await firstMsg.edit("Password saved!"); // edit the first message you sent
  } else await firstMsg.edit("Command timed out :("); // no message has been received

  firstMsg.delete(30000); // delete it after 30 seconds
}