Skip to content

Commit 34afeab

Browse files
committed
Allow passing initial seq, session_id and resume_url attributes to the shard, as well as retrieving them.
- Additionally export all impl classes to `hikari` namespace
1 parent 7055102 commit 34afeab

File tree

8 files changed

+68
-23
lines changed

8 files changed

+68
-23
lines changed

hikari/__init__.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# SOFTWARE.
2323
"""A sane Python framework for writing modern Discord bots.
2424
25-
To get started, you will want to initialize an instance of `hikari.impl.bot.GatewayBot`
25+
To get started, you will want to initialize an instance of `hikari.impl.gateway_bot.GatewayBot`
2626
for writing a gateway based bot, `hikari.impl.rest_bot.RESTBot` for a REST based bot,
2727
or `hikari.impl.rest.RESTApp` if you only need to use the REST API.
2828
"""
@@ -100,10 +100,7 @@
100100
from hikari.files import Rawish
101101
from hikari.files import Resourceish
102102
from hikari.guilds import *
103-
from hikari.impl import ClientCredentialsStrategy
104-
from hikari.impl import GatewayBot
105-
from hikari.impl import RESTApp
106-
from hikari.impl import RESTBot
103+
from hikari.impl import *
107104
from hikari.intents import *
108105
from hikari.interactions.base_interactions import *
109106
from hikari.interactions.command_interactions import *

hikari/__init__.pyi

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,7 @@ from hikari.files import Pathish as Pathish
7474
from hikari.files import Rawish as Rawish
7575
from hikari.files import Resourceish as Resourceish
7676
from hikari.guilds import *
77-
from hikari.impl import ClientCredentialsStrategy as ClientCredentialsStrategy
78-
from hikari.impl import GatewayBot as GatewayBot
79-
from hikari.impl import RESTApp as RESTApp
80-
from hikari.impl import RESTBot as RESTBot
77+
from hikari.impl import *
8178
from hikari.intents import *
8279
from hikari.interactions.base_interactions import *
8380
from hikari.interactions.command_interactions import *

hikari/api/shard.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ def is_connected(self) -> bool:
104104
def shard_count(self) -> int:
105105
"""Return the total number of shards expected in the entire application."""
106106

107+
@property
108+
@abc.abstractmethod
109+
def session_id(self) -> typing.Optional[str]:
110+
"""The session id for the shard."""
111+
112+
@property
113+
@abc.abstractmethod
114+
def seq(self) -> typing.Optional[int]:
115+
"""The sequence number for the shard."""
116+
117+
@property
118+
@abc.abstractmethod
119+
def resume_gateway_url(self) -> typing.Optional[str]:
120+
"""The resume gateway url for the shard."""
121+
107122
@abc.abstractmethod
108123
def get_user_id(self) -> snowflakes.Snowflake:
109124
"""Return the user ID.

hikari/impl/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
from hikari.impl.config import *
3636
from hikari.impl.entity_factory import *
3737
from hikari.impl.event_manager import *
38-
from hikari.impl.event_manager_base import *
38+
from hikari.impl.event_manager_base import EventManagerBase
39+
from hikari.impl.event_manager_base import EventStream
3940
from hikari.impl.gateway_bot import *
4041
from hikari.impl.interaction_server import *
4142
from hikari.impl.rate_limits import *

hikari/impl/__init__.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ from hikari.impl.cache import *
66
from hikari.impl.config import *
77
from hikari.impl.entity_factory import *
88
from hikari.impl.event_manager import *
9-
from hikari.impl.event_manager_base import *
9+
from hikari.impl.event_manager_base import EventManagerBase as EventManagerBase
10+
from hikari.impl.event_manager_base import EventStream as EventStream
1011
from hikari.impl.gateway_bot import *
1112
from hikari.impl.interaction_server import *
1213
from hikari.impl.rate_limits import *

hikari/impl/gateway_bot.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ async def start(
987987
large_threshold=large_threshold,
988988
shard_id=shard_id,
989989
shard_count=shard_count,
990-
url=requirements.url,
990+
gateway_url=requirements.url,
991991
)
992992
for shard_id in window
993993
)
@@ -1261,7 +1261,7 @@ async def _start_one_shard(
12611261
large_threshold: int,
12621262
shard_id: int,
12631263
shard_count: int,
1264-
url: str,
1264+
gateway_url: str,
12651265
) -> None:
12661266
new_shard = shard_impl.GatewayShardImpl(
12671267
http_settings=self._http_settings,
@@ -1279,7 +1279,7 @@ async def _start_one_shard(
12791279
shard_id=shard_id,
12801280
shard_count=shard_count,
12811281
token=self._token,
1282-
url=url,
1282+
gateway_url=gateway_url,
12831283
)
12841284
try:
12851285
start = time.monotonic()

hikari/impl/shard.py

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ class GatewayShardImpl(shard.GatewayShard):
373373
----------
374374
token : str
375375
The bot token to use.
376-
url : str
376+
gateway_url : str
377377
The gateway URL to use. This should not contain a query-string or
378378
fragments.
379379
event_manager : hikari.api.event_manager.EventManager
@@ -417,6 +417,18 @@ class GatewayShardImpl(shard.GatewayShard):
417417
The proxy settings to use while negotiating a websocket.
418418
data_format : str
419419
Data format to use for inbound data. Only supported format is `"json"`.
420+
initial_seq : typing.Optional[int]
421+
The initial session sequence to start at.
422+
initial_session_id : typing.Optional[str]
423+
The initial session id to start with.
424+
initial_resume_gateway_url : typing.Optional[str]
425+
The initial resume gateway url to use.
426+
427+
Raises
428+
------
429+
ValueError
430+
If not all of `initial_seq`, `initial_session_id`, and `initial_resume_gateway_url`
431+
are passed, with any one of them being given a value.
420432
"""
421433

