| 3 | from helper.utils import send_log |
| 4 | |
| 5 | class Database: |
| 6 | def __init__(self, uri, database_name): |
| 7 | self._client = motor.motor_asyncio.AsyncIOMotorClient(uri) |
| 8 | self.db = self._client[database_name] |
| 9 | self.col = self.db.user |
| 10 | self.premium = self.db.premium |
| 11 | |
| 12 | def new_user(self, id): |
| 13 | return dict( |
| 14 | _id=int(id), |
| 15 | join_date=datetime.date.today().isoformat(), |
| 16 | file_id=None, |
| 17 | caption=None, |
| 18 | prefix=None, |
| 19 | suffix=None, |
| 20 | used_limit=0, |
| 21 | usertype="Free", |
| 22 | uploadlimit=Config.FREE_UPLOAD_LIMIT, |
| 23 | daily=0, |
| 24 | metadata_mode=False, |
| 25 | metadata_code="--change-title @TechifyBots\n--change-video-title @TechifyBots\n--change-audio-title @TechifyBots\n--change-subtitle-title @TechifyBots\n--change-author @TechifyBots", |
| 26 | expiry_time=None, |
| 27 | has_free_trial=False, |
| 28 | ban_status=dict( |
| 29 | is_banned=False, |
| 30 | ban_duration=0, |
| 31 | banned_on=datetime.date.max.isoformat(), |
| 32 | ban_reason='' |
| 33 | ) |
| 34 | ) |
| 35 | |
| 36 | async def add_user(self, b, m): |
| 37 | u = m.from_user |
| 38 | if not await self.is_user_exist(u.id): |
| 39 | user = self.new_user(u.id) |
| 40 | await self.col.insert_one(user) |
| 41 | await send_log(b, u) |
| 42 | |
| 43 | async def is_user_exist(self, id): |
| 44 | user = await self.col.find_one({'_id': int(id)}) |
| 45 | return bool(user) |
| 46 | |
| 47 | async def total_users_count(self): |
| 48 | count = await self.col.count_documents({}) |
| 49 | return count |
| 50 | |
| 51 | async def get_all_users(self): |
| 52 | all_users = self.col.find({}) |
| 53 | return all_users |
| 54 | |
| 55 | async def delete_user(self, user_id): |
| 56 | await self.col.delete_many({'_id': int(user_id)}) |
| 57 | |
| 58 | async def set_thumbnail(self, id, file_id): |
| 59 | await self.col.update_one({'_id': int(id)}, {'$set': {'file_id': file_id}}) |
| 60 | |
| 61 | async def get_thumbnail(self, id): |
| 62 | user = await self.col.find_one({'_id': int(id)}) |