Skip to content

Commit 8bc3e1d

Browse files
authored
Merge pull request #1270 from interactions-py/unstable
chore: Merge unstable to stable.
2 parents 5a76913 + be2ac0e commit 8bc3e1d

37 files changed

+483
-262
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ repos:
2929
- id: check-merge-conflict
3030
name: Merge Conflicts
3131
- repo: https://github.com/psf/black
32-
rev: 22.12.0
32+
rev: 23.1.0
3333
hooks:
3434
- id: black
3535
name: Black Formatting
@@ -45,7 +45,7 @@ repos:
4545
types: [file, python]
4646
args: [--max-line-length=100, --ignore=E203 E301 E302 E501 E402 E704 W503 W504]
4747
- repo: https://github.com/pycqa/isort
48-
rev: 5.11.4
48+
rev: 5.12.0
4949
hooks:
5050
- id: isort
5151
name: isort Formatting

docs/events.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ There are several different internal events:
5252

5353
- ``raw_socket_create``
5454
- ``on_start``
55+
- ``on_disconnect``
5556
- ``on_interaction``
5657
- ``on_command``
5758
- ``on_command_error``
@@ -82,6 +83,13 @@ This function takes no arguments.
8283
.. attention::
8384
Unlike ``on_ready``, this event will never be dispatched more than once.
8485

86+
Event: ``on_disconnect``
87+
^^^^^^^^^^^^^^^^^^^^^^^^^^^
88+
This event fires whenever the connection is invalidated and will often precede an ``on_ready`` event
89+
90+
This function takes no arguments.
91+
92+
8593
Event: ``on_interaction``
8694
^^^^^^^^^^^^^^^^^^^^^^^^^^^
8795
This event fires on any interaction (commands, components, autocomplete and modals).

interactions/api/cache.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ def merge(self, item: _T, id: Optional["Key"] = None) -> None:
6666
continue
6767
# we can only assume that discord did not provide it, falsely deleting is worse than not deleting
6868
if getattr(old_item, attrib) != getattr(item, attrib):
69-
7069
if isinstance(getattr(item, attrib), list) and not isinstance(
7170
getattr(old_item, attrib), list
7271
): # could be None

interactions/api/dispatch.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ def dispatch(self, name: str, /, *args, **kwargs) -> None:
3737
if converters := getattr(event, "_converters", None):
3838
_kwargs = kwargs.copy()
3939
for key, value in _kwargs.items():
40-
4140
if key in converters.keys():
4241
del kwargs[key]
4342
kwargs[converters[key]] = value

interactions/api/error.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,6 @@ def __init__(self, code: int = 0, message: str = None, severity: int = 0, **kwar
317317
self.log(self.message)
318318

319319
if _fmt_error:
320-
321320
_flag: bool = (
322321
self.message.lower() in self.lookup(self.code).lower()
323322
) # creativity is hard

interactions/api/gateway/client.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
wait_for,
1818
)
1919
from contextlib import suppress
20-
from enum import IntEnum
2120
from sys import platform, version_info
2221
from time import perf_counter
2322
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Type, Union
@@ -26,7 +25,7 @@
2625
from aiohttp import ClientWebSocketResponse, WSMessage, WSMsgType
2726

2827
from ...base import __version__, get_logger
29-
from ...client.enums import ComponentType, InteractionType, OptionType
28+
from ...client.enums import ComponentType, IntEnum, InteractionType, OptionType
3029
from ...client.models import Option
3130
from ...utils.missing import MISSING
3231
from ..dispatch import Listener
@@ -242,7 +241,6 @@ async def _manage_heartbeat(self) -> None:
242241
"""Manages the heartbeat loop."""
243242
log.debug(f"Sending heartbeat every {self.__heartbeater.delay / 1000} seconds...")
244243
while not self.__heartbeat_event.is_set():
245-
246244
log.debug("Sending heartbeat...")
247245
if not self.__heartbeater.event.is_set():
248246
log.debug("HEARTBEAT_ACK missing, reconnecting...")
@@ -404,7 +402,6 @@ def _dispatch_interaction_event(self, data: dict) -> None:
404402
_option = self.__sub_command_context(option, _context)
405403
__kwargs.update(_option)
406404

