From 004b5336d8bf1db3a9a262fb32779eeae14f21dc Mon Sep 17 00:00:00 2001 From: profitroll Date: Tue, 22 Apr 2025 23:28:58 +0200 Subject: [PATCH] Fixed _set() and _remove() methods --- classes/pycord_event.py | 9 +++------ classes/pycord_event_stage.py | 9 +++------ classes/pycord_guild.py | 9 +++------ classes/pycord_user.py | 37 ++++++++++++++++++++--------------- 4 files changed, 30 insertions(+), 34 deletions(-) diff --git a/classes/pycord_event.py b/classes/pycord_event.py index a584df2..32518de 100644 --- a/classes/pycord_event.py +++ b/classes/pycord_event.py @@ -129,14 +129,12 @@ class PycordEvent: return cls(**db_entry) - # TODO Update the docstring async def _set(self, cache: Optional[Cache] = None, **kwargs) -> None: """Set attribute data and save it into the database. Args: - key (str): Attribute to change - value (Any): Value to set cache (:obj:`Cache`, optional): Cache engine to write the update into + **kwargs (str): Mapping of attribute names and respective values to be set """ for key, value in kwargs.items(): if not hasattr(self, key): @@ -150,13 +148,12 @@ class PycordEvent: logger.info("Set attributes of event %s to %s", self._id, kwargs) - # TODO Update the docstring - async def _remove(self, cache: Optional[Cache] = None, *args: str) -> None: + async def _remove(self, *args: str, cache: Optional[Cache] = None) -> None: """Remove attribute data and save it into the database. Args: - key (str): Attribute to remove cache (:obj:`Cache`, optional): Cache engine to write the update into + *args (str): List of attributes to remove """ attributes: Dict[str, Any] = {} diff --git a/classes/pycord_event_stage.py b/classes/pycord_event_stage.py index e7a17d7..82c84a5 100644 --- a/classes/pycord_event_stage.py +++ b/classes/pycord_event_stage.py @@ -106,14 +106,12 @@ class PycordEventStage: return cls(**db_entry) - # TODO Update the docstring async def _set(self, cache: Optional[Cache] = None, **kwargs) -> None: """Set attribute data and save it into the database. Args: - key (str): Attribute to change - value (Any): Value to set cache (:obj:`Cache`, optional): Cache engine to write the update into + **kwargs (str): Mapping of attribute names and respective values to be set """ for key, value in kwargs.items(): if not hasattr(self, key): @@ -127,13 +125,12 @@ class PycordEventStage: logger.info("Set attributes of event stage %s to %s", self._id, kwargs) - # TODO Update the docstring - async def _remove(self, cache: Optional[Cache] = None, *args: str) -> None: + async def _remove(self, *args: str, cache: Optional[Cache] = None) -> None: """Remove attribute data and save it into the database. Args: - key (str): Attribute to remove cache (:obj:`Cache`, optional): Cache engine to write the update into + *args (str): List of attributes to remove """ attributes: Dict[str, Any] = {} diff --git a/classes/pycord_guild.py b/classes/pycord_guild.py index f61dabe..aa37c54 100644 --- a/classes/pycord_guild.py +++ b/classes/pycord_guild.py @@ -67,14 +67,12 @@ class PycordGuild: return cls(**db_entry) - # TODO Update the docstring async def _set(self, cache: Optional[Cache] = None, **kwargs) -> None: """Set attribute data and save it into the database. Args: - key (str): Attribute to change - value (Any): Value to set cache (:obj:`Cache`, optional): Cache engine to write the update into + **kwargs (str): Mapping of attribute names and respective values to be set """ for key, value in kwargs.items(): if not hasattr(self, key): @@ -88,13 +86,12 @@ class PycordGuild: logger.info("Set attributes of guild %s to %s", self.id, kwargs) - # TODO Update the docstring - async def _remove(self, cache: Optional[Cache] = None, *args: str) -> None: + async def _remove(self, *args: str, cache: Optional[Cache] = None) -> None: """Remove attribute data and save it into the database. Args: - key (str): Attribute to remove cache (:obj:`Cache`, optional): Cache engine to write the update into + *args (str): List of attributes to remove """ attributes: Dict[str, Any] = {} diff --git a/classes/pycord_user.py b/classes/pycord_user.py index 16d7458..f3421b2 100644 --- a/classes/pycord_user.py +++ b/classes/pycord_user.py @@ -77,44 +77,49 @@ class PycordUser: "id": self.id, } - async def _set(self, key: str, value: Any, cache: Optional[Cache] = None) -> None: + async def _set(self, cache: Optional[Cache] = None, **kwargs) -> None: """Set attribute data and save it into the database. Args: - key (str): Attribute to change - value (Any): Value to set cache (:obj:`Cache`, optional): Cache engine to write the update into + **kwargs (str): Mapping of attribute names and respective values to be set """ - if not hasattr(self, key): - raise AttributeError() + for key, value in kwargs.items(): + if not hasattr(self, key): + raise AttributeError() - setattr(self, key, value) + setattr(self, key, value) - await self.__collection__.update_one({"_id": self._id}, {"$set": {key: value}}, upsert=True) + await self.__collection__.update_one({"_id": self._id}, {"$set": kwargs}, upsert=True) self._update_cache(cache) - logger.info("Set attribute '%s' of user %s to '%s'", key, self.id, value) + logger.info("Set attributes of user %s to %s", self.id, kwargs) - async def _remove(self, key: str, cache: Optional[Cache] = None) -> None: + async def _remove(self, *args: str, cache: Optional[Cache] = None) -> None: """Remove attribute data and save it into the database. Args: - key (str): Attribute to remove cache (:obj:`Cache`, optional): Cache engine to write the update into + *args (str): List of attributes to remove """ - if not hasattr(self, key): - raise AttributeError() + attributes: Dict[str, Any] = {} - default_value: Any = PycordUser.get_default_value(key) + for key in args: + if not hasattr(self, key): + raise AttributeError() - setattr(self, key, default_value) + default_value: Any = self.get_default_value(key) - await self.__collection__.update_one({"_id": self._id}, {"$set": {key: default_value}}, upsert=True) + setattr(self, key, default_value) + + attributes[key] = default_value + + await self.__collection__.update_one({"_id": self._id}, {"$set": attributes}, upsert=True) self._update_cache(cache) - logger.info("Removed attribute '%s' of user %s", key, self.id) + logger.info("Reset attributes %s of user %s to default values", args, self.id) def _get_cache_key(self) -> str: return f"{self.__short_name__}_{self.id}"