Migrate to async_pymongo

This commit is contained in:
2023-08-14 14:52:02 +02:00
parent 5b56919b80
commit cd26990b7e
7 changed files with 23 additions and 31 deletions

View File

@@ -141,7 +141,7 @@ class PyroClient(PyroClient):
async def submit_media(
self, id: str
) -> Tuple[Union[Message, None], Union[str, None]]:
db_entry = col_submitted.find_one({"_id": ObjectId(id)})
db_entry = await col_submitted.find_one({"_id": ObjectId(id)})
submission = None
if db_entry is None:
@@ -226,7 +226,7 @@ class PyroClient(PyroClient):
)
raise SubmissionDuplicatesError(str(filepath), duplicates)
col_submitted.find_one_and_update(
await col_submitted.find_one_and_update(
{"_id": ObjectId(id)}, {"$set": {"done": True}}
)
@@ -258,12 +258,12 @@ class PyroClient(PyroClient):
* `PyroUser`: PyroUser object
"""
if (
col_users.find_one(
await col_users.find_one(
{"id": user.id if isinstance(user, User) else user}
) # type: ignore
is None
):
col_users.insert_one(
await col_users.insert_one(
{
"id": user.id if isinstance(user, User) else user,
"locale": user.language_code if isinstance(user, User) else None,
@@ -273,7 +273,7 @@ class PyroClient(PyroClient):
}
) # type: ignore
db_record = col_users.find_one(
db_record = await col_users.find_one(
{"id": user.id if isinstance(user, User) else user}
) # type: ignore

View File

@@ -20,19 +20,19 @@ class PyroUser:
cooldown: datetime
subscription: dict
async def update_locale(self, locale: str):
col_users.update_one({"_id": self._id}, {"$set": {"locale": locale}})
async def update_locale(self, locale: str) -> None:
await col_users.update_one({"_id": self._id}, {"$set": {"locale": locale}})
async def update_cooldown(self, time: datetime = datetime.now()):
col_users.update_one({"_id": self._id}, {"$set": {"cooldown": time}})
async def update_cooldown(self, time: datetime = datetime.now()) -> None:
await col_users.update_one({"_id": self._id}, {"$set": {"cooldown": time}})
async def block(self) -> None:
"""Ban user from using command and submitting content."""
col_users.update_one({"_id": self._id}, {"$set": {"banned": True}})
await col_users.update_one({"_id": self._id}, {"$set": {"banned": True}})
async def unblock(self) -> None:
"""Allow user to use command and submit posts again."""
col_users.update_one({"_id": self._id}, {"$set": {"banned": False}})
await col_users.update_one({"_id": self._id}, {"$set": {"banned": False}})
async def is_limited(self, app: Union[PyroClient, None] = None) -> bool:
"""Check if user is on a cooldown after submitting something.
@@ -41,11 +41,9 @@ class PyroUser:
`bool`: Must be `True` if on the cooldown and `False` if not
"""
admins = (
app.admins
if app is not None
else (
await config_get("admins", "bot") + [await config_get("owner", "bot")]
)
await config_get("admins", "bot") + [await config_get("owner", "bot")]
if app is None
else app.admins
)
return (datetime.now() - self.cooldown).total_seconds() < (