AnixartBot/models/user.js
2022-08-10 13:06:23 +02:00

31 lines
639 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const UserSchema = new Schema(
{
_id: {
type: Number,
required: true,
},
anixartId: {
type: Number,
required: true,
}
},
{ versionKey: false }
);
// Возвращает пользователя и создаёт его, если нет
UserSchema.statics.firstOrCreate = async function(tgUser, anixartId) {
let user = await this.findById(tgUser.id);
if (!user && !tgUser.is_bot) {
user = await new this({
_id: tgUser.id,
anixartId: anixartId
}).save();
}
return user;
};
module.exports = mongoose.model("users", UserSchema);