Skip to content

feat: 🧑‍💻 Make Client.intents writable #2687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
29 changes: 29 additions & 0 deletions discord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions discord/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Loading