如何让您的 Discord 机器人在您输入命令后收听您的消息?

Sol*_*der 2 javascript arguments bots discord discord.js

我正在制作一个 Google Assistant Discord 机器人,但我想知道你的机器人将如何回复你的第二条消息。例如:首先,您说hey google,然后机器人说I'm listening,然后您说what time is it,他说2.40 pm

我做了第一部分,但我不知道如何让它回复第二个参数。有人可以帮我吗?

Zso*_*ros 7

您可以使用消息收集器。您可以发送I\'m listening消息并在同一频道中使用设置收集器createMessageCollector

\n

对于其过滤器,您可以检查传入消息是否来自想要询问您的助手的同一用户。

\n

您还可以添加一些选项,例如收集器收集消息的最长时间。我将其设置为一分钟,一分钟后它会发送一条消息,让用户知道您不再收听。

\n
client.on(\'messageCreate\', async (message) => {\n  if (message.author.bot) return;\n\n  if (message.content.toLowerCase().startsWith(\'hey google\')) {\n    const questions = [\n      \'what do you look like\',\n      \'how old are you\',\n      \'do you ever get tired\',\n      \'thanks\',\n    ];\n    const answers = [\n      \'Imagine the feeling of a friendly hug combined with the sound of laughter. Add a librarian\xe2\x80\x99s love of books, mix in a sunny disposition and a dash of unicorn sparkles, and voila!\',\n      \'I was launched in 2021, so I am still fairly young. But I\xe2\x80\x99ve learned so much!\',\n      \'It would be impossible to tire of our conversation.\',\n      \'You are welcome!\',\n    ];\n\n    // send the message and wait for it to be sent\n    const confirmation = await message.channel.send(`I\'m listening, ${message.author}`);\n    // filter checks if the response is from the author who typed the command\n    const filter = (m) => m.author.id === message.author.id;\n    // set up a message collector to check if there are any responses\n    const collector = confirmation.channel.createMessageCollector(filter, {\n      // set up the max wait time the collector runs (optional)\n      time: 60000,\n    });\n\n    // fires when a response is collected\n    collector.on(\'collect\', async (msg) => {\n      if (msg.content.toLowerCase().startsWith(\'what time is it\')) {\n        return message.channel.send(`The current time is ${new Date().toLocaleTimeString()}.`);\n      }\n\n      const index = questions.findIndex((q) =>\n        msg.content.toLowerCase().startsWith(q),\n      );\n\n      if (index >= 0) {\n        return message.channel.send(answers[index]);\n      }\n\n      return message.channel.send(`I don\'t have the answer for that...`);\n    });\n\n    // fires when the collector is finished collecting\n    collector.on(\'end\', (collected, reason) => {\n      // only send a message when the "end" event fires because of timeout\n      if (reason === \'time\') {\n        message.channel.send(\n          `${message.author}, it\'s been a minute without any question, so I\'m no longer interested... `,\n        );\n      }\n    });\n  }\n});\n
Run Code Online (Sandbox Code Playgroud)\n

在此输入图像描述

\n