javascript - 输入命令后如何让你的机器人听你的消息?
问题描述
我正在制作一个 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... `,
);
}
});
}
});
推荐阅读
- git - git push 错误:无法写入文件 ./objects/... 没有这样的文件或目录
- oauth-2.0 - “通过授权码授予获取 3 条腿令牌”不是 OAuth 2.0 RFC 投诉并生成错误 400
- java - 在 javaFx 中将整数表视图显示为字符串
- python-3.x - 在索引到数组时求解向量二阶微分方程
- ios - iOS:在锁定屏幕上使用控件时,AVPlayer 会播放多个音频实例
- node.js - Node.js - 将类似数组的字符串转换为数组
- javascript - 如何从我的 Babel 编译代码中删除 transform typeof 符号代码?
- javascript - 通过javascript从时间轴获取笔记输入控件
- c - 进程的退出状态
- php - 你能将 Symfony 配置为使用 XML 而不是 YAML 和注解吗?