diff --git a/discord/abc.py b/discord/abc.py index af2b15dac79c..891404b33f4a 100644 --- a/discord/abc.py +++ b/discord/abc.py @@ -26,6 +26,7 @@ import copy import time +import secrets import asyncio from datetime import datetime from typing import ( @@ -1614,6 +1615,9 @@ async def send( else: flags = MISSING + if nonce is None: + nonce = secrets.randbits(64) + with handle_message_parameters( content=content, tts=tts, diff --git a/discord/gateway.py b/discord/gateway.py index 13a213ce3ee9..d15c617d14c0 100644 --- a/discord/gateway.py +++ b/discord/gateway.py @@ -606,7 +606,10 @@ def latency(self) -> float: def _can_handle_close(self) -> bool: code = self._close_code or self.socket.close_code - return code not in (1000, 4004, 4010, 4011, 4012, 4013, 4014) + # If the socket is closed remotely with 1000 and it's not our own explicit close + # then it's an improper close that should be handled and reconnected + is_improper_close = self._close_code is None and self.socket.close_code == 1000 + return is_improper_close or code not in (1000, 4004, 4010, 4011, 4012, 4013, 4014) async def poll_event(self) -> None: """Polls for a DISPATCH event and handles the general gateway loop. diff --git a/discord/http.py b/discord/http.py index fbaf447aa415..c66132055413 100644 --- a/discord/http.py +++ b/discord/http.py @@ -197,6 +197,7 @@ def handle_message_parameters( if nonce is not None: payload['nonce'] = str(nonce) + payload['enforce_nonce'] = True if message_reference is not MISSING: payload['message_reference'] = message_reference