From 08880545877d0bafa207a7aa9bef0073bd82d8d5 Mon Sep 17 00:00:00 2001 From: grey-cat-1908 Date: Tue, 29 Mar 2022 22:05:23 +0300 Subject: [PATCH] make intents more user-friendly (you can use iter to specify them) and SIGNAL handler --- melisa/client.py | 65 ++++++++++++++++++++++++-------------- melisa/core/gateway.py | 4 ++- melisa/core/http.py | 2 +- melisa/models/app/shard.py | 2 +- 4 files changed, 47 insertions(+), 26 deletions(-) diff --git a/melisa/client.py b/melisa/client.py index 047911a..a7f1910 100644 --- a/melisa/client.py +++ b/melisa/client.py @@ -4,8 +4,9 @@ import logging import asyncio import signal -from typing import Dict, List, Union, Any +from typing import Dict, List, Union, Any, Iterable +from .models.app.intents import Intents from .models import User, Guild, Activity from .models.app import Shard from .utils import Snowflake, APIModelBase @@ -28,7 +29,7 @@ class Client: ---------- token: :class:`str` The token to login (you can found it in the developer portal) - intents: :class:`~melisa.Intents` + intents: :class:`Union[~melisa.Intents, Iterable[~melisa.Intents]]` The Discord Intents values. activity: :class:`~models.user.presence.BotActivity` The Activity to set (on connecting) @@ -55,14 +56,14 @@ class Client: """ def __init__( - self, - token: str, - intents, - *, - activity: Activity = None, - status: str = None, - mobile: bool = False, - logs: Union[None, int, str, Dict[str, Any]] = "INFO", + self, + token: str, + *, + intents: Union[Intents, Iterable[Intents]] = None, + activity: Activity = None, + status: str = None, + mobile: bool = False, + logs: Union[None, int, str, Dict[str, Any]] = "INFO", ): self.shards: Dict[int, Shard] = {} self.http: HTTPClient = HTTPClient(token) @@ -76,7 +77,12 @@ class Client: self._gateway_info = self._loop.run_until_complete(self._get_gateway()) - self.intents = intents + if isinstance(intents, Iterable): + self.intents = sum(intents) + + if intents is None: + self.intents = Intents.all() - Intents.GUILD_PRESENCES - Intents.GUILD_MEMBERS + self._token = token self._activity = activity @@ -88,6 +94,18 @@ class Client: init_logging(logs) + def sigint_handler(_signal, _frame): + _logger.info("SIGINT received, shutting down...") + + asyncio.create_task(self.http.close()) + + if self._loop.is_running(): + self._loop.stop() + + print("(SIGINT received some seconds ago) Successfully stopped client loop") + + signal.signal(signal.SIGINT, sigint_handler) + async def _get_gateway(self): """Get Gateway information""" return GatewayBotInfo.from_dict(await self.http.get("gateway/bot")) @@ -114,11 +132,12 @@ class Client: inited_shard = Shard(self, 0, 1) asyncio.ensure_future( - inited_shard.launch(activity=self._activity, - status=self._status, - mobile=self._mobile, - loop=self._loop, - ) + inited_shard.launch( + activity=self._activity, + status=self._status, + mobile=self._mobile, + loop=self._loop, + ) ) self._loop.run_forever() @@ -140,9 +159,9 @@ class Client: inited_shard = Shard(self, shard_id, num_shards) asyncio.ensure_future( - inited_shard.launch(activity=self._activity, - status=self._status, - mobile=self._mobile), + inited_shard.launch( + activity=self._activity, status=self._status, mobile=self._mobile + ), loop=self._loop, ) self._loop.run_forever() @@ -158,9 +177,9 @@ class Client: inited_shard = Shard(self, shard_id, num_shards) asyncio.ensure_future( - inited_shard.launch(activity=self._activity, - status=self._status, - mobile=self._mobile), + inited_shard.launch( + activity=self._activity, status=self._status, mobile=self._mobile + ), loop=self._loop, ) self._loop.run_forever() @@ -198,7 +217,7 @@ class Client: return Guild.from_dict(data) async def fetch_channel( - self, channel_id: Union[Snowflake, str, int] + self, channel_id: Union[Snowflake, str, int] ) -> Union[Channel, Any]: """ Fetch Channel from the Discord API (by id). diff --git a/melisa/core/gateway.py b/melisa/core/gateway.py index 307cee4..161b938 100644 --- a/melisa/core/gateway.py +++ b/melisa/core/gateway.py @@ -76,7 +76,9 @@ class Gateway: "intents": self.intents, "properties": { "$os": sys.platform, - "$browser": "Discord iOS" if kwargs.get("mobile") is not None else "MelisaPy", + "$browser": "Discord iOS" + if kwargs.get("mobile") is not None + else "MelisaPy", "$device": "Melisa Python Library", }, "compress": True, diff --git a/melisa/core/http.py b/melisa/core/http.py index 7dd0cd8..af818b1 100644 --- a/melisa/core/http.py +++ b/melisa/core/http.py @@ -79,7 +79,7 @@ class HTTPClient: if ttl == 0: raise ServerError(f"Maximum amount of retries for `{endpoint}`.") - _logger.debug(f"Send {method} request to the {endpoint}") + _logger.debug(f"Sending {method} request to the {endpoint}") await self.__rate_limiter.wait_until_not_ratelimited(endpoint, method) diff --git a/melisa/models/app/shard.py b/melisa/models/app/shard.py index 1ec4393..799288a 100644 --- a/melisa/models/app/shard.py +++ b/melisa/models/app/shard.py @@ -41,7 +41,7 @@ class Shard: self._num_shards, start_activity=kwargs.get("activity"), start_status=kwargs.get("status"), - mobile=kwargs.get("mobile") + mobile=kwargs.get("mobile"), ) self._client.shards[self._shard_id] = self