407-
self._dispatch.dispatch("on_command", _context)
408405
elif data["type"] == InteractionType.MESSAGE_COMPONENT:
409406
_name = f"component_{_context.data.custom_id}"
410407

@@ -931,6 +928,12 @@ async def _reconnect(self, to_resume: bool, code: Optional[int] = 1012) -> None:
931928
if self.__heartbeat_event.is_set():
932929
self.__heartbeat_event.clear() # Because we're hardresetting the process
933930

931+
self._dispatch.dispatch(
932+
"on_disconnect"
933+
) # will be followed by the on_ready event after reconnection
934+
# reconnection happens whenever it disconnects either with or without a resume prompt
935+
# as this is called whenever the WS client closes
936+
934937
if not to_resume:
935938
url = self.ws_url if self.ws_url else await self._http.get_gateway()
936939
else:
@@ -963,7 +966,6 @@ async def __receive_packet(self, ignore_lock: bool = False) -> Optional[Dict[str
963966
buffer = bytearray()
964967

965968
while True:
966-
967969
if not ignore_lock:
968970
# meaning if we're reconnecting or something because of tasks
969971
await self.__closed.wait()
@@ -1003,7 +1005,6 @@ async def __receive_packet(self, ignore_lock: bool = False) -> Optional[Dict[str
10031005
await self._reconnect(True)
10041006

10051007
elif packet.type == WSMsgType.CLOSING:
1006-
10071008
if ignore_lock:
10081009
raise LibraryException(
10091010
message="Discord unexpectedly closing on receiving by force.", severity=50
@@ -1061,7 +1062,6 @@ async def _send_packet(self, data: Dict[str, Any]) -> None:
10611062
await self._client.send_str(packet)
10621063
else:
10631064
async with self.reconnect_lock: # needs to lock while it reconnects.
1064-
10651065
if data["op"] != OpCodeType.HEARTBEAT.value:
10661066
# This is because the ratelimiter limits already accounts for this.
10671067
await self._ratelimiter.block()

interactions/api/http/interaction.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ async def create_interaction_response(
236236

237237
file_data = None
238238
if files:
239-
240239
file_data = MultipartWriter("form-data")
241240
part = file_data.append_json(data)
242241
part.set_content_disposition("form-data", name="payload_json")
@@ -295,7 +294,6 @@ async def edit_interaction_response(
295294
# ^ again, I don't know if python will let me
296295
file_data = None
297296
if files:
298-
299297
file_data = MultipartWriter("form-data")
300298
part = file_data.append_json(data)
301299
part.set_content_disposition("form-data", name="payload_json")

interactions/api/http/message.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ async def create_message(
8484

8585
data = None
8686
if files is not MISSING and len(files) > 0:
87-
8887
data = MultipartWriter("form-data")
8988
part = data.append_json(payload)
9089
part.set_content_disposition("form-data", name="payload_json")
@@ -172,7 +171,6 @@ async def edit_message(
172171
"""
173172
data = None
174173
if files is not MISSING and len(files) > 0:
175-
176174
data = MultipartWriter("form-data")
177175
part = data.append_json(payload)
178176
part.set_content_disposition("form-data", name="payload_json")

interactions/api/http/request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ async def request(self, route: Route, **kwargs) -> Optional[Any]:
209209
await asyncio.sleep(_limiter.reset_after)
210210
continue
211211
if remaining is not None and int(remaining) == 0:
212-
log.warning(
212+
log.debug(
213213
f"The HTTP client has exhausted a per-route ratelimit. Locking route for {reset_after} seconds."
214214
)
215215
self._loop.call_later(reset_after, _limiter.release_lock)

interactions/api/http/thread.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ async def create_thread_in_forum(
231231

232232
data = None
233233
if files is not MISSING and files and len(files) > 0: # edge case `None`
234-
235234
data = MultipartWriter("form-data")
236235
part = data.append_json(payload)
237236
part.set_content_disposition("form-data", name="payload_json")

0 commit comments

Comments
 (0)