Skip to content

Commit 2d829aa

Browse files
authored
✨ Make intents writable while the connection is not established (#70)
1 parent 636d07a commit 2d829aa

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

src/custom/__init__.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,50 @@ async def get_context(
135135
ctx.load_translations()
136136
return ctx
137137

138+
@property
139+
@override
140+
def intents(self) -> discord.Intents:
141+
"""The intents configured for this connection or a copy of the intents if the bot is connected.
142+
143+
Returns
144+
-------
145+
:class:`Intents`
146+
The intents configured for this Client.
147+
148+
"""
149+
# _connection._intents returns the intents themselves, _connection.intents returns a copy
150+
# so if the bot is connected, we return a copy so that changes don't affect the connection
151+
# if the bot is not connected, we return the actual intents so that the user can make changes
152+
if self.ws is None: # pyright: ignore [reportUnnecessaryComparison]
153+
return self._connection._intents # noqa: SLF001 # pyright: ignore [reportPrivateUsage]
154+
return self._connection.intents
155+
156+
@intents.setter
157+
def intents(self, value: Any) -> None: # pyright: ignore [reportExplicitAny]
158+
"""Set the intents for this Client.
159+
160+
Parameters
161+
----------
162+
value: :class:`Intents`
163+
The intents to set for this Client.
164+
165+
Raises
166+
------
167+
TypeError
168+
The value is not an instance of Intents.
169+
AttributeError
170+
The intents cannot be changed after the connection is established.
171+
172+
"""
173+
if not isinstance(value, discord.Intents):
174+
raise TypeError(f"Intents must be an instance of Intents not {value.__class__!r}")
175+
if self.ws is not None: # pyright: ignore [reportUnnecessaryComparison]
176+
raise AttributeError("Cannot change intents after the connection is established.")
177+
self._connection._intents.value = value.value # noqa: SLF001 # pyright: ignore [reportPrivateUsage]
178+
138179

139180
if not TYPE_CHECKING:
140-
Context: ApplicationContext = ApplicationContext # pyright: ignore [reportRedeclaration]
181+
Context: ApplicationContext = ApplicationContext
141182

142183
if TYPE_CHECKING: # temp fix for https://github.com/Pycord-Development/pycord/pull/2611
143184
type Context = ExtContext | ApplicationContext

0 commit comments

Comments
 (0)