问题描述
我正在制作 Google Assistant discord 机器人,但我想知道您的机器人将如何回复您的第二条消息。例如:
首先,你说hey google
,然后机器人说I'm listening
,然后你说what time is it
,他说2.40 pm
。
我做了第一部分,但我不知道如何让它回复第二个参数。有人可以帮我吗?
解决方法
您可以使用消息收集器。您可以发送 I'm listening
消息并在同一频道中使用 createMessageCollector
设置收集器。
对于它的过滤器,您可以检查传入的消息是否来自想要询问您的助手的同一用户。
您还可以添加一些选项,例如收集器收集消息的最长时间。我将其设置为一分钟,一分钟后它会发送一条消息,让用户知道您不再收听。
client.on('message',async (message) => {
if (message.author.bot) return;
if (message.content.toLowerCase().startsWith('hey google')) {
const questions = [
'what do you look like','how old are you','do you ever get tired','thanks',];
const answers = [
'Imagine the feeling of a friendly hug combined with the sound of laughter. Add a librarian’s love of books,mix in a sunny disposition and a dash of unicorn sparkles,and voila!','I was launched in 2021,so I am still fairly young. But I’ve learned so much!','It would be impossible to tire of our conversation.','You are welcome!',];
// send the message and wait for it to be sent
const confirmation = await message.channel.send(`I'm listening,${message.author}`);
// filter checks if the response is from the author who typed the command
const filter = (m) => m.author.id === message.author.id;
// set up a message collector to check if there are any responses
const collector = confirmation.channel.createMessageCollector(filter,{
// set up the max wait time the collector runs (optional)
time: 60000,});
// fires when a response is collected
collector.on('collect',async (msg) => {
if (msg.content.toLowerCase().startsWith('what time is it')) {
return message.channel.send(`The current time is ${new Date().toLocaleTimeString()}.`);
}
const index = questions.findIndex((q) =>
msg.content.toLowerCase().startsWith(q),);
if (index >= 0) {
return message.channel.send(answers[index]);
}
return message.channel.send(`I don't have the answer for that...`);
});
// fires when the collector is finished collecting
collector.on('end',(collected,reason) => {
// only send a message when the "end" event fires because of timeout
if (reason === 'time') {
message.channel.send(
`${message.author},it's been a minute without any question,so I'm no longer interested... ?`,);
}
});
}
});