等待私人消息discord.js中的回复

时间:2018-06-30 14:58:17

标签: javascript database async-await private discord.js

因此,这可以按预期工作。我有机器人专用消息传递,该用户在服务器的正确通道中使用正确的命令。我没有使用配置文件创建消息来阻塞服务器,而是让该机器人私下进行配置文件创建。我知道/认为问题出在message.channel.awaitMessages,因为它只看到命令所在的服务器上原始通道中的答复。

我不确定应该用什么替换message.channel.awaitMessages,以便在私人消息对话而不是服务器上的原始概要文件创建通道中获取答复。

我尚未测试sql消息。我还没有测试我拥有的东西,但是我很确定它不会起作用。我希望将用户在私人消息中给出的答复插入(可能已更新?)到我已经设置并可以正常工作的mysql数据库中。用户ID和用户名已放入此表中,与个人资料相关的其他问题此刻为空。当他们回答这些问题时,可以填写/更新这些字段。欢迎任何想法/建议!

module.exports.run = async (bot, message, args) => {
if(message.channel.id === '460633878244229120') return message.author.send(`Greetings, ${message.author.username}! FIRST QUESTION, **What is the name of your Brawler or Character?**`)
  .then(function(){
    message.channel.awaitMessages(response => message.content, {
      max: 1,
      time: 5000,
      errors: ['time'],
    })
    .then((collected) => {
        message.author.send(`Your Name is: ${collected.first().content}`);
        var sql = (`UPDATE profile SET name = '${message.content}' WHERE id = ${message.author.id}`);
      })
      .catch(function(){
        message.author.send('Please submit a name for your character. To restart Profile creation, please type "!profilecreate" command in Profile Creation channel on the server.');
      });
    });
  }

提前感谢您的时间!

1 个答案:

答案 0 :(得分:0)

我不使用SQL,因为问题不是专门针对此问题,所以不要问我。

使用message.channel.awaitMessages时,message仍然是您在module.exports.run函数中传递的第一个,而不是来自send()的函数。解决send时,它会传递一条新消息,您必须将其包含在then内的函数的参数中:因此,除了.then(function() {})之外,您还需要.then(function(new_message_sent_in_private) {})

这应该可以解决问题:

module.exports.run = async (bot, message, args) => {
if(message.channel.id === '460633878244229120') return message.author.send(`Greetings, ${message.author.username}! FIRST QUESTION, **What is the name of your Brawler or Character?**`)
  .then((newmsg) => { //Now newmsg is the message you sent
    newmsg.channel.awaitMessages(response => response.content, {
      max: 1,
      time: 5000,
      errors: ['time'],
    }).then((collected) => {
      newmsg.channel.send(`Your Name is: ${collected.first().content}`);
      var sql = (`UPDATE profile SET name = '${message.content}' WHERE id = ${message.author.id}`);
    }).catch(() => {
      newmsg.channel.send('Please submit a name for your character. To restart Profile creation, please type "!profilecreate" command in Profile Creation channel on the server.');
    });
  });
}

现在让我看看是否可行:)