Skip to content

Commit da11276

Browse files
try a different approach
1 parent fc05d78 commit da11276

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

disnake/guild.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
from .app_commands import APIApplicationCommand
9696
from .asset import AssetBytes
9797
from .automod import AutoModTriggerMetadata
98+
from .interactions import Interaction
9899
from .permissions import Permissions
99100
from .state import ConnectionState
100101
from .template import Template
@@ -107,6 +108,7 @@
107108
Guild as GuildPayload,
108109
GuildFeature,
109110
MFALevel,
111+
PartialGuild as PartialGuildPayload,
110112
)
111113
from .types.integration import IntegrationType
112114
from .types.role import CreateRole as CreateRolePayload
@@ -4963,6 +4965,33 @@ async def onboarding(self) -> Onboarding:
49634965
return Onboarding(data=data, guild=self)
49644966

49654967

4968+
class PartialInteractionGuild(Guild):
4969+
"""Reimplementation of :class:`Guild` for guilds interactions."""
4970+
4971+
def __init__(
4972+
self,
4973+
*,
4974+
state: ConnectionState,
4975+
data: PartialGuildPayload,
4976+
interaction: Interaction,
4977+
) -> None:
4978+
super().__init__(state=state, data=data)
4979+
# init the fake data
4980+
self._add_role(
4981+
Role(
4982+
state=state,
4983+
guild=self,
4984+
data={"id": self.id, "name": "@everyone"}, # type: ignore
4985+
)
4986+
)
4987+
self._add_channel(interaction.channel) # type: ignore
4988+
# honestly we cannot set me, because we do not necessarily have a user in the guild
4989+
4990+
@property
4991+
def me(self) -> Any:
4992+
return self._state.user
4993+
4994+
49664995
PlaceholderID = NewType("PlaceholderID", int)
49674996

49684997

disnake/interactions/base.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
NotFound,
4343
)
4444
from ..flags import MessageFlags
45-
from ..guild import Guild
45+
from ..guild import Guild, PartialInteractionGuild
4646
from ..i18n import Localized
4747
from ..member import Member
4848
from ..message import Attachment, Message
@@ -282,14 +282,7 @@ def guild(self) -> Optional[Guild]:
282282
# create a guild mash
283283
# honestly we should cache this for the duration of the interaction
284284
# but not if we fetch it from the cache, just the result of this creation
285-
guild = Guild(data=self._guild, state=self._state)
286-
guild._add_role(
287-
Role(
288-
state=self._state,
289-
guild=guild,
290-
data={"id": 1, "name": "@everyone"}, # type: ignore
291-
)
292-
)
285+
guild = PartialInteractionGuild(data=self._guild, state=self._state, interaction=self)
293286
return guild
294287

295288
@utils.cached_slot_property("_cs_me")

0 commit comments

Comments
 (0)