Skip to content

Commit 0efcc5a

Browse files
feat: provide guild objects when guild is uncached
1 parent f6c3491 commit 0efcc5a

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
@@ -208,8 +208,19 @@ def user(self) -> Union[User, Member]:
208208

209209
@property
210210
def guild(self) -> Optional[Guild]:
211-
"""Optional[:class:`Guild`]: The guild the interaction was sent from."""
212-
return self._state._get_guild(self.guild_id)
211+
"""Optional[:class:`Guild`]: The guild the interaction was sent from.
212+
213+
.. versionchanged:: 2.6
214+
215+
Returns an unavailable :class:`Guild` object when the guild could not be resolved from cache.
216+
"""
217+
if self.guild_id is None:
218+
return None
219+
220+
return self._state._get_guild(self.guild_id) or Guild(
221+
data={"unavailable": True, "id": self.guild_id}, # type: ignore # this is a valid guild payload
222+
state=self._state,
223+
)
213224

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

0 commit comments

Comments
 (0)