Skip to content

Commit

Permalink
try a different approach
Browse files Browse the repository at this point in the history
  • Loading branch information
onerandomusername committed Sep 23, 2024
1 parent fc05d78 commit da11276
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
29 changes: 29 additions & 0 deletions disnake/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
from .app_commands import APIApplicationCommand
from .asset import AssetBytes
from .automod import AutoModTriggerMetadata
from .interactions import Interaction
from .permissions import Permissions
from .state import ConnectionState
from .template import Template
Expand All @@ -107,6 +108,7 @@
Guild as GuildPayload,
GuildFeature,
MFALevel,
PartialGuild as PartialGuildPayload,
)
from .types.integration import IntegrationType
from .types.role import CreateRole as CreateRolePayload
Expand Down Expand Up @@ -4963,6 +4965,33 @@ async def onboarding(self) -> Onboarding:
return Onboarding(data=data, guild=self)


class PartialInteractionGuild(Guild):
"""Reimplementation of :class:`Guild` for guilds interactions."""

def __init__(
self,
*,
state: ConnectionState,
data: PartialGuildPayload,
interaction: Interaction,
) -> None:
super().__init__(state=state, data=data)
# init the fake data
self._add_role(
Role(
state=state,
guild=self,
data={"id": self.id, "name": "@everyone"}, # type: ignore
)
)
self._add_channel(interaction.channel) # type: ignore
# honestly we cannot set me, because we do not necessarily have a user in the guild

@property
def me(self) -> Any:
return self._state.user


PlaceholderID = NewType("PlaceholderID", int)


Expand Down
11 changes: 2 additions & 9 deletions disnake/interactions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
NotFound,
)
from ..flags import MessageFlags
from ..guild import Guild
from ..guild import Guild, PartialInteractionGuild
from ..i18n import Localized
from ..member import Member
from ..message import Attachment, Message
Expand Down Expand Up @@ -282,14 +282,7 @@ def guild(self) -> Optional[Guild]:
# create a guild mash
# honestly we should cache this for the duration of the interaction
# but not if we fetch it from the cache, just the result of this creation
guild = Guild(data=self._guild, state=self._state)
guild._add_role(
Role(
state=self._state,
guild=guild,
data={"id": 1, "name": "@everyone"}, # type: ignore
)
)
guild = PartialInteractionGuild(data=self._guild, state=self._state, interaction=self)
return guild

@utils.cached_slot_property("_cs_me")
Expand Down

0 comments on commit da11276

Please sign in to comment.