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 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.
@ -56,8 +59,9 @@ class Client:
token: str,
intents,
*,
activity=None,
activity: Activity = None,
status: str = None,
mobile: bool = False,
logs: Union[None, int, str, Dict[str, Any]] = "INFO",
):
self.shards: Dict[int, Shard] = {}
@ -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,9 +114,12 @@ class Client:
inited_shard = Shard(self, 0, 1)
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()
def run_shards(self, num_shards: int, *, shard_ids: List[int] = None):
@ -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()

View file

@ -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)

View file

@ -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|

View file

@ -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"