Discord.js:如何异步使用awaitMessages多次而不会产生过多的嵌套

时间:2019-10-05 02:01:38

标签: javascript discord.js

我正在为为幻想型服务器构建的机器人编写命令。该命令完成后,将使用户“请求一个咒语”,这实际上将向用户询问一系列问题,然后将其传递给将所有数据发送给我的函数,以便我可以进行查找。将其添加到机器人中。

问题是我无法一次不问每个问题地嵌套每个问题,在编辑器中感觉效率很低而且很混乱。

我拥有使它正常工作所需的所有其他功能,但是唯一的问题是,它会立即用所有消息轰炸我。我已经尝试过then()方法,但是对它的理解却很缺乏。

所以这是现在的样子:

if (message.content.includes("spell request")) {
        var spellName = message.content.slice(prefix.length + 14);
        var requestee = message.member.id;
        function question(toAsk) {
                var filter = msg => msg.member.id == requestee;
                var collector = message.channel.createMessageCollector(filter, { time: 200000 });
                message.channel.send(toAsk).then(() => {
                    message.channel.awaitMessages(filter, { maxMatches: 1, time: 200000, errors: ['time'] })
                        .then(collected => {

   return(collected.first().content);
                        })
                        .catch(collected => {
                            return("failed")
                        });
                });
    }
    message.channel.send("I'm going to give you a list of questions. Please make sure you read them carefully and answer them as directed, so that Wubzy will consider adding them to my database. Once you've finished, I'll neatly package deliver your spell to Wubzy, and he'll let you know if it gets added!")
    var spellName = question(`So, first and foremost, you named your spell \`${spellName}\`, but I want to be sure that that's the spell you want. So please tell me your spell name just by sending a message.`).then(() => {
        if (spellName == "failed") {
            return message.channel.send("It looks like you ran out of time. (Unless for some reason you named your spell \"failed\", in which case, try again but rename it.) Please try again, if you're still here, that is.");
        } else {
            var spellDesc = question(`Now, please describe your spell. Tell me what it looks like, and other special info about the spell like drawbacks.`);
        }
    });
}

很多道歉,如果有缩进错误,将其复制到堆栈中很棘手。

但是,细分是:

-它需要分别发送每个问题

-回答完前一个问题后,需要发送下一个问题

-如果当前过滤器用完了,则需要结束一系列问题。

注意到当前存在错误告诉我它无法读取未定义的属性“ then”,这可能也会有所帮助。

0 个答案:

没有答案