From 7409b21fab337181f3a9aee41e6d919775cf2934 Mon Sep 17 00:00:00 2001 From: MadCat9958 Date: Sat, 1 Jul 2023 22:49:28 +0300 Subject: [PATCH 01/14] Optional members and shards --- boticordpy/client.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/boticordpy/client.py b/boticordpy/client.py index 8cf5726..f6c7107 100644 --- a/boticordpy/client.py +++ b/boticordpy/client.py @@ -55,8 +55,8 @@ class BoticordClient: bot_id: typing.Union[str, int], *, servers: int = 0, - shards: int = 0, - users: int = 0, + shards: typing.Optional[int] = None, + users: typing.Optional[int] = None, ) -> boticord_types.ResourceBot: """Post Bot's stats. @@ -65,9 +65,9 @@ class BoticordClient: Id of the bot to post stats of. servers ( :obj:`int` ) Bot's servers count - shards ( :obj:`int` ) + shards ( Optional[:obj:`int`] ) Bot's shards count - users ( :obj:`int` ) + users ( Optional[:obj:`int`] ) Bot's users count Returns: From 2f7f1a52002f9854e492027d8f95278c6329bb19 Mon Sep 17 00:00:00 2001 From: MadCat9958 Date: Sun, 2 Jul 2023 21:20:31 +0300 Subject: [PATCH 02/14] servers also optional --- boticordpy/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/boticordpy/client.py b/boticordpy/client.py index f6c7107..768919b 100644 --- a/boticordpy/client.py +++ b/boticordpy/client.py @@ -54,7 +54,7 @@ class BoticordClient: self, bot_id: typing.Union[str, int], *, - servers: int = 0, + servers: typing.Optional[int] = None, shards: typing.Optional[int] = None, users: typing.Optional[int] = None, ) -> boticord_types.ResourceBot: @@ -63,7 +63,7 @@ class BoticordClient: Args: bot_id (Union[:obj:`str`, :obj:`int`]) Id of the bot to post stats of. - servers ( :obj:`int` ) + servers ( Optional[:obj:`int`] ) Bot's servers count shards ( Optional[:obj:`int`] ) Bot's shards count From e9bb850f7363e8e88cdb19511ace02b20178bec2 Mon Sep 17 00:00:00 2001 From: MadCat9958 Date: Sun, 2 Jul 2023 21:35:10 +0300 Subject: [PATCH 03/14] Global listener --- boticordpy/websocket.py | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/boticordpy/websocket.py b/boticordpy/websocket.py index 3b2bb70..e016584 100644 --- a/boticordpy/websocket.py +++ b/boticordpy/websocket.py @@ -19,9 +19,59 @@ class BotiCordWebsocket: self.loop = asyncio.get_event_loop() self.ws = None self._listeners = {} + self._global_listener = None self.not_closed = True self._token = token + + def global_listener(self): + """Decorator to set the global listener. It handles all notifications that + don't have their listeners. + + .. warning:: + + Callback functions must be a **coroutine**. If they aren't, then you might get unexpected + errors. In order to turn a function into a coroutine they must be ``async def`` + functions. + + For example: + + .. code-block:: python + + @websocket.global_listener() + async def all_notifications(data): + pass + """ + + def inner(func): + if not asyncio.iscoroutinefunction(func): + raise TypeError(f"<{func.__qualname__}> must be a coroutine function") + self._global_listener = func + _logger.debug(f"Global listener {func.__qualname__} added successfully!") + return func + + return inner + + def register_global_listener(self, callback: typing.Union[typing.Any, None]): + """Method to set the global listener. It handles all notifications that + don't have their listeners. + + Args: + callback (:obj:`function`) + Coroutine Callback Function. Set ``None`` if you need to remove global listener. + + .. warning:: + + Callback functions must be a **coroutine**. If they aren't, then you might get unexpected + errors. In order to turn a function into a coroutine they must be ``async def`` + functions. + """ + if callback is not None and not asyncio.iscoroutinefunction(callback): + raise TypeError(f"<{callback.__qualname__}> must be a coroutine function") + + self._global_listener = callback + _logger.debug(f"Listener {callback.__qualname__} added successfully!") + return self def listener(self): """Decorator to set the listener. @@ -118,6 +168,8 @@ class BotiCordWebsocket: listener = self._listeners.get(data["data"]["type"]) if listener: self.loop.create_task(listener(data["data"])) + else: + self.loop.create_task(self._global_listener(data['data'])) elif data["event"] == "pong": _logger.info("Received pong-response.") self.loop.create_task(self._send_ping()) From e4a602af2045944504dbe3cbc7f9ef824099c6b1 Mon Sep 17 00:00:00 2001 From: MadCat9958 Date: Sun, 2 Jul 2023 22:12:05 +0300 Subject: [PATCH 04/14] global_listener docs + more notify types --- docs/source/websocket.rst | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/docs/source/websocket.rst b/docs/source/websocket.rst index 5fcc5ec..2c14bdb 100644 --- a/docs/source/websocket.rst +++ b/docs/source/websocket.rst @@ -13,10 +13,26 @@ BotiCord Websocket .. automethod:: BotiCordWebsocket.listener() :decorator: + + .. automethod:: BotiCordWebsocket.global_listener() + :decorator: Notification types ------------------- -.. function:: comment_removed(data) +.. function:: up_added(data) + + Called when up is added. + +.. function:: comment_added(data) + + Called when comment is added. + +.. function:: comment_edited(data) + + Called when comment is deleted. - Called when comment is deleted. \ No newline at end of file +.. function:: comment_removed(data) + + Called when comment is deleted. + From 2a3b4c4af6d729f307b534d0b39a65f077de8d3f Mon Sep 17 00:00:00 2001 From: MadCat9958 Date: Sun, 2 Jul 2023 22:13:36 +0300 Subject: [PATCH 05/14] Bump version to 3.0.1 --- boticordpy/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boticordpy/__init__.py b/boticordpy/__init__.py index 596f461..a850f19 100644 --- a/boticordpy/__init__.py +++ b/boticordpy/__init__.py @@ -10,7 +10,7 @@ __title__ = "boticordpy" __author__ = "Marakarka" __license__ = "MIT" __copyright__ = "Copyright 2021 - 2023 Marakarka" -__version__ = "3.0.0" +__version__ = "3.0.1" from .client import BoticordClient from .types import * From 5511fd781807f574f82b87505f2832ba586ae781 Mon Sep 17 00:00:00 2001 From: MadCat9958 Date: Sun, 2 Jul 2023 22:16:53 +0300 Subject: [PATCH 06/14] fix: checking is global_listener None --- boticordpy/websocket.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boticordpy/websocket.py b/boticordpy/websocket.py index e016584..940f2fd 100644 --- a/boticordpy/websocket.py +++ b/boticordpy/websocket.py @@ -168,7 +168,7 @@ class BotiCordWebsocket: listener = self._listeners.get(data["data"]["type"]) if listener: self.loop.create_task(listener(data["data"])) - else: + elif self._global_listener: self.loop.create_task(self._global_listener(data['data'])) elif data["event"] == "pong": _logger.info("Received pong-response.") From c3a2692985d87c7c3bf568e86e7d9466d62f8771 Mon Sep 17 00:00:00 2001 From: MadCat9958 Date: Sun, 2 Jul 2023 22:57:41 +0300 Subject: [PATCH 07/14] suka, guilds or servers? --- README.md | 2 +- boticordpy/client.py | 2 +- examples/autopost.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c5287f6..c74e5d8 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ bot = commands.Bot(command_prefix="!") # Function that will return the current bot's stats. async def get_stats(): - return {"guilds": len(bot.guilds), "shards": 0, "members": len(bot.users)} + return {"servers": len(bot.guilds), "shards": 0, "members": len(bot.users)} # Function that will be called if stats are posted successfully. diff --git a/boticordpy/client.py b/boticordpy/client.py index 768919b..51f4e1d 100644 --- a/boticordpy/client.py +++ b/boticordpy/client.py @@ -77,7 +77,7 @@ class BoticordClient: _logger.info("Posting bot stats") response = await self.http.post_bot_stats( - bot_id, {"guilds": servers, "shards": shards, "members": users} + bot_id, {"servers": servers, "shards": shards, "members": users} ) return boticord_types.ResourceBot.from_dict(response) diff --git a/examples/autopost.py b/examples/autopost.py index 1239635..57afe50 100644 --- a/examples/autopost.py +++ b/examples/autopost.py @@ -10,7 +10,7 @@ bot = commands.Bot(command_prefix="!") # Function that will return the current bot's stats. async def get_stats(): - return {"guilds": len(bot.guilds), "shards": 0, "members": len(bot.users)} + return {"servers": len(bot.guilds), "shards": 0, "members": len(bot.users)} # Function that will be called if stats are posted successfully. From 395bb0f0c18688acc6202d9ad6f8c3cec128a72f Mon Sep 17 00:00:00 2001 From: MadCat9958 Date: Sun, 2 Jul 2023 23:01:48 +0300 Subject: [PATCH 08/14] not_closed = True after init??? --- boticordpy/websocket.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boticordpy/websocket.py b/boticordpy/websocket.py index 940f2fd..5a14990 100644 --- a/boticordpy/websocket.py +++ b/boticordpy/websocket.py @@ -20,7 +20,7 @@ class BotiCordWebsocket: self.ws = None self._listeners = {} self._global_listener = None - self.not_closed = True + self.not_closed = False self._token = token From 66685f5f6b7811a96f2415d2d1d853d63a09132a Mon Sep 17 00:00:00 2001 From: MadCat9958 Date: Mon, 3 Jul 2023 11:00:40 +0300 Subject: [PATCH 09/14] data logging --- boticordpy/websocket.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boticordpy/websocket.py b/boticordpy/websocket.py index 5a14990..11c18e4 100644 --- a/boticordpy/websocket.py +++ b/boticordpy/websocket.py @@ -174,7 +174,7 @@ class BotiCordWebsocket: _logger.info("Received pong-response.") self.loop.create_task(self._send_ping()) else: - _logger.error("An error has occurred.") + _logger.error(f"An error has occurred.\nData: {data}") async def _handle_close(self, code: int) -> None: self.not_closed = False From f6f3990d04c01c714fc14ca9b4404c11ffddb124 Mon Sep 17 00:00:00 2001 From: MadCat9958 Date: Mon, 3 Jul 2023 19:58:16 +0300 Subject: [PATCH 10/14] remove shit --- boticordpy/websocket.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boticordpy/websocket.py b/boticordpy/websocket.py index 11c18e4..92729bd 100644 --- a/boticordpy/websocket.py +++ b/boticordpy/websocket.py @@ -174,7 +174,7 @@ class BotiCordWebsocket: _logger.info("Received pong-response.") self.loop.create_task(self._send_ping()) else: - _logger.error(f"An error has occurred.\nData: {data}") + _logger.error(f"An error has occurred.") async def _handle_close(self, code: int) -> None: self.not_closed = False From 737b77a99b90f6e82b15c2136df525a82d5f7909 Mon Sep 17 00:00:00 2001 From: MadCat9958 Date: Mon, 3 Jul 2023 19:58:25 +0300 Subject: [PATCH 11/14] hotfix --- boticordpy/http.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boticordpy/http.py b/boticordpy/http.py index 4a62222..e9e15a4 100644 --- a/boticordpy/http.py +++ b/boticordpy/http.py @@ -22,7 +22,7 @@ class HttpClient: def __init__(self, auth_token: str = None, version: int = 3, **kwargs): self.token = auth_token - self.API_URL = f"https://api.boticord.top/v{version}" + self.API_URL = f"https://api.boticord.top/v{version}/" loop = kwargs.get("loop") or asyncio.get_event_loop() From 5deb963cfeeab844d07bb70748465a5cd4ef28b1 Mon Sep 17 00:00:00 2001 From: MadCat9958 Date: Tue, 4 Jul 2023 12:49:30 +0300 Subject: [PATCH 12/14] Revert "Global listener" This reverts commit e9bb850f7363e8e88cdb19511ace02b20178bec2. --- boticordpy/websocket.py | 53 ----------------------------------------- 1 file changed, 53 deletions(-) diff --git a/boticordpy/websocket.py b/boticordpy/websocket.py index 92729bd..fe9fd92 100644 --- a/boticordpy/websocket.py +++ b/boticordpy/websocket.py @@ -19,59 +19,8 @@ class BotiCordWebsocket: self.loop = asyncio.get_event_loop() self.ws = None self._listeners = {} - self._global_listener = None self.not_closed = False - self._token = token - - def global_listener(self): - """Decorator to set the global listener. It handles all notifications that - don't have their listeners. - - .. warning:: - - Callback functions must be a **coroutine**. If they aren't, then you might get unexpected - errors. In order to turn a function into a coroutine they must be ``async def`` - functions. - - For example: - - .. code-block:: python - - @websocket.global_listener() - async def all_notifications(data): - pass - """ - - def inner(func): - if not asyncio.iscoroutinefunction(func): - raise TypeError(f"<{func.__qualname__}> must be a coroutine function") - self._global_listener = func - _logger.debug(f"Global listener {func.__qualname__} added successfully!") - return func - - return inner - - def register_global_listener(self, callback: typing.Union[typing.Any, None]): - """Method to set the global listener. It handles all notifications that - don't have their listeners. - - Args: - callback (:obj:`function`) - Coroutine Callback Function. Set ``None`` if you need to remove global listener. - - .. warning:: - - Callback functions must be a **coroutine**. If they aren't, then you might get unexpected - errors. In order to turn a function into a coroutine they must be ``async def`` - functions. - """ - if callback is not None and not asyncio.iscoroutinefunction(callback): - raise TypeError(f"<{callback.__qualname__}> must be a coroutine function") - - self._global_listener = callback - _logger.debug(f"Listener {callback.__qualname__} added successfully!") - return self def listener(self): """Decorator to set the listener. @@ -168,8 +117,6 @@ class BotiCordWebsocket: listener = self._listeners.get(data["data"]["type"]) if listener: self.loop.create_task(listener(data["data"])) - elif self._global_listener: - self.loop.create_task(self._global_listener(data['data'])) elif data["event"] == "pong": _logger.info("Received pong-response.") self.loop.create_task(self._send_ping()) From 40bf6549a4df3cf801fa75b1f11e69b1f2d238c6 Mon Sep 17 00:00:00 2001 From: MadCat9958 Date: Tue, 4 Jul 2023 12:51:47 +0300 Subject: [PATCH 13/14] Global listener? What is this? --- docs/source/websocket.rst | 3 --- 1 file changed, 3 deletions(-) diff --git a/docs/source/websocket.rst b/docs/source/websocket.rst index 2c14bdb..9b06327 100644 --- a/docs/source/websocket.rst +++ b/docs/source/websocket.rst @@ -13,9 +13,6 @@ BotiCord Websocket .. automethod:: BotiCordWebsocket.listener() :decorator: - - .. automethod:: BotiCordWebsocket.global_listener() - :decorator: Notification types From cbde8ea727ac78b1ff3b90deb720a000c543163e Mon Sep 17 00:00:00 2001 From: MadCat9958 Date: Tue, 4 Jul 2023 12:55:57 +0300 Subject: [PATCH 14/14] typos --- boticordpy/websocket.py | 2 +- docs/source/websocket.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/boticordpy/websocket.py b/boticordpy/websocket.py index fe9fd92..977fdc1 100644 --- a/boticordpy/websocket.py +++ b/boticordpy/websocket.py @@ -121,7 +121,7 @@ class BotiCordWebsocket: _logger.info("Received pong-response.") self.loop.create_task(self._send_ping()) else: - _logger.error(f"An error has occurred.") + _logger.error("An error has occurred.") async def _handle_close(self, code: int) -> None: self.not_closed = False diff --git a/docs/source/websocket.rst b/docs/source/websocket.rst index 9b06327..00ec82e 100644 --- a/docs/source/websocket.rst +++ b/docs/source/websocket.rst @@ -27,7 +27,7 @@ Notification types .. function:: comment_edited(data) - Called when comment is deleted. + Called when comment is edited. .. function:: comment_removed(data)