From d618cbdcba29955d7e1033797f9b8b889e2e8b0a Mon Sep 17 00:00:00 2001 From: Victor Kotlin Date: Sat, 11 Sep 2021 15:53:15 +0300 Subject: [PATCH] add boticord-loop --- boticordpy/client.py | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/boticordpy/client.py b/boticordpy/client.py index 0dee32b..eb88a84 100644 --- a/boticordpy/client.py +++ b/boticordpy/client.py @@ -31,7 +31,8 @@ class BoticordClient: __slots__ = ( "Bots", "Servers", - "Users" + "Users", + "bot" ) bot: Union[commands.Bot, commands.AutoShardedBot] @@ -39,6 +40,37 @@ class BoticordClient: def __init__(self, bot, token=None, **kwargs): loop = kwargs.get('loop') or asyncio.get_event_loop() session = kwargs.get('session') or aiohttp.ClientSession(loop=loop) + self.bot = bot self.Bots = Bots(bot, token=token, loop=loop, session=session) self.Servers = Servers(bot, token=token, loop=loop, session=session) self.Users = Users(token=token, loop=loop, session=session) + + def start_loop(self, sleep_time: int = None) -> None: + """ + + Parameters + ---------- + sleep_time: :class:`int` + loop sleep time - can be unfilled or None + + """ + self.bot.loop.create_task(self.__loop(sleep_time=sleep_time)) + + async def __loop(self, sleep_time: int = None) -> None: + """ + The internal loop used for automatically posting stats + """ + await self.bot.wait_until_ready() + + while not self.bot.is_closed(): + data_to_send = {"servers": len(self.bot.guilds), "users": len(self.bot.users)} + + if isinstance(self.bot, commands.AutoShardedBot): + data_to_send["shards"] = self.bot.shard_count + + await self.Bots.postStats(data_to_send) + + if sleep_time is None: + sleep_time = 900 + + await asyncio.sleep(sleep_time)