This commit is contained in:
grey-cat-1908 2023-06-27 16:33:35 +03:00
parent e7ed02dee0
commit 783c6cb9de
3 changed files with 45 additions and 2 deletions

View file

@ -1,8 +1,11 @@
import asyncio
import typing
import logging
from . import exceptions as bexc
_logger = logging.getLogger("boticord.autopost")
class AutoPost:
"""
@ -69,6 +72,8 @@ class AutoPost:
self._success = callback
return func
_logger.info("Registering success callback")
return inner
def on_error(self, callback: typing.Any = None):
@ -95,6 +100,8 @@ class AutoPost:
self._error = callback
return func
_logger.info("Registering error callback")
return inner
def init_stats(self, callback: typing.Any = None):
@ -120,6 +127,8 @@ class AutoPost:
self._stats = callback
return func
_logger.info("Registered stats initialization function")
return inner
@property
@ -150,6 +159,7 @@ class AutoPost:
stats = await self._stats()
try:
await self.client.http.post_bot_stats(self.bot_id, stats)
_logger.info("Tried to post bot stats")
except Exception as err:
on_error = getattr(self, "_error", None)
if on_error:
@ -189,6 +199,9 @@ class AutoPost:
task = asyncio.ensure_future(self._internal_loop())
self._task = task
_logger.info("Started autoposting")
return task
def stop(self) -> None:
@ -199,3 +212,5 @@ class AutoPost:
return None
self._stopped = True
_logger.info("Stopped autoposting")

View file

@ -1,10 +1,13 @@
import typing
import logging
from . import types as boticord_types
from .http import HttpClient
from .autopost import AutoPost
from .exceptions import MeilisearchException
_logger = logging.getLogger("boticord")
class BoticordClient:
"""Represents a client that can be used to interact with the BotiCord API.
@ -42,6 +45,8 @@ class BoticordClient:
:obj:`~.types.ResourceBot`:
ResourceBot object.
"""
_logger.info("Requesting information about bot")
response = await self.http.get_bot_info(bot_id)
return boticord_types.ResourceBot.from_dict(response)
@ -69,6 +74,8 @@ class BoticordClient:
:obj:`~.types.ResourceBot`:
ResourceBot object.
"""
_logger.info("Posting bot stats")
response = await self.http.post_bot_stats(
bot_id, {"servers": servers, "shards": shards, "users": users}
)
@ -87,6 +94,8 @@ class BoticordClient:
:obj:`~.types.ResourceServer`:
ResourceServer object.
"""
_logger.info("Requesting information about server")
response = await self.http.get_server_info(server_id)
return boticord_types.ResourceServer.from_dict(response)
@ -103,11 +112,13 @@ class BoticordClient:
:obj:`~.types.UserProfile`:
UserProfile object.
"""
_logger.info("Requesting information about user")
response = await self.http.get_user_info(user_id)
return boticord_types.UserProfile.from_dict(response)
async def __search_for(self, index, data):
"""Search for smth on BotiCord"""
"""Search for something on BotiCord"""
if self._meilisearch_api_key is None:
token_response = await self.http.get_search_key()
self._meilisearch_api_key = token_response["key"]
@ -138,6 +149,7 @@ class BoticordClient:
List[:obj:`~.types.MeiliIndexedBot`]:
List of found bots
"""
_logger.info("Searching for bots on BotiCord")
response = await self.__search_for("bots", kwargs)
return [boticord_types.MeiliIndexedBot.from_dict(bot) for bot in response]
@ -154,6 +166,7 @@ class BoticordClient:
List[:obj:`~.types.MeiliIndexedServer`]:
List of found servers
"""
_logger.info("Searching for servers on BotiCord")
response = await self.__search_for("servers", kwargs)
return [
@ -172,6 +185,7 @@ class BoticordClient:
List[:obj:`~.types.MeiliIndexedComment`]:
List of found comments
"""
_logger.info("Searching for comments on BotiCord")
response = await self.__search_for("comments", kwargs)
return [

View file

@ -24,7 +24,13 @@ class BotiCordWebsocket:
self._token = token
def listener(self):
"""Decorator to set the listener (must be a coroutine function).
"""Decorator to set the 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.
For example:
@ -39,6 +45,7 @@ class BotiCordWebsocket:
if not asyncio.iscoroutinefunction(func):
raise TypeError(f"<{func.__qualname__}> must be a coroutine function")
self._listeners[func.__qualname__] = func
_logger.debug(f"Listener {func.__qualname__} added successfully!")
return func
return inner
@ -51,11 +58,18 @@ class BotiCordWebsocket:
Type of notification (Check reference page)
callback (:obj:`function`)
Coroutine Callback Function
.. 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 not asyncio.iscoroutinefunction(callback):
raise TypeError(f"<{callback.__qualname__}> must be a coroutine function")
self._listeners[notification_type] = callback
_logger.debug(f"Listener {callback.__qualname__} added successfully!")
return self
async def connect(self) -> None: