Skip to content

Commit 21c0501

Browse files
committed
🧑‍💻 Make Client.intents writable
1 parent 8d8fb2b commit 21c0501

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

discord/client.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ def __init__(
257257
self._connection: ConnectionState = self._get_state(**options)
258258
self._connection.shard_count = self.shard_count
259259
self._closed: bool = False
260+
self._was_connected: bool = False
260261
self._ready: asyncio.Event = asyncio.Event()
261262
self._connection._get_websocket = self._get_websocket
262263
self._connection._get_client = lambda: self
@@ -639,6 +640,7 @@ async def connect(self, *, reconnect: bool = True) -> None:
639640
"initial": True,
640641
"shard_id": self.shard_id,
641642
}
643+
self._was_connected = True
642644
while not self.is_closed():
643645
try:
644646
coro = DiscordWebSocket.from_client(self, **ws_params)
@@ -885,6 +887,30 @@ def intents(self) -> Intents:
885887
"""
886888
return self._connection.intents
887889

890+
@intents.setter
891+
def intents(self, value: Any) -> None: # pyright: ignore [reportExplicitAny]
892+
"""
893+
Set the intents for this Client.
894+
895+
Parameters
896+
----------
897+
value: :class:`Intents`
898+
The intents to set for this Client.
899+
900+
Raises
901+
------
902+
TypeError
903+
The value is not an instance of Intents.
904+
R
905+
"""
906+
if not isinstance(value, Intents):
907+
raise TypeError(
908+
f"Intents must be an instance of Intents not {value.__class__!r}"
909+
)
910+
if self._was_connected:
911+
raise ClientException("Cannot change intents on a running client.")
912+
self._connection.intents = value
913+
888914
# helpers/getters
889915

890916
@property

discord/state.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,26 @@ def intents(self) -> Intents:
334334
ret.value = self._intents.value
335335
return ret
336336

337+
@intents.setter
338+
def intents(self, value: Any) -> None: # pyright: ignore [reportExplicitAny]
339+
"""
340+
Set the intents for the connection. This can only be set before the connection is
341+
started or else the connection will crash.
342+
343+
Parameters
344+
----------
345+
value: :class:`Intents`
346+
The intents to use for the connection.
347+
348+
Raises
349+
------
350+
TypeError
351+
The value passed is not of type :class:`Intents`.
352+
"""
353+
if not isinstance(value, Intents):
354+
raise TypeError(f"Intents must be of type Intents not {value.__class__!r}")
355+
self._intents.value = value.value
356+
337357
@property
338358
def voice_clients(self) -> list[VoiceClient]:
339359
return list(self._voice_clients.values())

0 commit comments

Comments
 (0)