Skip to content

Commit

Permalink
Added Double DB Support
Browse files Browse the repository at this point in the history
Added Double DB Support. From now on, you can use two MongoDB databases for your bot. Currently it's been on testing. If you found any bugs or errors, report it on our support group using a hash tag #BugsFound
  • Loading branch information
Joelkb committed Mar 14, 2024
1 parent 6405906 commit d2121e4
Show file tree
Hide file tree
Showing 13 changed files with 499 additions and 140 deletions.
15 changes: 13 additions & 2 deletions Script.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,22 @@ class script(object):
• /delallg - <code>Tᴏ ᴅᴇʟᴇᴛᴇ ᴀʟʟ Gғɪʟᴛᴇʀs ғʀᴏᴍ ᴛʜᴇ ʙᴏᴛ's ᴅᴀᴛᴀʙᴀsᴇ.</code>
• /deletefiles - <code>Tᴏ ᴅᴇʟᴇᴛᴇ CᴀᴍRɪᴘ ᴀɴᴅ PʀᴇDVD Fɪʟᴇs ғʀᴏᴍ ᴛʜᴇ ʙᴏᴛ's ᴅᴀᴛᴀʙᴀsᴇ.</code>"""

STATUS_TXT = """<b>★ Tᴏᴛᴀʟ Fɪʟᴇs: <code>{}</code>
STATUS_TXT = """<b>Tᴏᴛᴀʟ Fɪʟᴇs Fʀᴏᴍ Bᴏᴛʜ DBs: <code>{}</code>
Bᴏᴛ Usᴇʀs ᴀɴᴅ Cʜᴀᴛs Cᴏᴜɴᴛ
★ Tᴏᴛᴀʟ Usᴇʀs: <code>{}</code>
★ Tᴏᴛᴀʟ Cʜᴀᴛs: <code>{}</code>
Pʀɪᴍᴀʀʏ Dᴀᴛᴀʙᴀsᴇ Sᴛᴀᴛɪsᴛɪᴄs
★ Tᴏᴛᴀʟ Fɪʟᴇs: <code>{}</code>
★ Usᴇᴅ Sᴛᴏʀᴀɢᴇ: <code>{} MB</code>
★ Fʀᴇᴇ Sᴛᴏʀᴀɢᴇ: <code>{} MB</code>
Sᴇᴄᴏɴᴅᴀʀʏ Dᴀᴛᴀʙᴀsᴇ Sᴛᴀᴛɪsᴛɪᴄs
★ Tᴏᴛᴀʟ Fɪʟᴇs: <code>{}</code>
★ Usᴇᴅ Sᴛᴏʀᴀɢᴇ: <code>{} MB</code>
★ Fʀᴇᴇ Sᴛᴏʀᴀɢᴇ: <code>{} MB</code></b>"""
★ Fʀᴇᴇ Sᴛᴏʀᴀɢᴇ: <code>{} MB</code>
</b>"""

