为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

HUWWW 2022-12-18 16:20:23
例子: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
查看完整描述

1 回答

?
喵喔喔

TA贡献1735条经验 获得超5个赞

您可以为用户定义一个状态,并根据用户的回答决定下一步去哪里,例如(我假设客户端对象上有某种 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 :(");

  }

});


查看完整回答
反对 回复 2022-12-18
  • 1 回答
  • 0 关注
  • 127 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号