【问题标题】:How to make the bot answer the user, when the bot responded and the user wanted to continue, like a conversation (but without repeating)当机器人响应并且用户想要继续时,如何让机器人回答用户,就像对话一样(但不重复)
【发布时间】:2020-11-23 16:34:15
【问题描述】:

示例:

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

【问题讨论】:

    标签: javascript node.js discord bots discord.js


    【解决方案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 :(");
      }
    });
    

    【讨论】:

    • 嘿伙计,出现此错误:(节点:18318)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。发生了什么?
    • 可能发送的不是字符串,让我仔细检查一下我的代码
    • 好吧,我没有修改你的代码来测试...可能是因为我没有修改...对不起,我在 Javascipt 中很糟糕...我想修改什么? :(
    • 是的,打错了,现在检查一下(记住我使用了client.id,我不知道你是否可以访问)
    • 陷入开发者模式的人?
    猜你喜欢
    • 2022-10-13
    • 2019-06-01
    • 1970-01-01
    • 2019-01-29
    • 2019-12-21
    • 2017-03-20
    • 2019-04-16
    • 2019-12-16
    • 1970-01-01
    相关资源
    最近更新 更多