mirror of
https://github.com/boticord/boticordpy.git
synced 2024-11-11 19:07:29 +03:00
logging
This commit is contained in:
parent
e7ed02dee0
commit
783c6cb9de
3 changed files with 45 additions and 2 deletions
|
@ -1,8 +1,11 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import typing
|
import typing
|
||||||
|
import logging
|
||||||
|
|
||||||
from . import exceptions as bexc
|
from . import exceptions as bexc
|
||||||
|
|
||||||
|
_logger = logging.getLogger("boticord.autopost")
|
||||||
|
|
||||||
|
|
||||||
class AutoPost:
|
class AutoPost:
|
||||||
"""
|
"""
|
||||||
|
@ -69,6 +72,8 @@ class AutoPost:
|
||||||
self._success = callback
|
self._success = callback
|
||||||
return func
|
return func
|
||||||
|
|
||||||
|
_logger.info("Registering success callback")
|
||||||
|
|
||||||
return inner
|
return inner
|
||||||
|
|
||||||
def on_error(self, callback: typing.Any = None):
|
def on_error(self, callback: typing.Any = None):
|
||||||
|
@ -95,6 +100,8 @@ class AutoPost:
|
||||||
self._error = callback
|
self._error = callback
|
||||||
return func
|
return func
|
||||||
|
|
||||||
|
_logger.info("Registering error callback")
|
||||||
|
|
||||||
return inner
|
return inner
|
||||||
|
|
||||||
def init_stats(self, callback: typing.Any = None):
|
def init_stats(self, callback: typing.Any = None):
|
||||||
|
@ -120,6 +127,8 @@ class AutoPost:
|
||||||
self._stats = callback
|
self._stats = callback
|
||||||
return func
|
return func
|
||||||
|
|
||||||
|
_logger.info("Registered stats initialization function")
|
||||||
|
|
||||||
return inner
|
return inner
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -150,6 +159,7 @@ class AutoPost:
|
||||||
stats = await self._stats()
|
stats = await self._stats()
|
||||||
try:
|
try:
|
||||||
await self.client.http.post_bot_stats(self.bot_id, stats)
|
await self.client.http.post_bot_stats(self.bot_id, stats)
|
||||||
|
_logger.info("Tried to post bot stats")
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
on_error = getattr(self, "_error", None)
|
on_error = getattr(self, "_error", None)
|
||||||
if on_error:
|
if on_error:
|
||||||
|
@ -189,6 +199,9 @@ class AutoPost:
|
||||||
|
|
||||||
task = asyncio.ensure_future(self._internal_loop())
|
task = asyncio.ensure_future(self._internal_loop())
|
||||||
self._task = task
|
self._task = task
|
||||||
|
|
||||||
|
_logger.info("Started autoposting")
|
||||||
|
|
||||||
return task
|
return task
|
||||||
|
|
||||||
def stop(self) -> None:
|
def stop(self) -> None:
|
||||||
|
@ -199,3 +212,5 @@ class AutoPost:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
self._stopped = True
|
self._stopped = True
|
||||||
|
|
||||||
|
_logger.info("Stopped autoposting")
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
import typing
|
import typing
|
||||||
|
import logging
|
||||||
|
|
||||||
from . import types as boticord_types
|
from . import types as boticord_types
|
||||||
from .http import HttpClient
|
from .http import HttpClient
|
||||||
from .autopost import AutoPost
|
from .autopost import AutoPost
|
||||||
from .exceptions import MeilisearchException
|
from .exceptions import MeilisearchException
|
||||||
|
|
||||||
|
_logger = logging.getLogger("boticord")
|
||||||
|
|
||||||
|
|
||||||
class BoticordClient:
|
class BoticordClient:
|
||||||
"""Represents a client that can be used to interact with the BotiCord API.
|
"""Represents a client that can be used to interact with the BotiCord API.
|
||||||
|
@ -42,6 +45,8 @@ class BoticordClient:
|
||||||
:obj:`~.types.ResourceBot`:
|
:obj:`~.types.ResourceBot`:
|
||||||
ResourceBot object.
|
ResourceBot object.
|
||||||
"""
|
"""
|
||||||
|
_logger.info("Requesting information about bot")
|
||||||
|
|
||||||
response = await self.http.get_bot_info(bot_id)
|
response = await self.http.get_bot_info(bot_id)
|
||||||
return boticord_types.ResourceBot.from_dict(response)
|
return boticord_types.ResourceBot.from_dict(response)
|
||||||
|
|
||||||
|
@ -69,6 +74,8 @@ class BoticordClient:
|
||||||
:obj:`~.types.ResourceBot`:
|
:obj:`~.types.ResourceBot`:
|
||||||
ResourceBot object.
|
ResourceBot object.
|
||||||
"""
|
"""
|
||||||
|
_logger.info("Posting bot stats")
|
||||||
|
|
||||||
response = await self.http.post_bot_stats(
|
response = await self.http.post_bot_stats(
|
||||||
bot_id, {"servers": servers, "shards": shards, "users": users}
|
bot_id, {"servers": servers, "shards": shards, "users": users}
|
||||||
)
|
)
|
||||||
|
@ -87,6 +94,8 @@ class BoticordClient:
|
||||||
:obj:`~.types.ResourceServer`:
|
:obj:`~.types.ResourceServer`:
|
||||||
ResourceServer object.
|
ResourceServer object.
|
||||||
"""
|
"""
|
||||||
|
_logger.info("Requesting information about server")
|
||||||
|
|
||||||
response = await self.http.get_server_info(server_id)
|
response = await self.http.get_server_info(server_id)
|
||||||
return boticord_types.ResourceServer.from_dict(response)
|
return boticord_types.ResourceServer.from_dict(response)
|
||||||
|
|
||||||
|
@ -103,11 +112,13 @@ class BoticordClient:
|
||||||
:obj:`~.types.UserProfile`:
|
:obj:`~.types.UserProfile`:
|
||||||
UserProfile object.
|
UserProfile object.
|
||||||
"""
|
"""
|
||||||
|
_logger.info("Requesting information about user")
|
||||||
|
|
||||||
response = await self.http.get_user_info(user_id)
|
response = await self.http.get_user_info(user_id)
|
||||||
return boticord_types.UserProfile.from_dict(response)
|
return boticord_types.UserProfile.from_dict(response)
|
||||||
|
|
||||||
async def __search_for(self, index, data):
|
async def __search_for(self, index, data):
|
||||||
"""Search for smth on BotiCord"""
|
"""Search for something on BotiCord"""
|
||||||
if self._meilisearch_api_key is None:
|
if self._meilisearch_api_key is None:
|
||||||
token_response = await self.http.get_search_key()
|
token_response = await self.http.get_search_key()
|
||||||
self._meilisearch_api_key = token_response["key"]
|
self._meilisearch_api_key = token_response["key"]
|
||||||
|
@ -138,6 +149,7 @@ class BoticordClient:
|
||||||
List[:obj:`~.types.MeiliIndexedBot`]:
|
List[:obj:`~.types.MeiliIndexedBot`]:
|
||||||
List of found bots
|
List of found bots
|
||||||
"""
|
"""
|
||||||
|
_logger.info("Searching for bots on BotiCord")
|
||||||
|
|
||||||
response = await self.__search_for("bots", kwargs)
|
response = await self.__search_for("bots", kwargs)
|
||||||
return [boticord_types.MeiliIndexedBot.from_dict(bot) for bot in response]
|
return [boticord_types.MeiliIndexedBot.from_dict(bot) for bot in response]
|
||||||
|
@ -154,6 +166,7 @@ class BoticordClient:
|
||||||
List[:obj:`~.types.MeiliIndexedServer`]:
|
List[:obj:`~.types.MeiliIndexedServer`]:
|
||||||
List of found servers
|
List of found servers
|
||||||
"""
|
"""
|
||||||
|
_logger.info("Searching for servers on BotiCord")
|
||||||
|
|
||||||
response = await self.__search_for("servers", kwargs)
|
response = await self.__search_for("servers", kwargs)
|
||||||
return [
|
return [
|
||||||
|
@ -172,6 +185,7 @@ class BoticordClient:
|
||||||
List[:obj:`~.types.MeiliIndexedComment`]:
|
List[:obj:`~.types.MeiliIndexedComment`]:
|
||||||
List of found comments
|
List of found comments
|
||||||
"""
|
"""
|
||||||
|
_logger.info("Searching for comments on BotiCord")
|
||||||
|
|
||||||
response = await self.__search_for("comments", kwargs)
|
response = await self.__search_for("comments", kwargs)
|
||||||
return [
|
return [
|
||||||
|
|
|
@ -24,7 +24,13 @@ class BotiCordWebsocket:
|
||||||
self._token = token
|
self._token = token
|
||||||
|
|
||||||
def listener(self):
|
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:
|
For example:
|
||||||
|
|
||||||
|
@ -39,6 +45,7 @@ class BotiCordWebsocket:
|
||||||
if not asyncio.iscoroutinefunction(func):
|
if not asyncio.iscoroutinefunction(func):
|
||||||
raise TypeError(f"<{func.__qualname__}> must be a coroutine function")
|
raise TypeError(f"<{func.__qualname__}> must be a coroutine function")
|
||||||
self._listeners[func.__qualname__] = func
|
self._listeners[func.__qualname__] = func
|
||||||
|
_logger.debug(f"Listener {func.__qualname__} added successfully!")
|
||||||
return func
|
return func
|
||||||
|
|
||||||
return inner
|
return inner
|
||||||
|
@ -51,11 +58,18 @@ class BotiCordWebsocket:
|
||||||
Type of notification (Check reference page)
|
Type of notification (Check reference page)
|
||||||
callback (:obj:`function`)
|
callback (:obj:`function`)
|
||||||
Coroutine Callback 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):
|
if not asyncio.iscoroutinefunction(callback):
|
||||||
raise TypeError(f"<{callback.__qualname__}> must be a coroutine function")
|
raise TypeError(f"<{callback.__qualname__}> must be a coroutine function")
|
||||||
|
|
||||||
self._listeners[notification_type] = callback
|
self._listeners[notification_type] = callback
|
||||||
|
_logger.debug(f"Listener {callback.__qualname__} added successfully!")
|
||||||
return self
|
return self
|
||||||
|
|
||||||
async def connect(self) -> None:
|
async def connect(self) -> None:
|
||||||
|
|
Loading…
Reference in a new issue