0

I'm trying to create a discord bot, specifically the married.

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({ userID: message.author.id });, everything finds, I checked with console log. But I get text for 100 lines, one of the lines contains userID: 9573697251580611109.

But I need to get only numbers 9573697251580611109 and nothing more as I try to validate it just doesn't work. if (exists == message.author.id) { return message.channel.send("You are already married!"); }

How can i do this? Help me please!

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
    },

    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",
            }, ],
        });
    }

    async run(message, { userToMarry }) {
        const exists = await Marry.findOne({ userID: message.author.id });
        const married = await 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,
                                    userPartnerID: userToMarry.id
                                });
                                createdExists.save().catch(e => console.log(e));

                                const createdMarried = new Marry({
                                    userID: 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(() => {});
        }
    }
};

Now I am getting the full record from the database

{"_id":{"$oid":"6245bfbd9f4e545addad1111"},
"userID":"654733308680201312",
"userPartnerID":"5134125460452801324",
"__v":{"$numberInt":"0"}}

And I need to get only numbers from userID (654733308680201312) so that I can check

2 Answers2

0

you need to use await

for example

    const exists = await Marry.findOne({ userID: message.author.id });
    const married = await Marry.findOne({ userID: userToMarry.id });
  • Thanks, there are no more extra lines, now I get only the entire record from the database `{ userID: '654733308680201312', userMarryID: '654733308680201312', userPartnerID: '5134125460452801324', }` –  Apr 01 '22 at 09:05
  • But I only need the numbers of the first ID, is that possible? Or somehow a separate function to get it –  Apr 01 '22 at 09:05
  • yes it's possible you can use count() for count result – Mehul Koradiya Apr 01 '22 at 09:39
0

You need to use await for every findOne If you want to find any data on your db. This find the userToMarry.id message.author.id on your db

const exists = await Marry.findOne({ userID: message.author.id });
const married = await Marry.findOne({ userID: userToMarry.id });

And why are you using userID twice for different function? userToMarry should be a member's ID right? the database will be malfunction and might get errors in the future.

EDIT

So you wanted to get the author's id

const exists = await Marry.findOne({
   userID: message.author.id
})

if(!exist.userID === message.author.id) return message.reply("You already married!")

Reply if you get any error

EDIT

You can replace the code to this
if(!exist) return message.reply("You're already married! / This person is already married!")
新Acesyyy
  • 1,152
  • 1
  • 3
  • 22
  • Thanks, there are no more extra lines, now I get only the entire record from the database `{ userID: '654733308680201312', userMarryID: '654733308680201312', userPartnerID: '5134125460452801324', }` –  Apr 01 '22 at 09:07
  • But I only need the numbers of the first ID, is that possible? Or somehow a separate function to get it –  Apr 01 '22 at 09:07
  • Thank you, I understand you, I only compose userID and userPartnerID –  Apr 01 '22 at 09:08
  • What's is this for? `userPartnerID: { type: mongoose.SchemaTypes.String, required: true }` – 新Acesyyy Apr 01 '22 at 09:10
  • Since you already got the `userID` and `userMarryID` – 新Acesyyy Apr 01 '22 at 09:10
  • I understand you, I will leave only 2 IDs, now I want to get the userID so that I can check –  Apr 01 '22 at 09:12
  • See my edit post – 新Acesyyy Apr 01 '22 at 09:16
  • Thank you, I understood the principle, in my case it will be like this `exists.userID === message.author.id` thanks a lot!) –  Apr 01 '22 at 09:22
  • Let me know if you get some errors – 新Acesyyy Apr 01 '22 at 09:23
  • When there is no data in the database, I get this error, what can I do? `An error occurred while running the command: TypeError: Cannot read property 'userID' of null` –  Apr 01 '22 at 09:38