Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何让discord.js bot仅在用户响应后回复一系列DM消息?_Javascript_Node.js_Discord.js - Fatal编程技术网

Javascript 如何让discord.js bot仅在用户响应后回复一系列DM消息?

Javascript 如何让discord.js bot仅在用户响应后回复一系列DM消息?,javascript,node.js,discord.js,Javascript,Node.js,Discord.js,我试图让一个机器人读取和回复一系列来自DM频道用户的回复。在我的设置中,工作流的过程是(左侧的消息作者): 现在我有一个waitingmessages来收集用户对每个问题的回答。预期的功能是,bot将存储响应,并在主服务器上创建包含所有信息的嵌入式帖子。但现在,每个问题都是在同一时间提出的,而不是在等待用户回答之后。以下是我当前代码的一个片段: client.on("message", msg => { var words = msg.content.split(' ').map(

我试图让一个机器人读取和回复一系列来自DM频道用户的回复。在我的设置中,工作流的过程是(左侧的消息作者):

现在我有一个
waitingmessages
来收集用户对每个问题的回答。预期的功能是,bot将存储响应,并在主服务器上创建包含所有信息的嵌入式帖子。但现在,每个问题都是在同一时间提出的,而不是在等待用户回答之后。以下是我当前代码的一个片段:

client.on("message", msg => {
    var words = msg.content.split(' ').map(x => x.toLowerCase());
    if (msg.author.bot || msg.guild !== null || words[1] !== 'post') return;
    const filter = m => m.author.id === msg.author.id;

    img = "" \\ store the image url inputted
    date = "" \\ store the date as a string

    \\ bot asks for image
    msg.author.send("First, upload an image if you have one. If not, just reply `skip`.") 
        .then(() => {
            msg.channel.awaitMessages(filter, {
                max: 1,
                time: 300000
            })
                .then((collected) => {
                    console.log(collected.first().content)
                    if (collected.first().attachments.size === 1) {
                        img = collected.first().attachments.first().url;
                        msg.author.send("Image successfully uploaded.");
                    } else {
                        msg.author.send("No image uploaded.");
                    }
                })
                .catch(() => {
                    msg.author.send("No image uploaded. Your time limit ran out");
                });
        })

    \\ bot asks for date input
    msg.author.send("Next, please input a start/due date in `dd/mm/yyyy`.") 
        .then(() => {
            msg.channel.awaitMessages(filter, {
                max: 1,
                time: 30000,
                errors: ['time'],
            })
                .then((collected) => {
                    date = collected.first().content
                    msg.author.send("Date has been entered.")
                })
                .catch(() => {
                    msg.author.send("No date was entered. Your time limit ran out");
                });
        });

})
运行bot并在到bot的DM通道中发送消息
bot post
,结果如下:

bot: First upload an image if you have one. If not just reply skip.
bot: Next, please input a start date/due date in dd/mm/yyyy form.
这导致写入的下一条消息是在两条
等待消息中收集的消息,即回复
跳过
将告诉第一部分没有图像,同时告诉第二部分输入的日期是
跳过

有没有办法让机器人仅在用户响应前一个功能后才执行该功能?我尝试使用
setTimeout()
延迟每个部分,但这不是一个理想的功能。我希望bot在读取用户输入后立即回复


如何解决问题?

您需要将第二个
send
函数移动到第一个函数的
then()
中。有点像这样:

client.on(“message”,msg=>{
var words=msg.content.split(“”.map(x=>x.toLowerCase());
如果(msg.author.bot | | | msg.guild!==null | | | words[1]!==post)返回;
const filter=m=>m.author.id==msg.author.id;
img=”“;//存储输入的图像url
date=”“;//将日期存储为字符串
//机器人请求图像
msg.author
.send(“首先,如果你有一张图片,请上传一张。如果没有,请回复‘跳过’。”)
.然后(()=>{
味精频道
.等待消息(过滤器{
最高:1,
时间:30万
})
。然后(收集=>{
console.log(collected.first().content);
if(collected.first().attachments.size==1){
img=collected.first().attachments.first().url;
msg.author.send(“图像已成功上传”);
//bot请求输入日期
msg.author
.send(“下一步,请在'dd/mm/yyyy'中输入开始/到期日期”)
.然后(()=>{
味精频道
.等待消息(过滤器{
最高:1,
时间:30000,
错误:[“时间”]
})
。然后(收集=>{
日期=收集的.first().content;
msg.author.send(“已输入日期”);
})
.catch(()=>{
msg.author.send(
“未输入日期。您的时间限制已过”
);
});
});
}否则{
msg.author.send(“没有上传图像”);
}
})
.catch(()=>{
msg.author.send(“没有上传图像,您的时间限制已过”);
});
});
});

是的,这看起来很糟糕。您可以研究async/await,使您的代码更易于阅读,缩进更少。

谢谢,这对我帮助很大,对我也很有用。我之前尝试过使用异步/等待结构,但无法使其正常工作。使用then()和catch()确实会使代码非常混乱,因此我肯定会研究如何正确使用async/await。
bot: First upload an image if you have one. If not just reply skip.
bot: Next, please input a start date/due date in dd/mm/yyyy form.