javascript - 如何让 Discord 机器人等待我的消息?
问题描述
我正在制作一个简单的不和谐机器人,这是我第一次这样做。我希望 Discord 机器人在继续之前等待我的消息,但我似乎无法让它工作。如何让 Discord 机器人在继续之前等待?另外,我知道我的“抛出新错误”可能不是非常正确;我不确定是否有其他方法可以阻止跑步。
任何帮助是极大的赞赏!太感谢了!
代码:
const Discord = require('discord.js');
module.exports = class NewgameCommand extends BaseCommand {
constructor() {
super('newgame', 'managment', []);
}
async run(client, message, args) {
if (!message.member.roles.cache.some((r) => r.name === "staff")) {
return message.channel.send("Only staff members can set up games!");
}
const gameEmbed = new Discord.MessageEmbed()
.setTitle('New MvM Mondays Game!')
.setDescription('Add description here.')
.setColor("#FF0000")
.setTimestamp();
const twoCitiesMissionsDisplayed = "\`\`\`\n1 - Empire Escalation\n2 - Metro Malice\n3 - Hamlet Hostility\n4 - Bavarian Botbash\`\`\`";
const twoCitiesMissionsArray = ['Empire Escalation', 'Metro Malice', 'Hamlet Hostility', 'Bavarian Botbash'];
const numOfMissions = [1, 2, 3, 4];
message.channel.send("What Two Cities mission?");
message.channel.send(twoCitiesMissionsDisplayed);
message.channel.awaitMessages(m => m.author.id == message.author.id,
{max: 1, time: 30000}).then(collected => {
const chosenMission = collected.first().content;
if (!numOfMissions.includes(chosenMission)) {
message.channel.send("You chose an invalid number!");
throw new Error("Chose invalid mission number");
}
}).catch(() => {
message.channel.send('No answer after 30 seconds, operation canceled.');
throw new Error("Took too long to respond.");
});
message.channel.send("Default classes? (yes/no)");
message.channel.awaitMessages(m => m.author.id == message.author.id,
{max: 1, time: 30000}).then(collected => {
const classesAnswer = collected.first().content;
if (!numOfMissions.includes(collected.first().content)) {
message.channel.send("All you had to do was say \"yes\" or \"no\"...");
throw new Error("Didn't say \"yes\" or \"no\"");
}
}).catch(() => {
message.channel.send('No answer after 30 seconds, operation canceled.');
throw new Error("Took too long to respond.");
});
}
}
我在 Discord 中看到的:
解决方案
我们可以使用 awaitMessages 承诺的 catch 块来检测超时。然后,我们可以让它在超时后发出错误。这是一个例子。
function filter(m){
if(m.author.id != message.author.id) return false;
if (!numOfMissions.includes(chosenMission)) {
m.channel.send('You chose an invalid number!');
return false;
}
return true;
}
message.channel.awaitMessages(filter, { max: 1, time: 30000, errors: ['time'] })
.then(collected => {
const chosenMission = collected.first().content;
message.channel.send(`You chose the ${chosenMission} mission`);
})
.catch(() => {
message.channel.send('No answer after 30 seconds, operation canceled.');
});
如您所见,我在过滤器中包含了有效数字检查。这可以允许消息收集器即使输入了无效号码也不会结束。
另外,我包含errors: ['time']
在awaitMessages()
功能的选项中。这告诉函数在时间到时(本例中为 30 秒)抛出错误,该错误将导致代码落入 Promise 的catch
块中。
推荐阅读
- php - 为从 API 接收的数据创建分页
- python - 获取嵌套字典中字典中键的平均值
- java - Local.getDefault().getCountry() 不稳定 - Image Picker Android 错误
- excel - Power Query:使用 List.Accumulate 添加多个自定义列
- reactjs - 如何防止此递归函数超过最大调用堆栈?
- c++ - QML地图没有缩放到光标
- amazon-web-services - 如何将数据从一个 AWS S3 文件夹复制到同一存储桶中的另一个文件夹?
- node.js - NGINX _ nginx 上的 rtmp nodejs 服务器配置:[emerg] bind() to 0.0.0.0:1935 failed (98: Address already in use)
- javascript - 创建视频聊天应用程序时出现 WebRTC 错误
- gitlab - jq 是否已安装并在 PATH 上可用?