【问题标题】:Await reply in private message discord.js在私信 discord.js 中等待回复
【发布时间】:2018-12-09 11:47:54
【问题描述】:

因此,这几乎可以按预期工作。我让机器人私信给在服务器正确通道中使用正确命令的用户。我没有让配置文件创建消息阻塞服务器,而是让机器人私下创建配置文件。我知道/认为问题出在 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.');
      });
    });
  }

提前感谢您的宝贵时间!

【问题讨论】:

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


    【解决方案1】:

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

    当您使用message.channel.awaitMessages 时,message 仍然是您在module.exports.run 函数中传递的第一个,而不是来自send() 的那个。当send 被解析时,它会传递一条新消息,你必须将它包含在你放在then 中的函数的参数中:所以,你需要.then(function(new_message_sent_in_private) {}) 之类的东西,而不是.then(function() {})

    这应该可以解决问题:

    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.');
        });
      });
    }
    

    如果可行,现在让我来:)

    【讨论】:

    • 是的,确实有效。但是,mysql 不工作,这是我所期望的。我会弄清楚!感谢您的帮助,因为这似乎工作得很好! :)
    猜你喜欢
    • 2018-02-02
    • 2021-07-23
    • 2020-05-24
    • 2020-07-02
    • 2021-12-30
    • 2020-11-21
    • 2020-12-05
    • 1970-01-01
    • 2020-07-02
    相关资源
    最近更新 更多