I’m trying to create a discord bot, specifically the wedding team.
I am using a MongoDB database. Now everything works and is saved, but there is one problem, the data is saved for the second and third rounds, etc.
That is, the checks that I added do not work. I am trying to find data through const exists = Marry.findOne({ message.author.id });
, everything finds, I checked with console log.
But when I try to validate it just doesn’t work. if (exists == message.author.id) { return message.channel.send("You are already married!"); }
What could be the problem? Help me please!
Maybe instead userID: message.author.id
i just need to find for the value message.author.id
. Is it possible?
const { Command } = require("discord.js-commando");
const mongoose = require("mongoose");
mongoose.connect('mongodb+srv://admon:[email protected]/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(() => {});
}
}
};
2
Answers
So I think you’re having an async issue in your code. The query itself should work just fined. However, I suggest you move exists and married outside of run. Maybe paste them under the MongoDB connection and find a way to pass the message.
If that doesn’t work, another thing you could try is making the findOne queries async inside the run method, like this:
Try using
async
for the run function. Next,await
to search for data in the database.You get something like this:
Further, in the check, we add
userID
toexists
to compare exactly the numbers:?
this is in case there is no data andexists = null
Same for
userToMarry