javascript - 指定一条私人消息以获得有限的答案 discord.js
问题描述
我有一个机器人通过私人消息向用户提问。我只是希望机器人只接受某些单词作为答案。
例如,询问用户他们的性别。我希望选项是“男性”、“女性”和“其他”。如果机器人收到的不是这些答案,我还希望它说“请输入“男性”、“女性”或“其他”。我尝试了一些似乎不起作用的不同方法。代码有效,在我搞砸的行之外,但会接受“猫”,例如,作为性别,所以我显然希望它只接受几个答案。不确定指定这个的 if 语句是否有效。我知道它是如何单独工作的,但结合等待,我很困惑......
这是我上次尝试的代码,但无济于事(更不用说它给出了语法错误/由于我弄乱的行而导致的意外标记)
module.exports.run = async (bot, message, args) => {
message.author.send(`THIRD QUESTION, **What is the gender of your Brawler or Character?** Please enter "Male", "Female" or "Other".`)
.then((newmsg) => { //Now newmsg is the message you send to the bot
newmsg.channel.awaitMessages(response => response.content.includes("Male", "Female", "Other") {
max: 1,
time: 300000,
errors: ['time'],
}).then((collected) => {
newmsg.channel.send(`Your brawler's gender is: **${collected.first().content}**
If you are okay with this gender, type !profilerace to continue the profile creation process!
If you would like to edit your gender, please type !profilegender`)
con.query(`UPDATE profile SET gender = '${collected.first().content}' WHERE id = ${message.author.id}`);
console.log("1 record updated!")
}).catch(() => {
newmsg.channel.send('Please submit an age for your character. To restart Profile creation, please type "!profilecreate" command in Profile Creation channel on the server.');
});
//con.query(sql, console.log);
//if (err) throw err;
//console.log("1 record inserted!")
});
}
编辑:在下面的评论中得到一些帮助后,我得到了这样的结果。我不断收到语法错误。我觉得我不知道我在这里做什么。囧
module.exports.run = async (bot, message, args) => {
message.author.send(`THIRD QUESTION, **What is the gender of your Brawler or Character?** Please enter "Male", "Female" or "Other".`)
.then((newmsg) => { //Now newmsg is the message you send to the bot
newmsg.channel.awaitMessages(response => response.content, {
max: 1,
time: 300000,
errors: ['time'],
})
.then((collected) => {
if (response.content === "Male" || response.content === "Female"|| response.content === "Other") {
return
newmsg.channel.send(`Your brawler's gender is: **${collected.first().content}**
If you are okay with this gender, type !profilerace to continue the profile creation process!
If you would like to edit your gender, please type !profilegender`)
con.query(`UPDATE profile SET gender = '${collected.first().content}' WHERE id = ${message.author.id}`);
console.log("1 record updated!")}
} else {
newmsg.channel.send("Please submit 'Male', 'Female' or 'Other'. Type !profilegender to restart the choice of gender.")
}
}).catch(() => {
newmsg.channel.send('Please submit an age for your character. To restart Profile creation, please type "!profilecreate" command in Profile Creation channel on the server.');
});
});
}
谢谢您的帮助!
解决方案
我不确定你的意思,但我会创建一个 ArrayList 并检查消息的内容是否与 ArrayList 中的值之一匹配,如下所示:
const genders = ["male", "female", "other"];
if (genders.indexOf(response.content) > -1) {
// code if person chose one of the three options.
} else {
// code here if person did not choose one of the three options and put something else.
}
推荐阅读
- java - LibGDX:模型、节点、网格、MeshPart 架构
- c# - 复选框按钮工作一次
- python - Spyder IDE 随机死机
- prism - 简化的 PRISM 事件聚合器 - 订阅错误
- apache - Apache 在服务器上提供不同的 HTTPS 虚拟主机,而不是请求的虚拟主机
- python - python matplotlib.patches:绘制一个圆形补丁,但只保留圆形的一部分
- mysql - 如何选择过去 4 整周的数据(周日到周六)?
- graphql - graphql 突变和沙发库
- python - 当 DEBUG 设置为 False 时,Django 不提供媒体文件
- ajax - f:ajax 不会使用从 selectOneMenu 中选择的值呈现 DataTable