From f215a4ea503d236dff7706a2db3da46a625d56a8 Mon Sep 17 00:00:00 2001 From: grey-cat-1908 Date: Tue, 29 Mar 2022 21:48:13 +0300 Subject: [PATCH] removed BotActivity, add mobile device support --- melisa/client.py | 38 ++++++++++++++++++++++------------ melisa/core/gateway.py | 8 +++---- melisa/models/app/shard.py | 5 +++-- melisa/models/user/presence.py | 23 ++------------------ 4 files changed, 34 insertions(+), 40 deletions(-) diff --git a/melisa/client.py b/melisa/client.py index db2c3e5..047911a 100644 --- a/melisa/client.py +++ b/melisa/client.py @@ -3,9 +3,10 @@ import logging import asyncio +import signal from typing import Dict, List, Union, Any -from .models import User, Guild +from .models import User, Guild, Activity from .models.app import Shard from .utils import Snowflake, APIModelBase from .utils.types import Coro @@ -34,6 +35,8 @@ class Client: status: :class:`str` The Status to set (on connecting). Can be generated using :class:`~models.user.presence.StatusType` + mobile: :class:`bool` + Set user device as mobile? logs: :class:`Optional[None, str, Dict[str, Any]]` The hint for configuring logging. This can be `None` to disable logging automatically. @@ -52,13 +55,14 @@ class Client: """ def __init__( - self, - token: str, - intents, - *, - activity=None, - status: str = None, - logs: Union[None, int, str, Dict[str, Any]] = "INFO", + self, + token: str, + intents, + *, + 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) @@ -77,6 +81,7 @@ class Client: self._activity = activity self._status = status + self._mobile = mobile self._none_guilds_cached = False APIModelBase.set_client(self) @@ -109,8 +114,11 @@ class Client: inited_shard = Shard(self, 0, 1) asyncio.ensure_future( - inited_shard.launch(activity=self._activity, status=self._status), - loop=self._loop, + inited_shard.launch(activity=self._activity, + status=self._status, + mobile=self._mobile, + loop=self._loop, + ) ) self._loop.run_forever() @@ -132,7 +140,9 @@ class Client: inited_shard = Shard(self, shard_id, num_shards) asyncio.ensure_future( - inited_shard.launch(activity=self._activity, status=self._status), + inited_shard.launch(activity=self._activity, + status=self._status, + mobile=self._mobile), loop=self._loop, ) self._loop.run_forever() @@ -148,7 +158,9 @@ class Client: inited_shard = Shard(self, shard_id, num_shards) asyncio.ensure_future( - inited_shard.launch(activity=self._activity, status=self._status), + inited_shard.launch(activity=self._activity, + status=self._status, + mobile=self._mobile), loop=self._loop, ) self._loop.run_forever() @@ -186,7 +198,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 4d7087f..307cee4 100644 --- a/melisa/core/gateway.py +++ b/melisa/core/gateway.py @@ -14,7 +14,7 @@ import aiohttp from ..exceptions import GatewayError, PrivilegedIntentsRequired, LoginFailure from ..listeners import listeners -from ..models.user import BotActivity +from ..models.user import Activity from ..utils import APIModelBase, json _logger = logging.getLogger("melisa.gateway") @@ -76,7 +76,7 @@ class Gateway: "intents": self.intents, "properties": { "$os": sys.platform, - "$browser": "Melisa Python Library", + "$browser": "Discord iOS" if kwargs.get("mobile") is not None else "MelisaPy", "$device": "Melisa Python Library", }, "compress": True, @@ -267,11 +267,11 @@ class Gateway: ) @staticmethod - def generate_presence(activity: BotActivity = None, status: str = None): + def generate_presence(activity: Activity = None, status: str = None): data = {"since": time.time() * 1000, "afk": False} if activity is not None: - data["activities"] = activity.to_dict() + data["activities"] = [activity.to_dict()] if status is not None: data["status"] = str(status) diff --git a/melisa/models/app/shard.py b/melisa/models/app/shard.py index f6db724..1ec4393 100644 --- a/melisa/models/app/shard.py +++ b/melisa/models/app/shard.py @@ -6,7 +6,7 @@ from __future__ import annotations from asyncio import create_task, sleep from ...core.gateway import Gateway -from ..user import BotActivity +from ..user import Activity class Shard: @@ -41,6 +41,7 @@ class Shard: self._num_shards, start_activity=kwargs.get("activity"), start_status=kwargs.get("status"), + mobile=kwargs.get("mobile") ) self._client.shards[self._shard_id] = self @@ -58,7 +59,7 @@ class Shard: create_task(self._gateway.close()) async def update_presence( - self, activity: BotActivity = None, status: str = None + self, activity: Activity = None, status: str = None ) -> Shard: """ |coro| diff --git a/melisa/models/user/presence.py b/melisa/models/user/presence.py index 317663b..af588c8 100644 --- a/melisa/models/user/presence.py +++ b/melisa/models/user/presence.py @@ -195,7 +195,7 @@ class Activity(BasePresence, APIModelBase): Activity type url: Optional[:class:`str`] Stream url, is validated when type is 1 - created_at: :class:`int` + created_at: Optional[:class:`int`] Unix timestamp (in milliseconds) of when the activity was added to the user's session timestamps: Optional[:class:`~melisa.models.user.activity.ActivityTimestamp`] Unix timestamps for start and/or end of the game @@ -223,8 +223,7 @@ class Activity(BasePresence, APIModelBase): name: str type: ActivityType - created_at: int - + created_at: APINullable[int] = None url: APINullable[str] = None timestamps: APINullable[ActivityTimestamp] = None application_id: APINullable[Snowflake] = None @@ -239,24 +238,6 @@ class Activity(BasePresence, APIModelBase): buttons: APINullable[List[ActivityButton]] = None -@dataclass(repr=False) -class BotActivity(BasePresence, APIModelBase): - """ - - Attributes - ---------- - name: :class:`str` - The activity's name - type: :class:`~melisa.models.user.activity.ActivityType` - Activity type - url: Optional[:class:`str`] - Stream url, is validated when type is Streaming""" - - name: str - type: ActivityType - url: APINullable[str] = None - - class StatusType(Enum): ONLINE = "online" OFFLINE = "offline"