Skip to content

Commit 4bdccd0

Browse files
feat: provide guild objects when guild is uncached
1 parent 91dc3fa commit 4bdccd0

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

changelog/647.breaking.rst

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:attr:`Interaction.guild` is now an unavailable guild instance if the guild could not be found in cache.
2+
3+
Importantly, this affects code which used ``inter.guild`` as a logic gate. A simple solution to this is to now use :attr:`Interaction.guild_id` instead.
4+
To know whether or not the bot is in the guild, check if the guild is present in the client's guild cache.
5+
If the cache is not enabled, checking if the bot is in the guild can be attempted by fetching the guild, but this is not recommended due to ratelimits.

disnake/interactions/base.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,19 @@ def user(self) -> Union[User, Member]:
229229

230230
@property
231231
def guild(self) -> Optional[Guild]:
232-
"""Optional[:class:`Guild`]: The guild the interaction was sent from."""
233-
return self._state._get_guild(self.guild_id)
232+
"""Optional[:class:`Guild`]: The guild the interaction was sent from.
233+
234+
.. versionchanged:: 2.9
235+
236+
Returns an unavailable :class:`Guild` object when the guild could not be resolved from cache.
237+
"""
238+
if self.guild_id is None:
239+
return None
240+
241+
return self._state._get_guild(self.guild_id) or Guild(
242+
data={"unavailable": True, "id": self.guild_id}, # type: ignore # this is a valid guild payload
243+
state=self._state,
244+
)
234245

235246
@utils.cached_slot_property("_cs_me")
236247
def me(self) -> Union[Member, ClientUser]:

0 commit comments

Comments
 (0)