Skip to content

Commit a935318

Browse files
feat: provide guild objects when guild is uncached
1 parent 6d0af62 commit a935318

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

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

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

0 commit comments

Comments
 (0)