discord.js-commando 中等待的命令

Awaited commands in discord.js-commando

我正在努力让这一切发生。假设您键入“!command”,它会向您发送 DM,或者如果 DM 被禁用,则会向您发送错误消息。

如果他们不给您发消息,请提问。你回答它,它 returns 你是如何回答的。所以我现在正在尝试做一些简单的事情:

async run(msg) {
  const messages = [];
  try {
    messages.push(await msg.direct('This is further instruction. In order to continue type "ok" without the quotes. This process will expire after 30 seconds.')
      .then(() => {
        msg.channel.awaitMessages(response => response.content === 'ok', {
          max: 1,
          time: 30000,
          errors: ['time'],
        })
          .then((collected) => {
            msg.channel.send('The collected message was:' + collected);
          })
          .catch(() => {
            msg.channel.send('There was no content collected.');
          });
      })
  );
    if (msg.channel.type !== 'dm') messages.push(await msg.reply('I have sent you a DM with further instructions.'));
  } catch (err) {
    messages.push(await msg.reply('Unable to send you a DM, you most likely have them disabled.'));
  }

  return messages;
}

我在启用 DM 的情况下执行了命令,但它发送的是没有 DM 你的错误消息,但它还是发送了 DM。然后我尝试在 DM 中键入 "ok",它 returns 和错误,这不是一个有效的命令。我不确定这里出了什么问题。我想这可能是我的语法问题,但我看不出问题。

编辑:所以我已经让它工作了,在命令执行的通道内。但是我需要它在 DM 内做它的事情。我绝对需要它这样做,因为在所述 DM 中会有敏感信息处理。

  messages.push(await
  msg.direct('This is further instruction. In order to continue type "ok" without the quotes. This process will expire after 30 seconds.')
  .then(() => {
    msg.channel.awaitMessages(response => response.content === 'ok', {
      max: 1,
      time: 30000,
      errors: ['time'],
    })
    .then((collected) => {
      msg.channel.send('The collected message was:' + collected.first().content);
    })
    .catch(() => {
      msg.channel.send('There was no content collected.');
    });
  })
);

好的,我现在已经让它进入​​ dms,但是...我似乎无法获取不包括机器人响应的内容:

  async run(msg) {
    const messages = [];
    try {
      msg.author.send('What do you want your username to be?')
        .then(() => {
          msg.author.dmChannel.awaitMessages(response => response.content && !msg.author.bot, {
            max: 1,
            time: 30000,
            errors: ['time'],
          })
          .then((collected) => {
            var username = collected.first().content;
            msg.author.send('Message sent: ' + collected.first().content);
            colllected.content.delete();
            console.log('Hmmm');
            msg.author.send('What do you want your email to be?')
            .then(() => {
              msg.author.dmChannel.awaitMessages(response => response.content, {
                max: 1,
                time: 30000,
                errors: ['time'],
              })
              .then((collected) => {
                var email = collected.first().content;
                msg.author.send('Email: ' + collected.first.content);
                msg.author.send('```Username: ' + username + '\n Email: ' + email + '```');
              })
              .catch((err) => {
                msg.author.send('No content sent, killing process.');
              });
          })
          .catch((err) => {
            msg.author.send('No content was added. Killing process.');
          });
        })
      })
      if (msg.channel.type !== 'dm') messages.push(await msg.reply('I have sent you a DM with further instructions.'));
    } catch (err) {
      messages.push(await msg.reply('Unable to send you a DM, you most likely have them disabled.'));
    }

    return messages;
  }

所以,我找到了答案。它可能不是最有效的,但对我来说效果很好。所以这里是:

async run(msg) {
    const messages = [];
    var username = null;
    var userId = msg.author.id;
    var email = null;
    var password = null;
    var blockProcess = 0;
    try {
      if (blockProcess === 0) {
        await msg.direct('You are about to register. You will have 10 seconds to enter each process. You will need to have your username, email, and password ready! Type **`ok`** to continue.');
        await msg.author.dmChannel.awaitMessages(res  => {
          if(!res.author.bot) {
            if (res.content === 'ok') {
              return blockProcess = 1;
            } else {
              console.log(res.content);
              console.log(blockProcess);
              return msg.direct('You did not produce the correct answer.');
            }
          }
        }, { max: 1, time: 10000, errors: ['time'] })
        .catch(() => {
          msg.direct('You have ran out of time!');
        });
      }

      if (blockProcess === 1) {
        await msg.direct('What do you wish your username to be?');
        await msg.author.dmChannel.awaitMessages(res =>  {
          if(!res.author.bot) {
            username = res.content;
            blockProcess = 2;
            console.log(res.author.bot);
            return msg.direct('Your username: ' + username);
          }
        }, { max: 1, time: 10000, errors: ['time'], })
        .catch(() => {
          msg.direct('You have ran out of time!');
        });
      }

      if (blockProcess === 2) {
        await msg.direct('What is you email address?');
        await msg.author.dmChannel.awaitMessages(res => {
          if(!res.author.bot) {
            email = res.content;
            blockProcess = 3;
            return msg.direct('Your email: ' + email);
          }
        }, { max: 1, time: 10000, errors: ['time'] })
        .catch(() => {
          msg.direct('You have ran out of time!');
        });
      }

      if (blockProcess === 3) {
        await msg.direct('What do you wish your password to be?');
        await msg.author.dmChannel.awaitMessages(res => {
          if(!res.author.bot) {
            password = res.content;
            blockProcess = 4;
            return msg.direct('Your password: ' + password);
          }
        }, { max: 1, time: 10000, errors: ['time'] })
        .catch(() => {
          msg.direct('You have ran out of time!');
        });
      }

      if (blockProcess === 4) {

        bcrypt.genSalt(10, (err, salt) => {
          bcrypt.hash(password, salt, (err, hash) => {
            if (err) {
              console.log(err);
              return msg.direct('There seems to have been an error. Please report this to the developer.');
            }

            password = hash;
            let post = {
              userid: userId,
              username: username,
              email: email,
              password: password,
            };

            let sql = 'INSERT INTO users SET ?';
            db.query(sql, post, (err, row) => {
              post = {
                userid: userId,
              };
              sql = 'INSERT INTO users_meta SET ?';
              db.query(sql, post, (err, row) => {
                console.log(err);
                return msg.direct('`You have been registered!`');
              });
            });
          });
        });
      }

      if (msg.channel.type !== 'dm') messages.push(await msg.reply('I have sent you a DM with further instructions.'));
    } catch (err) {
      console.log(err);
      messages.push(await msg.reply('Unable to send you a DM, you most likely have them disabled.'));
    }

    return messages;
  }