Skip to content

Commit 87d3d38

Browse files
EepyElvyrai0bs
authored andcommitted
chore: catchup branch to stable.
* fix!: make context not required * fix!: remove requirement of reason in helper methods * Update gateway.py feat!(gateway): Introduce proper reconnection without close. (#567) It may introduce a runtime error however. docs: add migration docs for 4.1 docs: detail message content intent in migration
1 parent 0cc265d commit 87d3d38

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

docs/migration.rst

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,45 @@ will need it appended as a key-word argument. The example below shows this chang
2020
2121
# 4.0.2
2222
...
23-
channel = interactions.Channel(**data, _state=bot.http)
23+
channel = interactions.Channel(**data, _client=bot.http)
2424
2525
This change was added in favor for being able to use endpoints in an abstracted state.
26+
27+
4.0.2 → 4.1.0
28+
~~~~~~~~~~~~~~~
29+
30+
``4.1.0`` introduces numerous breaking changes that affect the bot developers' ability to work with mainly modifying
31+
the way our Gateway processes information, as well as supplying a ``reason`` argument to ``HTTPClient``-based methods.
32+
33+
In this version, you are no longer required to give a ``reason`` for every HTTP request. You can instead send it as an optional,
34+
which will work to keep any existing modifications of our codebase from breaking.
35+
36+
Additionally, modifying the way information is dispatched from the Gateway must be done like this now:
37+
38+
.. code-block:: python
39+
40+
import interactions
41+
42+
class ExtendedWebSocketClient(interactions.WebSocketClient):
43+
def _dispatch_event(self, event: str, data: dict):
44+
super()._dispatch_event(event, data)
45+
46+
if event != "TYPING_START" and event == "INTERACTION_CREATE":
47+
... # run your special interaction dispatch rules here.
48+
49+
We recommend that working in correspondance with this, you should be making use of our ``interactions.ext`` SDK framework.
50+
51+
A slight, yet another breaking change we made was dundering numerous attributes in our internal models.
52+
You will now need to refer to the client's HTTP object as ``_http`` instead of ``http``. In order to view
53+
the full list of these, we highly encourage you to view the documentation readily available.
54+
55+
The last, and most major change is the introduction of the ``MESSAGE_CONTENT`` privileged intent in the library.
56+
Because of ``4.1.0`` using version 10 of the Discord API, this intent is now required for reading all message
57+
content. In order to make sure your bot works still, you will need to enable this intent in your bot's developer
58+
portal and add the intent to your current intents when connecting:
59+
60+
.. code-block:: python
61+
62+
from interactions import Client, Intents
63+
64+
bot = Client("TOKEN", intents=Intents.DEFAULT | Intents.MEESAGE_CONTENT)

interactions/api/gateway.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ async def __restart(self):
165165
if self.__task:
166166
self.__task: Task
167167
self.__task.cancel()
168-
await self._client.close()
168+
self._client = None # clear pending waits
169169
self.__heartbeater.event.clear()
170170
await self._establish_connection()
171171

@@ -199,6 +199,10 @@ async def _establish_connection(
199199

200200
if stream is None:
201201
continue
202+
if self._client is None:
203+
await self._establish_connection()
204+
break
205+
202206
if self._client.close_code in range(4010, 4014) or self._client.close_code == 4004:
203207
raise GatewayException(self._client.close_code)
204208

@@ -230,6 +234,10 @@ async def _handle_connection(
230234
if op == OpCodeType.HELLO:
231235
self.__heartbeater.delay = data["heartbeat_interval"]
232236
self.__heartbeater.event.set()
237+
238+
if self.__task:
239+
self.__task.cancel() # so we can reduce redundant heartbeat bg tasks.
240+
233241
self.__task = ensure_future(self._manage_heartbeat())
234242

235243
if not self.session_id:
@@ -246,12 +254,17 @@ async def _handle_connection(
246254
if op in (OpCodeType.INVALIDATE_SESSION, OpCodeType.RECONNECT):
247255
log.debug("INVALID_SESSION/RECONNECT")
248256

249-
if data and op != OpCodeType.RECONNECT:
257+
# if data and op != OpCodeType.RECONNECT:
258+
# self.session_id = None
259+
# self.sequence = None
260+
# self._closed = True
261+
262+
if bool(data) is False and op == OpCodeType.INVALIDATE_SESSION:
250263
self.session_id = None
251-
self.sequence = None
252-
self._closed = True
253264

254265
await self.__restart()
266+
elif event == "RESUMED":
267+
log.debug(f"RESUMED (session_id: {self.session_id}, seq: {self.sequence})")
255268
elif event == "READY":
256269
self._ready = data
257270
self.session_id = data["session_id"]
@@ -342,7 +355,7 @@ def _dispatch_event(self, event: str, data: dict) -> None:
342355

343356
if _context.data._json.get("options"):
344357
for option in _context.data.options:
345-
__name, _value = self.__sub_command_context(option)
358+
__name, _value = self.__sub_command_context(option, _context)
346359
_name += f"_{__name}" if __name else ""
347360

348361
if _value:
@@ -399,7 +412,7 @@ def __contextualize(self, data: dict) -> object:
399412
return context(**data)
400413

401414
def __sub_command_context(
402-
self, data: Union[dict, Option], context: object = MISSING
415+
self, data: Union[dict, Option], context: object
403416
) -> Union[Tuple[str], dict]:
404417
"""
405418
Checks if an application command schema has sub commands
@@ -515,6 +528,9 @@ def __option_type_context(self, context: object, type: int) -> dict:
515528
}
516529
return _resolved
517530

531+
async def restart(self):
532+
await self.__restart()
533+
518534
@property
519535
async def __receive_packet_stream(self) -> Optional[Dict[str, Any]]:
520536
"""
@@ -523,6 +539,7 @@ async def __receive_packet_stream(self) -> Optional[Dict[str, Any]]:
523539
:return: The packet stream.
524540
:rtype: Optional[Dict[str, Any]]
525541
"""
542+
526543
packet: WSMessage = await self._client.receive()
527544
return loads(packet.data) if packet and isinstance(packet.data, str) else None
528545

interactions/api/gateway.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,4 @@ class WebSocketClient:
7878
def shard(self) -> None: ...
7979
@property
8080
def presence(self) -> None: ...
81+
async def restart(self): ...

0 commit comments

Comments
 (0)