diff --git a/CHANGELOG.md b/CHANGELOG.md index e336f0e2fe..11149625bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -94,6 +94,8 @@ These changes are available on the `master` branch, but have not yet been releas ([#2176](https://github.com/Pycord-Development/pycord/pull/2176)) - Updated `Guild.filesize_limit` to 10 MB instead of 25 MB following Discord's API changes. ([#2671](https://github.com/Pycord-Development/pycord/pull/2671)) +- Made `Client.intents` and `ConnectionState.intents` writable as long as the client + wasn't started. ([#2687](https://github.com/Pycord-Development/pycord/pull/2687)) ### Deprecated diff --git a/discord/client.py b/discord/client.py index 4db53e33e3..6d4dd3bc13 100644 --- a/discord/client.py +++ b/discord/client.py @@ -257,6 +257,7 @@ def __init__( self._connection: ConnectionState = self._get_state(**options) self._connection.shard_count = self.shard_count self._closed: bool = False + self._was_connected: bool = False self._ready: asyncio.Event = asyncio.Event() self._connection._get_websocket = self._get_websocket self._connection._get_client = lambda: self @@ -639,6 +640,7 @@ async def connect(self, *, reconnect: bool = True) -> None: "initial": True, "shard_id": self.shard_id, } + self._was_connected = True while not self.is_closed(): try: coro = DiscordWebSocket.from_client(self, **ws_params) @@ -885,6 +887,33 @@ def intents(self) -> Intents: """ return self._connection.intents + @intents.setter + def intents(self, value: Any) -> None: # pyright: ignore [reportExplicitAny] + """ + Set the intents for this Client. + + Parameters + ---------- + value: :class:`Intents` + The intents to set for this Client. + + Raises + ------ + TypeError + The value is not an instance of Intents. + AttributeError + The intents cannot be changed after the connection is established. + """ + if not isinstance(value, Intents): + raise TypeError( + f"Intents must be an instance of Intents not {value.__class__!r}" + ) + if self._was_connected: + raise AttributeError( + "Cannot change intents after the connection is established." + ) + self._connection.intents = value + # helpers/getters @property diff --git a/discord/state.py b/discord/state.py index cf74d99285..34ef4703ab 100644 --- a/discord/state.py +++ b/discord/state.py @@ -334,6 +334,33 @@ def intents(self) -> Intents: ret.value = self._intents.value return ret + @intents.setter + def intents(self, value: Any) -> None: # pyright: ignore [reportExplicitAny] + """ + Set the intents for the connection. This can only be set before the connection is + started or else the connection will crash. + + Parameters + ---------- + value: :class:`Intents` + The intents to use for the connection. + + Raises + ------ + TypeError + The value passed is not of type :class:`Intents`. + AttributeError + The intents cannot be changed after the connection is established. + """ + if not isinstance(value, Intents): + raise TypeError(f"Intents must be of type Intents not {value.__class__!r}") + ws = self._get_websocket() + if ws and ws.open: + raise AttributeError( + "Cannot change intents after the connection is established." + ) + self._intents.value = value.value + @property def voice_clients(self) -> list[VoiceClient]: return list(self._voice_clients.values())