Skip to content

Commit 30debeb

Browse files
feat: provide guild objects when guild is uncached
1 parent 060ae1f commit 30debeb

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

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

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

0 commit comments

Comments
 (0)