Olá! Recentemente no discord eu fiz um sistema de clans, usando o mongodb atlas, para aprender como ele funciona. O problema é que ele cria o schema no atlas certinho, mas dá o update no schema do usuário.
Aqui estão os dois Schemas:
const User = new mongoose.Schema({
_id: { type: String, required: true },
tag: { type: String, required: false },
clan: { type: Boolean, default: false },
clan_name: { type: String, default: 'The member has no clan.' },
coins: { type: Number, default: 0 },
wood: { type: Number, default: 0 },
rock: { type: Number, default: 0 },
gold: { type: Number, default: 0 },
});
const Clan = new mongoose.Schema({
_name: { type: String, required: true },
all: { type: Array, default: [] },
owner: { type: String, required: true },
co_owners: { type: Array, default: 'The clan doesn\'t have co-owners.' },
members: { type: Array, default: 'The clan doesn\'t have members.' },
bankLevel: { Type: Number, default: 0 },
bankGold: { Type: Number, default: 0 },
bankRock: { Type: Number, default: 0 },
bankWood: { type: Number, default: 0 },
members_counter: { type: Number, default: 0 }
});
Este é o código do comando para criar o clan:
const { Command } = require('discord-akairo');
const { RedEmbed, GreenEmbed } = require("../../Helpers/EmbedHelper");
const Database = require("../../Database");
class CreateClanCommand extends Command {
constructor() {
super('createClan', {
aliases: ['create-clan', 'criarClan', 'criar-clan'],
category: 'RPG',
args: [{ id: 'name' }]
});
}
async exec(message, args) {
await Database.Users.findOne({ '_id': message.author.id }, function (error, document) {
if (document) {
if (document.clan === true) {
return new RedEmbed('já tens um clan');
} else {
if (document.gold < 500) return new RedEmbed("n tens ouro", message);
if (!args.name) return new RedEmbed("põe um nome", message);
if (args.name.length > 15) return new RedEmbed("nome com + de 15 caractéres", message);
if (args.name.length < 5) return new RedEmbed("nome com - de caractéres", message);
let newClan = new Database.Clans({
_name: args.name,
all: [message.author.id],
owner: message.author.id,
co_owners: 'The clan doesn\'t have co-owners.',
members: 'The clan doesn\'t have members.',
bankLevel: 1,
bankGold: 0,
bankRock: 0,
bankWood: 0,
members_counter: 1
});
newClan.save();
new RedEmbed('criaste um clan', message)
let updateUser = new Database.Users({
_id: message.author.id,
tag: message.author.tag,
clan: true,
clan_name: 'The member has no clan.',
coins: 0,
wood: 0,
rock: 0,
gold: 0,
});
updateUser.save();
}
} else {
return new RedEmbed('n tens conta', message);
}
});
}
}
module.exports = CreateClanCommand;
E o erro fala sobre uma key duplicado, e eu não faço ideia como o resolver: