【问题标题】:DiscordJs cascade of awaitMessages等待消息的 DiscordJs 级联
【发布时间】:2021-03-26 01:22:51
【问题描述】:

我是 discordjs 包的新手,我一直在挖掘,但在过去的 2 天里,我被困在这个问题上。

我想要实现的是“问卷调查”,它将引导用户完成一些问题。一个例子是: 用户类型:!askOrder

  • 问:你要红酒还是白酒?
  • A:白葡萄酒
  • 问:你想要白葡萄酒。你喜欢 verdejo 还是 rueda?
  • 答:rueda
  • 问:它是一个rueda。你想在室温下还是冷藏?
  • A:室温。

我所拥有的是:

msg.author.send('Do you want red wine or white wine?');        
        msg.author.send(WineText)
        .then((systemMsg) => { 
            systemMsg.channel.awaitMessages(response => response.content, {
              max: 1,
              time: 5000,
              errors: ['time'],
        }).then((collected) => {
            systemMsg.channel.send(`You want ${collected.first().content}. Do you prefer a verdejo or a rueda?`);
            collected.channel.awaitMessages(response => response.content, {
                max: 1,
                time: 10000
            });
        })then((collected2) => {
            systemMsg.channel.send(`A ${collected2.first().content} it's. Do you want it at room temperature or chill? `);
        }).catch((err) => {
            return;
        });

但它只是在提出第二个问题后停止,而不是捕获/收集第二个答案。我到底做错了什么?

非常感谢您的回答!

【问题讨论】:

    标签: discord.js


    【解决方案1】:

    我要做的是将所有问题(和答案)放在一个数组中,将这些语句包装在一个函数中,然后像这样在迭代中循环它们:

        let i = 0; let questions = ['Q1', 'Q2'];
    
        function askQuestion() {
            const filter = m => m.author.id == message.author.id;
    
            message.channel.send(questions[i]).then(() => {
                message.channel.awaitMessages(filter, { max: 1, time: 120000, errors: ['time'] })
                    .then(async collected => {
    
                        //do stuff
                        message.channel.send(`answer was ${collected.first().content}`);
    
                        i++; //increase iteration
                        if (i === questions.length) return;
                        else askQuestion();
                    })
                    .catch(collected => {
                        //timed out
                    });
            });
        }
        askQuestion();
    

    它有助于稍微清理您的代码。您可以根据需要更改它们。

    我做了一个这样问问题的机器人,你可以查看代码here

    【讨论】:

      【解决方案2】:

      您的 awaitMessages 事件结构错误:

      msg.author.send('Do you want red wine or white wine?');        
              msg.author.send(WineText)
              .then((systemMsg) => { 
                  systemMsg.channel.awaitMessages(response => response.content, {
                    max: 1,
                    time: 5000,
                    errors: ['time']
              }).then((collected) => {
                  systemMsg.channel.send(`You want ${collected.first().content}. Do you prefer a verdejo or a rueda?`);
                  collected.first().channel.awaitMessages(response => response.content, {
                      max: 1,
                      time: 10000
                  }).then((collected2) => {
                      systemMsg.channel.send(`A ${collected2.first().content} it's. Do you want it at room temperature or chill? `);
              }).catch((err) => {
                  return;
              });
              });
              });
      

      你犯了两个关键错误:

      (1) 您向collected.channel 发送了一条消息。但是,collected 是消息的collection,并且没有通道属性。要将其发送到频道,只需执行collected.first().channel,因为collected.first() 是一条消息。 (2) 您在代码末尾缺少两个 })s,因为您包含了 thens 并且从未关闭。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-02
        • 1970-01-01
        • 2021-12-29
        • 1970-01-01
        • 1970-01-01
        • 2014-09-15
        • 2011-06-09
        • 2021-04-23
        相关资源
        最近更新 更多