pizda-bot/mongo.py
emmatveev 7b77ca2f56
All checks were successful
Deploy Dev / Build (pull_request) Successful in 5s
Deploy Dev / Push (pull_request) Successful in 8s
Deploy Dev / Deploy dev (pull_request) Successful in 13s
try
2024-11-17 12:58:09 +03:00

43 lines
1.4 KiB
Python

import pymongo
import settings
class Mongo:
def __init__(self):
url = f"mongodb://{settings.MONGO_USER}:{settings.MONGO_PASSWORD}@{settings.MONGO_HOST}:27017/"
self.client = pymongo.MongoClient(url)
self.database = self.client.get_database("pizda-bot")
self.chats_collection.create_index([
("chat_id", 1)
])
self.counter_collection.create_index([
("chat_id", 1),
("username", 1),
])
def __getitem__(self, item):
return self.database.get_collection(item)
@property
def chats_collection(self):
return self["chats"]
@property
def counter_collection(self):
return self["counter"]
def inc(self, username, chat_id):
if self.counter_collection.find_one({"chat_id": chat_id, "username": username}):
self.counter_collection.update_one({"chat_id": chat_id, "username": username}, {"$inc": {"count": 1}})
else:
self.counter_collection.insert_one({"chat_id": chat_id, "username": username, "count": 1})
def inc_points(self, username, chat_id):
if self.counter_collection.find_one({"chat_id": chat_id, "username": username}):
self.counter_collection.update_one({"chat_id": chat_id, "username": username}, {"$inc": {"points": 1}})
else:
self.counter_collection.insert_one({"chat_id": chat_id, "username": username, "points": 1})
mongo = Mongo()