Skip to content

Commit cb8d411

Browse files
committed
feat: context menu context is now generated
1 parent 765ef2e commit cb8d411

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

interactions/client/client.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
ModalContext,
102102
ComponentContext,
103103
AutocompleteContext,
104+
ContextMenuContext,
104105
)
105106
from interactions.models.internal.listener import Listener
106107
from interactions.models.internal.tasks import Task
@@ -257,7 +258,9 @@ def __init__(
257258
activity: Union[Activity, str] = None,
258259
auto_defer: Absent[Union[AutoDefer, bool]] = MISSING,
259260
autocomplete_context: Type[BaseContext] = AutocompleteContext,
261+
basic_logging: bool = False,
260262
component_context: Type[BaseContext] = ComponentContext,
263+
context_menu_context: Type[BaseContext] = ContextMenuContext,
261264
debug_scope: Absent["Snowflake_Type"] = MISSING,
262265
delete_unused_application_cmds: bool = False,
263266
disable_dm_commands: bool = False,
@@ -268,19 +271,18 @@ def __init__(
268271
intents: Union[int, Intents] = Intents.DEFAULT,
269272
interaction_context: Type[InteractionContext] = InteractionContext,
270273
logger: logging.Logger = MISSING,
271-
owner_ids: Iterable["Snowflake_Type"] = (),
274+
logging_level: int = logging.INFO,
272275
modal_context: Type[BaseContext] = ModalContext,
276+
owner_ids: Iterable["Snowflake_Type"] = (),
273277
send_command_tracebacks: bool = True,
274278
shard_id: int = 0,
275279
show_ratelimit_tracebacks: bool = False,
276280
slash_context: Type[BaseContext] = SlashContext,
277281
status: Status = Status.ONLINE,
282+
sync_ext: bool = True,
278283
sync_interactions: bool = True,
279284
token: str | None = None,
280-
sync_ext: bool = True,
281285
total_shards: int = 1,
282-
basic_logging: bool = False,
283-
logging_level: int = logging.INFO,
284286
**kwargs,
285287
) -> None:
286288
if logger is MISSING:
@@ -332,6 +334,8 @@ def __init__(
332334
"""The object to instantiate for Modal Context"""
333335
self.slash_context: Type[BaseContext] = slash_context
334336
"""The object to instantiate for Slash Context"""
337+
self.context_menu_context: Type[BaseContext] = context_menu_context
338+
"""The object to instantiate for Context Menu Context"""
335339

336340
self.token: str | None = token
337341

@@ -1553,7 +1557,10 @@ async def get_context(self, data: dict) -> InteractionContext:
15531557
case InteractionType.MODAL_RESPONSE:
15541558
cls = self.modal_context.from_dict(self, data)
15551559
case InteractionType.APPLICATION_COMMAND:
1556-
cls = self.slash_context.from_dict(self, data)
1560+
if data["data"].get("target_id"):
1561+
cls = self.context_menu_context.from_dict(self, data)
1562+
else:
1563+
cls = self.slash_context.from_dict(self, data)
15571564
case _:
15581565
self.logger.warning(f"Unknown interaction type [{data['type']}] - please update or report this.")
15591566
cls = self.interaction_context.from_dict(self, data)

interactions/models/internal/context.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from interactions.models.discord.components import BaseComponent
1111
from interactions.models.discord.file import UPLOADABLE_TYPE
1212
from interactions.models.discord.sticker import Sticker
13+
from interactions.models.discord.user import Member, User
1314

1415
from interactions.models.internal.command import BaseCommand
1516
from interactions.client.mixins.modal import ModalMixin
@@ -21,6 +22,7 @@
2122
MessageFlags,
2223
InteractionType,
2324
ComponentType,
25+
CommandType,
2426
)
2527
from interactions.models.discord.message import (
2628
AllowedMentions,
@@ -584,6 +586,8 @@ class ContextMenuContext(InteractionContext, ModalMixin):
584586
"""The id of the target of the context menu."""
585587
editing_origin: bool
586588
"""Whether you have deferred the interaction and are editing the original response."""
589+
target_type: None | CommandType
590+
"""The type of the target of the context menu."""
587591

588592
def __init__(self, client: "interactions.Client") -> None:
589593
super().__init__(client)
@@ -592,7 +596,8 @@ def __init__(self, client: "interactions.Client") -> None:
592596
@classmethod
593597
def from_dict(cls, client: "interactions.Client", payload: dict) -> Self:
594598
instance = super().from_dict(client, payload)
595-
instance.target_id = payload["target_id"]
599+
instance.target_id = Snowflake(payload["data"]["target_id"])
600+
instance.target_type = CommandType(payload["data"]["type"])
596601
return instance
597602

598603
async def defer(self, *, ephemeral: bool = False, edit_origin: bool = False) -> None:
@@ -619,6 +624,16 @@ async def defer(self, *, ephemeral: bool = False, edit_origin: bool = False) ->
619624
self.ephemeral = ephemeral
620625
self.editing_origin = edit_origin
621626

627+
@property
628+
def target(self) -> None | Message | User | Member:
629+
"""
630+
The target of the context menu.
631+
632+
Returns:
633+
The target of the context menu.
634+
"""
635+
return self.resolved.get(self.target_id)
636+
622637

623638
class ComponentContext(InteractionContext):
624639
values: list[str]

0 commit comments

Comments
 (0)