LOG_TEXT_G = """#NewGroup
Gʀᴏᴜᴘ = {}(<code>{}</code>)
Expand Down
19 changes: 17 additions & 2 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@

from pyrogram import Client, __version__
from pyrogram.raw.all import layer
from database.ia_filterdb import Media
from database.ia_filterdb import Media, Media2, choose_mediaDB, db as clientDB
from database.users_chats_db import db
from info import SESSION, API_ID, API_HASH, BOT_TOKEN, LOG_STR, LOG_CHANNEL, PORT
from info import SESSION, API_ID, API_HASH, BOT_TOKEN, LOG_STR, LOG_CHANNEL, SECONDDB_URI
from utils import temp
from typing import Union, Optional, AsyncGenerator
from pyrogram import types
from Script import script
from datetime import date, datetime
import pytz
from sample_info import tempDict

class Bot(Client):

Expand All @@ -38,6 +39,20 @@ async def start(self):
temp.BANNED_CHATS = b_chats
await super().start()
await Media.ensure_indexes()
await Media2.ensure_indexes()
#choose the right db by checking the free space
stats = await clientDB.command('dbStats')
#calculating the free db space from bytes to MB
free_dbSize = round(512-((stats['dataSize']/(1024*1024))+(stats['indexSize']/(1024*1024))), 2)
if SECONDDB_URI and free_dbSize<10: #if the primary db have less than 10MB left, use second DB.
tempDict["indexDB"] = SECONDDB_URI
logging.info(f"Since Primary DB have only {free_dbSize} MB left, Secondary DB will be used to store datas.")
elif SECONDDB_URI is None:
logging.error("Missing second DB URI !\n\nAdd SECONDDB_URI now !\n\nExiting...")
exit()
else:
logging.info(f"Since primary DB have enough space ({free_dbSize}MB) left, It will be used for storing datas.")
await choose_mediaDB()
me = await self.get_me()
temp.ME = me.id
temp.U_NAME = me.username
Expand Down
144 changes: 106 additions & 38 deletions database/connections_mdb.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import pymongo

from info import DATABASE_URI, DATABASE_NAME
from sample_info import tempDict
from info import DATABASE_URI, DATABASE_NAME, SECONDDB_URI

import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.ERROR)

myclient = pymongo.MongoClient(DATABASE_URI)
mydb = myclient[DATABASE_NAME]
mycol = mydb['CONNECTION']
mycol = mydb['CONNECTION']

myclient2 = pymongo.MongoClient(SECONDDB_URI)
mydb2 = myclient2[DATABASE_NAME]
mycol2 = mydb2['CONNECTION']

async def add_connection(group_id, user_id):
query = mycol.find_one(
Expand All @@ -20,7 +23,7 @@ async def add_connection(group_id, user_id):
group_ids = [x["group_id"] for x in query["group_details"]]
if group_id in group_ids:
return False

group_details = {
"group_id" : group_id
}
Expand All @@ -31,23 +34,37 @@ async def add_connection(group_id, user_id):
'active_group' : group_id,
}

if mycol.count_documents( {"_id": user_id} ) == 0:
if mycol.count_documents( {"_id": user_id} ) == 0 and mycol2.count_documents( {"_id": user_id} ) == 0:
try:
mycol.insert_one(data)
return True
if tempDict['indexDB'] == DATABASE_URI:
mycol.insert_one(data)
return True
else:
mycol2.insert_one(data)
return True
except:
logger.exception('Some error occurred!', exc_info=True)

else:
try:
mycol.update_one(
{'_id': user_id},
{
"$push": {"group_details": group_details},
"$set": {"active_group" : group_id}
}
)
return True
if mycol.count_documents( {"_id": user_id} ) == 0:
mycol2.update_one(
{'_id': user_id},
{
"$push": {"group_details": group_details},
"$set": {"active_group" : group_id}
}
)
return True
else:
mycol.update_one(
{'_id': user_id},
{
"$push": {"group_details": group_details},
"$set": {"active_group" : group_id}
}
)
return True
except:
logger.exception('Some error occurred!', exc_info=True)

Expand All @@ -58,20 +75,32 @@ async def active_connection(user_id):
{ "_id": user_id },
{ "_id": 0, "group_details": 0 }
)
if not query:
query2 = mycol2.find_one(
{ "_id": user_id },
{ "_id": 0, "group_details": 0 }
)
if not query and not query2:
return None

group_id = query['active_group']
return int(group_id) if group_id != None else None

elif query:
group_id = query['active_group']
return int(group_id) if group_id != None else None
else:
group_id = query2['active_group']
return int(group_id) if group_id != None else None

async def all_connections(user_id):
query = mycol.find_one(
{ "_id": user_id },
{ "_id": 0, "active_group": 0 }
)
query2 = mycol2.find_one(
{ "_id": user_id },
{ "_id": 0, "active_group": 0 }
)
if query is not None:
return [x["group_id"] for x in query["group_details"]]
elif query2 is not None:
return [x["group_id"] for x in query2["group_details"]]
else:
return None

Expand All @@ -81,6 +110,11 @@ async def if_active(user_id, group_id):
{ "_id": user_id },
{ "_id": 0, "group_details": 0 }
)
if query is None:
query = mycol2.find_one(
{ "_id": user_id },
{ "_id": 0, "group_details": 0 }
)
return query is not None and query['active_group'] == group_id


Expand All @@ -89,6 +123,11 @@ async def make_active(user_id, group_id):
{'_id': user_id},
{"$set": {"active_group" : group_id}}
)
if update.modified_count == 0:
update = mycol2.update_one(
{'_id': user_id},
{"$set": {"active_group" : group_id}}
)
return update.modified_count != 0


Expand All @@ -97,6 +136,11 @@ async def make_inactive(user_id):
{'_id': user_id},
{"$set": {"active_group" : None}}
)
if update.modified_count == 0:
update = mycol2.update_one(
{'_id': user_id},
{"$set": {"active_group" : None}}
)
return update.modified_count != 0


Expand All @@ -108,26 +152,50 @@ async def delete_connection(user_id, group_id):
{"$pull" : { "group_details" : {"group_id":group_id} } }
)
if update.modified_count == 0:
return False
query = mycol.find_one(
{ "_id": user_id },
{ "_id": 0 }
)
if len(query["group_details"]) >= 1:
if query['active_group'] == group_id:
prvs_group_id = query["group_details"][len(query["group_details"]) - 1]["group_id"]

mycol.update_one(
{'_id': user_id},
{"$set": {"active_group" : prvs_group_id}}
update = mycol2.update_one(
{"_id": user_id},
{"$pull" : { "group_details" : {"group_id":group_id} } }
)
if update.modified_count == 0:
return False
else:
query = mycol2.find_one(
{ "_id": user_id },
{ "_id": 0 }
)
if len(query["group_details"]) >= 1:
if query['active_group'] == group_id:
prvs_group_id = query["group_details"][len(query["group_details"]) - 1]["group_id"]

mycol2.update_one(
{'_id': user_id},
{"$set": {"active_group" : prvs_group_id}}
)
else:
mycol2.update_one(
{'_id': user_id},
{"$set": {"active_group" : None}}
)
return True
else:
mycol.update_one(
{'_id': user_id},
{"$set": {"active_group" : None}}
query = mycol.find_one(
{ "_id": user_id },
{ "_id": 0 }
)
return True
if len(query["group_details"]) >= 1:
if query['active_group'] == group_id:
prvs_group_id = query["group_details"][len(query["group_details"]) - 1]["group_id"]

mycol.update_one(
{'_id': user_id},
{"$set": {"active_group" : prvs_group_id}}
)
else:
mycol.update_one(
{'_id': user_id},
{"$set": {"active_group" : None}}
)
return True
except Exception as e:
logger.exception(f'Some error occurred! {e}', exc_info=True)
return False

return False
Loading

0 comments on commit d2121e4

Please sign in to comment.