我正在尝试创建一个discordbot,特别是婚礼团队。
我使用的是MongoDB数据库。现在一切都正常并被保存,但有一个问题,数据被保存到第二轮和第三轮,等等
也就是说,我添加的检查不起作用。我试图通过const exists = Marry.findOne({ message.author.id });
找到数据,所有找到的东西,我用控制台日志进行了检查。
但当我试图验证它时,它根本不起作用。if (exists == message.author.id) { return message.channel.send("You are already married!"); }
可能是什么问题?请帮帮我!
也许userID: message.author.id
,我只需要找到值message.author.id
。有可能吗?
const { Command } = require("discord.js-commando");
const mongoose = require("mongoose");
mongoose.connect('mongodb+srv://admon:admin@cluster0.sobzp.mongodb.net/dbname?retryWrites=true&w=majority');
//create Schema
const marrySchema = new mongoose.Schema({
userID: {
type: mongoose.SchemaTypes.String,
required: true
},
userMarryID: {
type: mongoose.SchemaTypes.String,
required: true
},
userPartnerID: {
type: mongoose.SchemaTypes.String,
required: true
}
});
const Marry = mongoose.model('Marry', marrySchema);
module.exports = class MarryCommand extends Command {
constructor(client) {
super(client, {
name: "marry",
memberName: "marry",
group: "test",
description: "Marry the mentioned user",
guildOnly: true,
args: [{
key: "userToMarry",
prompt: "Please select the member you wish to marry.",
type: "member",
}, ],
});
}
run(message, { userToMarry }) {
const exists = Marry.findOne({ userID: message.author.id });
const married = Marry.findOne({ userID: userToMarry.id });
if (!userToMarry) {
return message.channel.send("Please try again with a valid user.");
}
if (exists == message.author.id) {
return message.channel.send("You are already married!");
}
if (married == userToMarry.id) {
return message.channel.send("This user is already married!");
}
if (userToMarry.id == message.author.id) {
return message.channel.send("You cannot marry yourself!");
}
if (exists != message.author.id && married != userToMarry.id) {
message.channel.send(
`**Important announcement!**
${message.author} makes a marriage proposal ${userToMarry}
Are you ready to get married?`
)
.then((message) => {
message.react("👍")
.then(() => message.react("👎"))
.catch(() => {
//code
});
message.awaitReactions((reaction, user) =>
user.id == userToMarry.id && (reaction.emoji.name == "👍" || reaction.emoji.name == "👎"), {
max: 1,
time: 10000,
errors: ["time"]
}
).then((collected) => {
const reaction = collected.first();
if (reaction.emoji.name === "👎") {
return message.channel.send("I think **no**...");
}
if (reaction.emoji.name === "👍") {
const createdExists = new Marry({
userID: message.author.id,
userMarryID: message.author.id,
userPartnerID: userToMarry.id
});
createdExists.save().catch(e => console.log(e));
const createdMarried = new Marry({
userID: userToMarry.id,
userMarryID: userToMarry.id,
userPartnerID: message.author.id
});
createdMarried.save().catch(e => console.log(e));
message.channel.send(`${message.author} and ${userToMarry} now married!!`)
.catch(() => {
message.reply(
"No reaction after 10 seconds, operation canceled"
);
});
}
}).catch(() => {});
}).catch(() => {});
}
}
};
所以我认为您的代码中存在异步问题。查询本身应该可以正常工作。但是,我建议您将存在并且已婚移出运行。也许可以将它们粘贴到MongoDB连接下,并找到一种方法来传递消息。
const Marry = mongoose.model('Marry', marrySchema);
const exists = Marry.findOne({ message.author.id });
const married = Marry.findOne({ userToMarry.id });
如果这不起作用,您可以尝试在run方法中使findOne查询异步,如下所示:
const married = await db.collection("yourcollectioname").findOne({ message.author.id });
const exists = await db.collection("yourcollectioname").findOne({ userToMarry.id });
尝试将async
用于run函数。接下来,await
在数据库中搜索数据。
你会得到这样的东西:
async run(message, { userToMarry }) {
const exists = await Marry.findOne({ userID: message.author.id });
此外,在检查中,我们将userID
添加到exists
以精确比较数字:
if (exists?.userID === message.author.id) { return message.channel.send("You are already married!"); }
?
这是在没有数据和exists = null
的情况下
与userToMarry
相同