422434
__slots__: typing.Sequence[str] = (
@@ -467,24 +479,34 @@ def __init__(
467479
large_threshold: int = 250,
468480
shard_id: int = 0,
469481
shard_count: int = 1,
482+
initial_seq: typing.Optional[int] = None,
483+
initial_session_id: typing.Optional[str] = None,
484+
initial_resume_gateway_url: typing.Optional[str] = None,
470485
http_settings: config.HTTPSettings,
471486
proxy_settings: config.ProxySettings,
472487
data_format: str = shard.GatewayDataFormat.JSON,
473488
event_manager: event_manager_.EventManager,
474489
event_factory: event_factory_.EventFactory,
475490
token: str,
476-
url: str,
491+
gateway_url: str,
477492
) -> None:
478493
if data_format != shard.GatewayDataFormat.JSON:
479494
raise NotImplementedError(f"Unsupported gateway data format: {data_format}")
480495

481496
if compression and compression != shard.GatewayCompression.TRANSPORT_ZLIB_STREAM:
482497
raise NotImplementedError(f"Unsupported compression format {compression}")
483498

499+
if bool(initial_seq) ^ bool(initial_session_id) ^ bool(initial_resume_gateway_url):
500+
# It makes no sense to allow passing RESUME data if not all the data is passed
501+
raise ValueError(
502+
"You must specify exactly all or neither of "
503+
"`initial_seq`, `initial_session_id` or `initial_resume_gateway_url`"
504+
)
505+
484506
self._activity = initial_activity
485507
self._event_manager = event_manager
486508
self._event_factory = event_factory
487-
self._gateway_url = url
509+
self._gateway_url = gateway_url
488510
self._handshake_event: typing.Optional[asyncio.Event] = None
489511
self._heartbeat_latency = float("nan")
490512
self._http_settings = http_settings
@@ -501,9 +523,9 @@ def __init__(
501523
f"shard {shard_id} non-priority rate limit", *_NON_PRIORITY_RATELIMIT
502524
)
503525
self._proxy_settings = proxy_settings
504-
self._resume_gateway_url: typing.Optional[str] = None
505-
self._seq: typing.Optional[int] = None
506-
self._session_id: typing.Optional[str] = None
526+
self._resume_gateway_url = initial_resume_gateway_url
527+
self._seq = initial_seq
528+
self._session_id = initial_session_id
507529
self._shard_count = shard_count
508530
self._shard_id = shard_id
509531
self._status = initial_status
@@ -541,6 +563,18 @@ def is_connected(self) -> bool:
541563
def shard_count(self) -> int:
542564
return self._shard_count
543565

566+
@property
567+
def session_id(self) -> typing.Optional[str]:
568+
return self._session_id
569+
570+
@property
571+
def seq(self) -> typing.Optional[int]:
572+
return self._seq
573+
574+
@property
575+
def resume_gateway_url(self) -> typing.Optional[str]:
576+
return self._resume_gateway_url
577+
544578
async def close(self) -> None:
545579
if not self._keep_alive_task:
546580
raise errors.ComponentStateConflictError("Cannot close an inactive shard")

tests/hikari/impl/test_gateway_bot.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ async def test_start_one_shard(self, bot):
948948
large_threshold=1000,
949949
shard_id=1,
950950
shard_count=3,
951-
url="https://some.website",
951+
gateway_url="https://some.website",
952952
)
953953

954954
shard.assert_called_once_with(
@@ -989,7 +989,7 @@ async def test_start_one_shard_when_not_alive(self, bot):
989989
large_threshold=1000,
990990
shard_id=1,
991991
shard_count=3,
992-
url="https://some.website",
992+
gateway_url="https://some.website",
993993
)
994994

995995
assert bot._shards == {}
@@ -1015,7 +1015,7 @@ async def test_start_one_shard_when_exception(self, bot, is_alive):
10151015
large_threshold=1000,
10161016
shard_id=1,
10171017
shard_count=3,
1018-
url="https://some.website",
1018+
gateway_url="https://some.website",
10191019
)
10201020

10211021
assert bot._shards == {}

0 commit comments

Comments
 (0)