Home > Back-end >  discord.js-commando enabling and working with mongodb
discord.js-commando enabling and working with mongodb

Time:04-02

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("           
  • Related