如何让机器人回答用户,当机器人响应并且用户想要继续时,就像对话一样(但不重复)

例子:


user: "Hello!" - bot: "Hi! You wanna help with the codes?"



user: "No" - bot: "Okay!"

但是只有当用户打招呼时才会发生。我不希望他回答“好吧!” 当用户在任何句子中说“不”时......

我用来回复用户的机器人代码是:


client.on('message', async message => { 

if (message.content.toLowerCase().includes("hello")) {

    message.channel.send("Hi! You wanna help with the codes?");

}

});

对不起我的英语错误,我不会说那么多英语......


无论如何,有人可以帮助我吗?


特别感谢:https ://stackoverflow.com/users/5896453/naszos



HUWWW
浏览 128回答 1
1回答

喵喔喔

您可以为用户定义一个状态,并根据用户的回答决定下一步去哪里,例如(我假设客户端对象上有某种 id):const userStates = {};const replies = {  "": [    {      messages: ["hello"],      answer: "Hi! You wanna help with the codes?",      next_state: "asked_to_help",    },  ],  asked_to_help: [    {      messages: ["no"],      answer: "Okay :(",      next_state: "",    },    {      messages: ["yes"],      answer: "Yay, tell me more!",      next_state: "some_next_stage",    },  ],};client.on("message", async (message) => {  userStates[client.id] = userStates[client.id] || "";  const text = message.content.toLowerCase();  const possibleReplies = replies[userStates[client.id]].filter((reply) =>    reply.messages.includes(text)  ); // filter by matching messages  const reply = possibleReplies [Math.floor(Math.random() * possibleReplies .length)]; // get random answer from valid ones  if (reply) {    message.channel.send(reply.answer);    userStates[client.id] = reply.next_state;  } else {    message.channel.send("I dont understand :(");  }});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript