Skip to content

Commit ac6c00a

Browse files
feat: provide guild objects when guild is uncached
1 parent 86d3b4a commit ac6c00a

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

changelog/647.breaking.rst

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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. If intents are not enabled, it is not possible to detect whether or not the bot is in the guild.

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.6
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)