亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何讓機器人回答用戶,當機器人響應并且用戶想要繼續時,就像對話一樣(但不重復)

如何讓機器人回答用戶,當機器人響應并且用戶想要繼續時,就像對話一樣(但不重復)

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 關注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號