Compare commits
6 Commits
dae89fd1ac
...
v0.2.0
Author | SHA1 | Date | |
---|---|---|---|
4fc37f72f3 | |||
b9aeaf5c86 | |||
6087349622 | |||
3010dc02bc | |||
4afcbc93d5 | |||
72ccaa04a4 |
@@ -6,10 +6,11 @@ from bson import ObjectId
|
|||||||
from discord import User, Member
|
from discord import User, Member
|
||||||
from libbot.utils import config_get
|
from libbot.utils import config_get
|
||||||
from pymongo.results import InsertOneResult
|
from pymongo.results import InsertOneResult
|
||||||
|
from typing_extensions import deprecated
|
||||||
|
|
||||||
from classes.cache import HoloCache
|
from classes.cache import HoloCache
|
||||||
from errors import UserNotFoundError
|
from errors import UserNotFoundError
|
||||||
from modules.database import col_users
|
from modules.database import col_warnings, col_users
|
||||||
|
|
||||||
logger: Logger = logging.getLogger(__name__)
|
logger: Logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -70,7 +71,40 @@ class HoloUser:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def from_id(cls, user_id: int) -> "HoloUser":
|
async def from_id(cls, user_id: int) -> "HoloUser":
|
||||||
raise NotImplementedError()
|
return NotImplemented
|
||||||
|
|
||||||
|
# TODO Deprecate and remove warnings
|
||||||
|
@deprecated("Warnings are deprecated")
|
||||||
|
async def get_warnings(self) -> int:
|
||||||
|
"""Get number of warnings user has
|
||||||
|
|
||||||
|
### Returns:
|
||||||
|
* `int`: Number of warnings
|
||||||
|
"""
|
||||||
|
warns: Dict[str, Any] | None = await col_warnings.find_one({"id": self.id})
|
||||||
|
|
||||||
|
return 0 if warns is None else warns["warns"]
|
||||||
|
|
||||||
|
# TODO Deprecate and remove warnings
|
||||||
|
@deprecated("Warnings are deprecated")
|
||||||
|
async def warn(self, count: int = 1, reason: str = "Reason not provided") -> None:
|
||||||
|
"""Warn and add count to warns number
|
||||||
|
|
||||||
|
### Args:
|
||||||
|
* `count` (int, optional): Count of warnings to be added. Defaults to 1.
|
||||||
|
* `reason` (int, optional): Count of warnings to be added. Defaults to 1.
|
||||||
|
"""
|
||||||
|
warns: Dict[str, Any] | None = await col_warnings.find_one({"id": self.id})
|
||||||
|
|
||||||
|
if warns is not None:
|
||||||
|
await col_warnings.update_one(
|
||||||
|
{"_id": self._id},
|
||||||
|
{"$set": {"warns": warns["warns"] + count}},
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
await col_warnings.insert_one(document={"id": self.id, "warns": count})
|
||||||
|
|
||||||
|
logger.info("User %s was warned %s times due to: %s", self.id, count, reason)
|
||||||
|
|
||||||
async def _set(self, key: str, value: Any, cache: HoloCache | None = None) -> None:
|
async def _set(self, key: str, value: Any, cache: HoloCache | None = None) -> None:
|
||||||
"""Set attribute data and save it into the database
|
"""Set attribute data and save it into the database
|
||||||
@@ -175,7 +209,7 @@ class HoloUser:
|
|||||||
await self._remove("custom_role", cache=cache)
|
await self._remove("custom_role", cache=cache)
|
||||||
|
|
||||||
async def purge(self, cache: HoloCache | None = None) -> None:
|
async def purge(self, cache: HoloCache | None = None) -> None:
|
||||||
"""Completely remove user data from database. Only removes the user record from users collection.
|
"""Completely remove user data from database. Will not remove transactions logs and warnings.
|
||||||
|
|
||||||
### Args:
|
### Args:
|
||||||
* `cache` (HoloCache | None, optional): Cache engine to write the update into
|
* `cache` (HoloCache | None, optional): Cache engine to write the update into
|
||||||
|
@@ -29,12 +29,14 @@ db_client_sync: MongoClient = MongoClient(con_string)
|
|||||||
db: AsyncDatabase = db_client.get_database(name=db_config["name"])
|
db: AsyncDatabase = db_client.get_database(name=db_config["name"])
|
||||||
|
|
||||||
col_users: AsyncCollection = db.get_collection("users")
|
col_users: AsyncCollection = db.get_collection("users")
|
||||||
|
col_warnings: AsyncCollection = db.get_collection("warnings")
|
||||||
col_analytics: AsyncCollection = db.get_collection("analytics")
|
col_analytics: AsyncCollection = db.get_collection("analytics")
|
||||||
|
|
||||||
# Sync declarations as a fallback
|
# Sync declarations as a fallback
|
||||||
sync_db: Database = db_client_sync.get_database(name=db_config["name"])
|
sync_db: Database = db_client_sync.get_database(name=db_config["name"])
|
||||||
|
|
||||||
sync_col_users: Collection = sync_db.get_collection("users")
|
sync_col_users: Collection = sync_db.get_collection("users")
|
||||||
|
sync_col_warnings: Collection = sync_db.get_collection("warnings")
|
||||||
sync_col_analytics: Collection = sync_db.get_collection("analytics")
|
sync_col_analytics: Collection = sync_db.get_collection("analytics")
|
||||||
|
|
||||||
# Update indexes
|
# Update indexes
|
||||||
|
18
validation/warnings.json
Normal file
18
validation/warnings.json
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"$jsonSchema": {
|
||||||
|
"required": [
|
||||||
|
"user_id",
|
||||||
|
"warnings"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"user_id": {
|
||||||
|
"bsonType": "long",
|
||||||
|
"description": "Discord ID of user"
|
||||||
|
},
|
||||||
|
"warnings": {
|
||||||
|
"bsonType": "int",
|
||||||
|
"description": "Number of warnings on count"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user