removed BotActivity, add mobile device support

This commit is contained in:
grey-cat-1908 2022-03-29 21:48:13 +03:00
parent da64d071e2
commit f215a4ea50
4 changed files with 34 additions and 40 deletions

View file

@ -3,9 +3,10 @@
import logging import logging
import asyncio import asyncio
import signal
from typing import Dict, List, Union, Any from typing import Dict, List, Union, Any
from .models import User, Guild from .models import User, Guild, Activity
from .models.app import Shard from .models.app import Shard
from .utils import Snowflake, APIModelBase from .utils import Snowflake, APIModelBase
from .utils.types import Coro from .utils.types import Coro
@ -34,6 +35,8 @@ class Client:
status: :class:`str` status: :class:`str`
The Status to set (on connecting). The Status to set (on connecting).
Can be generated using :class:`~models.user.presence.StatusType` 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]]` logs: :class:`Optional[None, str, Dict[str, Any]]`
The hint for configuring logging. The hint for configuring logging.
This can be `None` to disable logging automatically. This can be `None` to disable logging automatically.
@ -52,13 +55,14 @@ class Client:
""" """
def __init__( def __init__(
self, self,
token: str, token: str,
intents, intents,
*, *,
activity=None, activity: Activity = None,
status: str = None, status: str = None,
logs: Union[None, int, str, Dict[str, Any]] = "INFO", mobile: bool = False,
logs: Union[None, int, str, Dict[str, Any]] = "INFO",
): ):
self.shards: Dict[int, Shard] = {} self.shards: Dict[int, Shard] = {}
self.http: HTTPClient = HTTPClient(token) self.http: HTTPClient = HTTPClient(token)
@ -77,6 +81,7 @@ class Client:
self._activity = activity self._activity = activity
self._status = status self._status = status
self._mobile = mobile
self._none_guilds_cached = False self._none_guilds_cached = False
APIModelBase.set_client(self) APIModelBase.set_client(self)
@ -109,8 +114,11 @@ class Client:
inited_shard = Shard(self, 0, 1) inited_shard = Shard(self, 0, 1)
asyncio.ensure_future( asyncio.ensure_future(
inited_shard.launch(activity=self._activity, status=self._status), inited_shard.launch(activity=self._activity,
loop=self._loop, status=self._status,
mobile=self._mobile,
loop=self._loop,
)
) )
self._loop.run_forever() self._loop.run_forever()
@ -132,7 +140,9 @@ class Client:
inited_shard = Shard(self, shard_id, num_shards) inited_shard = Shard(self, shard_id, num_shards)
asyncio.ensure_future( 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, loop=self._loop,
) )
self._loop.run_forever() self._loop.run_forever()
@ -148,7 +158,9 @@ class Client:
inited_shard = Shard(self, shard_id, num_shards) inited_shard = Shard(self, shard_id, num_shards)
asyncio.ensure_future( 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, loop=self._loop,
) )
self._loop.run_forever() self._loop.run_forever()
@ -186,7 +198,7 @@ class Client:
return Guild.from_dict(data) return Guild.from_dict(data)
async def fetch_channel( async def fetch_channel(
self, channel_id: Union[Snowflake, str, int] self, channel_id: Union[Snowflake, str, int]
) -> Union[Channel, Any]: ) -> Union[Channel, Any]:
""" """
Fetch Channel from the Discord API (by id). Fetch Channel from the Discord API (by id).

View file

@ -14,7 +14,7 @@ import aiohttp
from ..exceptions import GatewayError, PrivilegedIntentsRequired, LoginFailure from ..exceptions import GatewayError, PrivilegedIntentsRequired, LoginFailure
from ..listeners import listeners from ..listeners import listeners
from ..models.user import BotActivity from ..models.user import Activity
from ..utils import APIModelBase, json from ..utils import APIModelBase, json
_logger = logging.getLogger("melisa.gateway") _logger = logging.getLogger("melisa.gateway")
@ -76,7 +76,7 @@ class Gateway:
"intents": self.intents, "intents": self.intents,
"properties": { "properties": {
"$os": sys.platform, "$os": sys.platform,
"$browser": "Melisa Python Library", "$browser": "Discord iOS" if kwargs.get("mobile") is not None else "MelisaPy",
"$device": "Melisa Python Library", "$device": "Melisa Python Library",
}, },
"compress": True, "compress": True,
@ -267,11 +267,11 @@ class Gateway:
) )
@staticmethod @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} data = {"since": time.time() * 1000, "afk": False}
if activity is not None: if activity is not None:
data["activities"] = activity.to_dict() data["activities"] = [activity.to_dict()]
if status is not None: if status is not None:
data["status"] = str(status) data["status"] = str(status)

View file

@ -6,7 +6,7 @@ from __future__ import annotations
from asyncio import create_task, sleep from asyncio import create_task, sleep
from ...core.gateway import Gateway from ...core.gateway import Gateway
from ..user import BotActivity from ..user import Activity
class Shard: class Shard:
@ -41,6 +41,7 @@ class Shard:
self._num_shards, self._num_shards,
start_activity=kwargs.get("activity"), start_activity=kwargs.get("activity"),
start_status=kwargs.get("status"), start_status=kwargs.get("status"),
mobile=kwargs.get("mobile")
) )
self._client.shards[self._shard_id] = self self._client.shards[self._shard_id] = self
@ -58,7 +59,7 @@ class Shard:
create_task(self._gateway.close()) create_task(self._gateway.close())
async def update_presence( async def update_presence(
self, activity: BotActivity = None, status: str = None self, activity: Activity = None, status: str = None
) -> Shard: ) -> Shard:
""" """
|coro| |coro|

View file

@ -195,7 +195,7 @@ class Activity(BasePresence, APIModelBase):
Activity type Activity type
url: Optional[:class:`str`] url: Optional[:class:`str`]
Stream url, is validated when type is 1 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 Unix timestamp (in milliseconds) of when the activity was added to the user's session
timestamps: Optional[:class:`~melisa.models.user.activity.ActivityTimestamp`] timestamps: Optional[:class:`~melisa.models.user.activity.ActivityTimestamp`]
Unix timestamps for start and/or end of the game Unix timestamps for start and/or end of the game
@ -223,8 +223,7 @@ class Activity(BasePresence, APIModelBase):
name: str name: str
type: ActivityType type: ActivityType
created_at: int created_at: APINullable[int] = None
url: APINullable[str] = None url: APINullable[str] = None
timestamps: APINullable[ActivityTimestamp] = None timestamps: APINullable[ActivityTimestamp] = None
application_id: APINullable[Snowflake] = None application_id: APINullable[Snowflake] = None
@ -239,24 +238,6 @@ class Activity(BasePresence, APIModelBase):
buttons: APINullable[List[ActivityButton]] = None 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): class StatusType(Enum):
ONLINE = "online" ONLINE = "online"
OFFLINE = "offline" OFFLINE = "offline"